~ifolder-dev/simias/trunk-packaging

« back to all changes in this revision

Viewing changes to src/webservices/.svn/text-base/iFolderFile.cs.svn-base

  • Committer: Jorge O. Castro
  • Date: 2007-12-03 06:56:46 UTC
  • Revision ID: jorge@ubuntu.com-20071203065646-mupcnjcwgm5mnhyt
* Remove a bunch of .svn directories we no longer need.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/****************************************************************************
2
 
|
3
 
| Copyright (c) 2007 Novell, Inc.
4
 
| All Rights Reserved.
5
 
|
6
 
| This program is free software; you can redistribute it and/or
7
 
| modify it under the terms of version 2 of the GNU General Public License as
8
 
| published by the Free Software Foundation.
9
 
|
10
 
| This program is distributed in the hope that it will be useful,
11
 
| but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
| GNU General Public License for more details.
14
 
|
15
 
| You should have received a copy of the GNU General Public License
16
 
| along with this program; if not, contact Novell, Inc.
17
 
|
18
 
| To contact Novell about this file by physical or electronic mail,
19
 
| you may find current contact information at www.novell.com 
20
 
|
21
 
| Author: Rob 
22
 
|***************************************************************************/
23
 
 
24
 
using System;
25
 
using System.IO;
26
 
using System.Collections;
27
 
 
28
 
using Simias;
29
 
using Simias.Client;
30
 
using Simias.Storage;
31
 
using Simias.Policy;
32
 
 
33
 
namespace iFolder.WebService
34
 
