Exemple #1
0
            /// <summary>
            /// 删除目录
            /// </summary>
            /// <param name="directory">目录信息</param>
            public bool Delete(DirectoryInfo directory)
            {
                permission permission = getPermission(Client, directory.fullName().toLower());

                if (verify(permission, permissionType.Write))
                {
                    bool isDelete = true;
                    foreach (FileInfo file in directory.GetFiles())
                    {
                        try
                        {
                            permission.Backup(file, BackupIdentity);
                        }
                        catch (Exception error)
                        {
                            log.Error.Add(error, null, false);
                            isDelete = false;
                        }
                    }
                    foreach (DirectoryInfo nextDirectory in directory.GetDirectories())
                    {
                        isDelete &= Delete(nextDirectory);
                    }
                    if (isDelete)
                    {
                        directory.Delete();
                        return(true);
                    }
                }
                return(false);
            }
Exemple #2
0
        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);
        }
Exemple #3
0
            /// <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);
            }
Exemple #4
0
 private listName[] delete(fastCSharp.net.tcp.commandServer.socket client, string path, listName[] listNames, int backupIdentity)
 {
     if (verify(client))
     {
         DirectoryInfo pathInfo = new DirectoryInfo(path);
         if (pathInfo.Exists)
         {
             permission permission = getPermission(client, path = pathInfo.fullName().toLower());
             if (verify(permission, permissionType.Write))
             {
                 directoryDeleter directoryDeleter = new directoryDeleter {
                     Client = client, BackupIdentity = backupIdentity
                 };
                 Monitor.Enter(deleteLock);
                 try
                 {
                     deleteFileNames.Clear();
                     deleteDirectoryNames.Clear();
                     foreach (listName listName in listNames)
                     {
                         (listName.IsFile ? deleteFileNames : deleteDirectoryNames).Add(listName.LowerName);
                     }
                     if (deleteFileNames.Count != 0)
                     {
                         foreach (FileInfo file in pathInfo.GetFiles())
                         {
                             if (deleteFileNames.Contains(file.Name.toLower()))
                             {
                                 try
                                 {
                                     permission.Backup(file, backupIdentity);
                                 }
                                 catch (Exception error)
                                 {
                                     log.Error.Add(error, null, false);
                                 }
                             }
                         }
                     }
                     if (deleteDirectoryNames.Count != 0)
                     {
                         foreach (DirectoryInfo directory in pathInfo.GetDirectories())
                         {
                             if (deleteDirectoryNames.Contains(directory.Name.toLower()))
                             {
                                 try
                                 {
                                     directoryDeleter.Delete(directory);
                                 }
                                 catch (Exception error)
                                 {
                                     log.Error.Add(error, null, false);
                                 }
                             }
                         }
                     }
                 }
                 finally { Monitor.Exit(deleteLock); }
                 return(getListName(pathInfo));
             }
         }
     }
     return(null);
 }