private fileState createDirectory(fastCSharp.net.tcp.commandServer.socket client, string path) { fileState fileState = fileState.NoPermission; try { if (verify(client)) { DirectoryInfo directory = new DirectoryInfo(path); if (verify(client, path = directory.fullName().toLower(), permissionType.Write)) { if (directory.Exists) { return(fileState.Success); } directory.Create(); return(fileState.FileNotFound); } } } catch (Exception error) { fileState = fileState.Exception; log.Error.Add(error, path, false); } return(fileState); }
/// <summary> /// 文件操作结束 /// </summary> /// <param name="fileState">文件操作状态</param> private void push(fileState fileState) { Func <returnValue <fileState>, bool> onUpload = this.onUpload; this.onUpload = null; pub.Dispose(ref fileStream); typePool <uploader> .PushNotNull(this); onUpload(fileState); }
private fileState upload(fastCSharp.net.tcp.commandServer.socket client, listName listName, byte[] data, int backupIdentity, bool isTimeVersion) { fileState fileState = fileState.NoPermission; try { if (verify(client)) { FileInfo file = new FileInfo(listName.Name); listName.Set(file.FullName); permission permission = getPermission(client, listName.LowerName); if (verify(permission, permissionType.Write)) { bool isFile = file.Exists; if (isFile && isTimeVersion && file.LastWriteTimeUtc > listName.LastWriteTime) { return(server.fileState.TimeVersionError); } if (!isFile || file.LastWriteTimeUtc != listName.LastWriteTime || listName.Length != file.Length) { if (isFile) { permission.Backup(file, backupIdentity); } File.WriteAllBytes(listName.Name, data); new FileInfo(listName.Name).LastWriteTimeUtc = listName.LastWriteTime; } fileState = fileState.Success; } } } catch (Exception error) { fileState = fileState.Exception; log.Error.Add(error, listName.Name, false); } return(fileState); }
private void upload(fastCSharp.net.tcp.commandServer.socket client, listName listName, Stream fileStream, int backupIdentity, bool isTimeVersion , Func <returnValue <fileState>, bool> onUpload) { fileState fileState = fileState.NoPermission; try { if (verify(client)) { FileInfo file = new FileInfo(listName.Name); listName.Set(file.FullName); permission permission = getPermission(client, listName.LowerName); if (verify(permission, permissionType.Write)) { DirectoryInfo directory = file.Directory; if (!directory.Exists) { directory.Create(); } Action upload = uploader.GetUpload(permission, onUpload, listName, fileStream, backupIdentity, isTimeVersion); onUpload = null; fileStream = null; fastCSharp.threading.threadPool.TinyPool.Start(upload); } } } catch (Exception error) { fileState = fileState.Exception; log.Error.Add(error, listName.Name, false); } pub.Dispose(ref fileStream); if (onUpload != null) { onUpload(fileState); } }
/// <summary> /// 上传文件 /// </summary> private void upload() { fileState fileState = fileState.Exception; FileStream writeStream = null; int isWriteTime = 0; try { FileInfo fileInfo = new FileInfo(listName.Name); if (fileInfo.Exists) { if (isTimeVersion && fileInfo.LastWriteTimeUtc > listName.LastWriteTime) { fileState = server.fileState.TimeVersionError; } else if (fileInfo.LastWriteTimeUtc == listName.LastWriteTime && listName.Length >= fileInfo.Length) { writeStream = new FileStream(listName.Name, FileMode.Open, FileAccess.Write, FileShare.None); writeStream.Seek(fileInfo.Length, SeekOrigin.Begin); } else { permission.Backup(fileInfo, backupIdentity); } } if (fileState != server.fileState.TimeVersionError) { if (writeStream == null) { writeStream = new FileStream(listName.Name, FileMode.CreateNew, FileAccess.Write, FileShare.None); } isWriteTime = 1; long position = writeStream.Position, length = fileStream.Length; if (position < length) { if (position != 0) { fileStream.Seek(position, SeekOrigin.Begin); } while (position < length) { int read = fileStream.Read(buffer, 0, buffer.Length); if (read > 0) { writeStream.Write(buffer, 0, read); position += read; } else { break; } } } fileState = position == length ? fileState.Success : fileState.LengthError; } } catch (Exception error) { log.Error.Add(error, null, false); } Exception exception = null; pub.Dispose(ref writeStream, ref exception); if (exception != null) { log.Error.Add(exception, null, false); if (fileState == fileState.Success) { fileState = fileState.Exception; } } if (isWriteTime != 0) { try { new FileInfo(listName.Name).LastWriteTimeUtc = listName.LastWriteTime; } catch (Exception error) { log.Error.Add(error, null, false); } } push(fileState); }