{
35
 
        /// <summary>
36
 
        /// An iFolder File
37
 
        /// </summary>
38
 
        [Serializable]
39
 
        public class iFolderFile
40
 
        {
41
 
                /// <summary>
42
 
                /// The File ID
43
 
                /// </summary>
44
 
                private string id;
45
 
 
46
 
                /// <summary>
47
 
                /// The File Path
48
 
                /// </summary>
49
 
                private string path;
50
 
 
51
 
                /// <summary>
52
 
                /// The File Stream
53
 
                /// </summary>
54
 
                private FileStream stream;
55
 
 
56
 
                /// <summary>
57
 
                /// The Collection Object
58
 
                /// </summary>
59
 
                private Collection collection;
60
 
 
61
 
                /// <summary>
62
 
                /// The FileNode Object
63
 
                /// </summary>
64
 
                private FileNode node;
65
 
 
66
 
                /// <summary>
67
 
                /// Is the File Changing?
68
 
                /// </summary>
69
 
                private bool updating;
70
 
 
71
 
                /// <summary>
72
 
                /// The New File Length
73
 
                /// </summary>
74
 
                private long length;
75
 
 
76
 
                /// <summary>
77
 
                /// The Access User ID
78
 
                /// </summary>
79
 
                private string accessID;
80
 
 
81
 
                /// <summary>
82
 
                /// The Backup File Path
83
 
                /// </summary>
84
 
                private string backupPath;
85
 
 
86
 
                /// <summary>
87
 
                /// Access Log
88
 
                /// </summary>
89
 
                protected SimiasAccessLogger log;
90
 
 
91
 
                /// <summary>
92
 
                /// Member
93
 
                /// </summary>
94
 
                private Member member;
95
 
 
96
 
                /// <summary>
97
 
                /// Constructor
98
 
                /// </summary>
99
 
                /// <param name="ifolderID">The ID of the iFolder.</param>
100
 
                /// <param name="entryID">The ID of the Entry.</param>
101
 
                /// <param name="accessID">The Access ID.</param>
102
 
                public iFolderFile(string ifolderID, string entryID, string accessID)
103
 
                {
104
 
                        Store store = Store.GetStore();
105
 
 
106
 
                        collection = store.GetCollectionByID(ifolderID);
107
 
 
108
 
                        if (collection == null)
109
 
                        {
110
 
                                throw new iFolderDoesNotExistException(ifolderID);
111
 
                        }
112
 
                        
113
 
                        // impersonate
114
 
                        this.accessID = accessID;
115
 
                        iFolder.Impersonate(collection, accessID);
116
 
                        
117
 
                        // member
118
 
                        member = collection.GetMemberByID(accessID);
119
 
 
120
 
                        // does member exist?
121
 
                        if (member == null)
122
 
                        {
123
 
                                throw new MemberDoesNotExistException(accessID);
124
 
                        }
125
 
                        
126
 
                        // node
127
 
                        Node n = collection.GetNodeByID(entryID);
128
 
 
129
 
                        // does the node exist?
130
 
                        if (n == null)
131
 
                        {
132
 
                                throw new EntryDoesNotExistException(entryID);
133
 
                        }
134
 
 
135
 
                        // is the node a file
136
 
                        if (!n.IsBaseType(NodeTypes.FileNodeType))
137
 
                        {
138
 
                                throw new FileDoesNotExistException(entryID);
139
 
                        }
140
 
 
141
 
                        // log
142
 
                        log = new SimiasAccessLogger(member.Name, collection.ID);
143
 
 
144
 
                        // node
145
 
                        node = (FileNode)n;
146
 
                        id = String.Format("{0}:{1}", collection.ID, n.ID);
147
 
                        path = node.GetFullPath(collection);
148
 
                        updating = false;
149
 
                }
150
 
 
151
 
                /// <summary>
152
 
                /// The File ID.
153
 
                /// </summary>
154
 
                public string ID
155
 
                {
156
 
                        get { return id; }
157
 
                }
158
 
 
159
 
                /// <summary>
160
 
                /// Open the File for Reading.
161
 
                /// </summary>
162
 
                public void OpenRead()
163
 
                {
164
 
                        try
165
 
                        {
166
 
                                stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
167
 
 
168
 
                                // log
169
 
                                log.LogAccess("OpenRead", node.GetRelativePath(), node.ID, "Success");
170
 
                        }
171
 
                        catch
172
 
                        {
173
 
                                // log
174
 
                                log.LogAccess("OpenRead", node.GetRelativePath(), node.ID, "Failed");
175
 
 
176
 
                                Close();
177
 
 
178
 
                throw;
179
 
                        }
180
 
                }
181
 
 
182
 
                /// <summary>
183
 
                /// Open the File for Writing.
184
 
                /// </summary>
185
 
                /// <param name="length">New file length.</param>
186
 
                public void OpenWrite(long length)
187
 
                {
188
 
                        this.length = length;
189
 
 
190
 
                        try
191
 
                        {
192
 
                                // check access
193
 
                                member = collection.GetMemberByID(accessID);
194
 
 
195
 
                                // does the member exist?
196
 
                                if (member == null)
197
 
                                {
198
 
                                        throw new MemberDoesNotExistException(accessID);
199
 
                                }
200
 
                                
201
 
                                // does the member have wright rights
202
 
                                if ((member.Rights != Access.Rights.Admin) && (member.Rights != Access.Rights.ReadWrite))
203
 
                                {
204
 
                                        throw new AccessException(collection, member, Access.Rights.ReadWrite);
205
 
                                }
206
 
 
207
 
                                // backup file
208
 
                                backupPath = String.Format("{0}.simias.temp", path);
209
 
                                File.Copy(path, backupPath, true);
210
 
                                long deltaSize = length - (new FileInfo(backupPath)).Length;
211
 
 
212
 
                                // open file
213
 
                                stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
214
 
                                updating = true;
215
 
 
216
 
                                // check file size policy
217
 
                                FileSizeFilter fsFilter = FileSizeFilter.Get(collection);
218
 
                                if (!fsFilter.Allowed(deltaSize))
219
 
                                {
220
 
                                        throw new FileSizeException(node.Name);
221
 
                                }
222
 
 
223
 
                                // check disk quota policy
224
 
                                DiskSpaceQuota dsQuota = DiskSpaceQuota.Get(collection);
225
 
                                if (!dsQuota.Allowed(deltaSize))
226
 
                                {
227
 
                                        throw new DiskQuotaException(node.Name);
228
 
                                }
229
 
 
230
 
                                // log
231
 
                                log.LogAccess("OpenWrite", node.GetRelativePath(), node.ID, "Success");
232
 
                        }
233
 
                        catch
234
 
                        {
235
 
                                // log
236
 
                                log.LogAccess("OpenWrite", node.GetRelativePath(), node.ID, "Failed");
237
 
 
238
 
                Close(true);
239
 
 
240
 
                throw;
241
 
                        }
242
 
                }
243
 
 
244
 
                /// <summary>
245
 
                /// Read from the File.
246
 
                /// </summary>
247
 
                /// <param name="size"></param>
248
 
                /// <returns></returns>
249
 
                public byte[] Read(int size)
250
 
                {
251
 
                        byte[] buffer = null;
252
 
 
253
 
                        long togo = stream.Length - stream.Position;
254
 
 
255
 
                        // pre-size array
256
 
                        if (togo < size)
257
 
                        {
258
 
                                size = (int)togo;
259
 
                        }
260
 
 
261
 
                        // check size
262
 
                        if (size > 0)
263
 
                        {
264
 
                                buffer = new byte[size];
265
 
 
266
 
                                int count = stream.Read(buffer, 0, size);
267
 
                        
268
 
                                // correct array
269
 
                                if (count < 1)
270
 
                                {
271
 
                                        // done
272
 
                                        buffer = null;
273
 
                                        Close();
274
 
                                }
275
 
                                else if (size != count)
276
 
                                {
277
 
                                        byte[] temp = new byte[count];
278
 
                                        Array.Copy(buffer, temp, count);
279
 
                                        buffer = temp;
280
 
                                }
281
 
                        }
282
 
 
283
 
                        return buffer;
284
 
                }
285
 
 
286
 
                /// <summary>
287
 
                /// Write to the File.
288
 
                /// </summary>
289
 
                /// <param name="buffer"></param>
290
 
                public void Write(byte[] buffer)
291
 
                {
292
 
                        try
293
 
                        {
294
 
                                if ((stream.Position + buffer.Length) > length)
295
 
                                {
296
 
                                        throw new FileSizeException(node.Name);
297
 
                                }
298
 
 
299
 
                                stream.Write(buffer, 0, buffer.Length);
300
 
                        }
301
 
                        catch
302
 
                        {
303
 
                Close(true);
304
 
 
305
 
                throw;
306
 
                        }
307
 
                }
308
 
 
309
 
                /// <summary>
310
 
                /// Close the File.
311
 
                /// </summary>
312
 
                public void Close()
313
 
                {
314
 
                        Close(false);
315
 
                }
316
 
                
317
 
                /// <summary>
318
 
                /// Close the File.
319
 
                /// </summary>
320
 
                public void Close(bool failed)
321
 
                {
322
 
                        if (node != null)
323
 
                        {
324
 
                                try
325
 
                                {
326
 
                                        // stream
327
 
                                        if (stream != null)
328
 
                                        {
329
 
                                                stream.Close();
330
 
                                                stream = null;
331
 
                                        }
332
 
 
333
 
                                        // log
334
 
                                        log.LogAccess("Closed", node.GetRelativePath(), node.ID, "Success");
335
 
    
336
 
                                        if (updating && !failed)
337
 
                                        {
338
 
                                                try
339
 
                                                {
340
 
                                                        node.UpdateFileInfo(collection);
341
 
                                                        collection.Commit(node);
342
 
                                                }
343
 
                                                catch
344
 
                                                {
345
 
                                                        failed = true;
346
 
                                                        throw;
347
 
                                                }
348
 
                                        }
349
 
                                }
350
 
                                finally
351
 
                                {
352
 
                                        node = null;
353
 
 
354
 
                                        // check backup file
355
 
                                        if (File.Exists(backupPath))
356
 
                                        {
357
 
                                                if (failed)
358
 
                                                {
359
 
                                                        // restore backup file
360
 
                                                        File.Copy(backupPath, path, true);
361
 
                                                }
362
 
 
363
 
                                                // delete backup file
364
 
                                                File.Delete(backupPath);
365
 
                                        }
366
 
                                }
367
 
                        }
368
 
                }
369
 
        }
370
 
}