1
/****************************************************************************
3
| Copyright (c) 2007 Novell, Inc.
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.
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.
15
| You should have received a copy of the GNU General Public License
16
| along with this program; if not, contact Novell, Inc.
18
| To contact Novell about this file by physical or electronic mail,
19
| you may find current contact information at www.novell.com
22
|***************************************************************************/
26
using System.Collections;
33
namespace iFolder.WebService
39
public class iFolderFile
54
private FileStream stream;
57
/// The Collection Object
59
private Collection collection;
62
/// The FileNode Object
64
private FileNode node;
67
/// Is the File Changing?
69
private bool updating;
72
/// The New File Length
77
/// The Access User ID
79
private string accessID;
82
/// The Backup File Path
84
private string backupPath;
89
protected SimiasAccessLogger log;
94
private Member member;
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)
104
Store store = Store.GetStore();
106
collection = store.GetCollectionByID(ifolderID);
108
if (collection == null)
110
throw new iFolderDoesNotExistException(ifolderID);
114
this.accessID = accessID;
115
iFolder.Impersonate(collection, accessID);
118
member = collection.GetMemberByID(accessID);
120
// does member exist?
123
throw new MemberDoesNotExistException(accessID);
127
Node n = collection.GetNodeByID(entryID);
129
// does the node exist?
132
throw new EntryDoesNotExistException(entryID);
135
// is the node a file
136
if (!n.IsBaseType(NodeTypes.FileNodeType))
138
throw new FileDoesNotExistException(entryID);
142
log = new SimiasAccessLogger(member.Name, collection.ID);
146
id = String.Format("{0}:{1}", collection.ID, n.ID);
147
path = node.GetFullPath(collection);
160
/// Open the File for Reading.
162
public void OpenRead()
166
stream = File.Open(path, FileMode.Open, FileAccess.Read, FileShare.Read);
169
log.LogAccess("OpenRead", node.GetRelativePath(), node.ID, "Success");
174
log.LogAccess("OpenRead", node.GetRelativePath(), node.ID, "Failed");
183
/// Open the File for Writing.
185
/// <param name="length">New file length.</param>
186
public void OpenWrite(long length)
188
this.length = length;
193
member = collection.GetMemberByID(accessID);
195
// does the member exist?
198
throw new MemberDoesNotExistException(accessID);
201
// does the member have wright rights
202
if ((member.Rights != Access.Rights.Admin) && (member.Rights != Access.Rights.ReadWrite))
204
throw new AccessException(collection, member, Access.Rights.ReadWrite);
208
backupPath = String.Format("{0}.simias.temp", path);
209
File.Copy(path, backupPath, true);
210
long deltaSize = length - (new FileInfo(backupPath)).Length;
213
stream = File.Open(path, FileMode.Create, FileAccess.Write, FileShare.None);
216
// check file size policy
217
FileSizeFilter fsFilter = FileSizeFilter.Get(collection);
218
if (!fsFilter.Allowed(deltaSize))
220
throw new FileSizeException(node.Name);
223
// check disk quota policy
224
DiskSpaceQuota dsQuota = DiskSpaceQuota.Get(collection);
225
if (!dsQuota.Allowed(deltaSize))
227
throw new DiskQuotaException(node.Name);
231
log.LogAccess("OpenWrite", node.GetRelativePath(), node.ID, "Success");
236
log.LogAccess("OpenWrite", node.GetRelativePath(), node.ID, "Failed");
245
/// Read from the File.
247
/// <param name="size"></param>
248
/// <returns></returns>
249
public byte[] Read(int size)
251
byte[] buffer = null;
253
long togo = stream.Length - stream.Position;
264
buffer = new byte[size];
266
int count = stream.Read(buffer, 0, size);
275
else if (size != count)
277
byte[] temp = new byte[count];
278
Array.Copy(buffer, temp, count);
287
/// Write to the File.
289
/// <param name="buffer"></param>
290
public void Write(byte[] buffer)
294
if ((stream.Position + buffer.Length) > length)
296
throw new FileSizeException(node.Name);
299
stream.Write(buffer, 0, buffer.Length);
320
public void Close(bool failed)
334
log.LogAccess("Closed", node.GetRelativePath(), node.ID, "Success");
336
if (updating && !failed)
340
node.UpdateFileInfo(collection);
341
collection.Commit(node);
355
if (File.Exists(backupPath))
359
// restore backup file
360
File.Copy(backupPath, path, true);
363
// delete backup file
364
File.Delete(backupPath);