Beispiel #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);
            }
Beispiel #2
0
        /// <summary>
        /// 获取文件权限
        /// </summary>
        /// <param name="client">客户端标识</param>
        /// <param name="fileName">文件名</param>
        /// <returns>文件权限,失败返回null</returns>
        private unsafe static permission getPermission(fastCSharp.net.tcp.commandServer.socket client, string fileName)
        {
#if NotFastCSharpCode
            fastCSharp.log.Error.Throw(fastCSharp.log.exceptionType.NotFastCSharpCode);
#else
            string userName = ((user)client.ClientUserInfo).Name;
            fixed(char *pathFixed = fileName)
            {
                char directorySeparatorChar = Path.DirectorySeparatorChar;

                for (char *read = pathFixed + fileName.Length; read != pathFixed;)
                {
                    if (*--read == directorySeparatorChar)
                    {
                        permission permission = permission.Table.Cache.Get(new permission.primaryKey {
                            UserName = userName, Path = new string(pathFixed, 0, (int)(read - pathFixed) + 1)
                        });
                        if (permission != null)
                        {
                            return(permission);
                        }
                    }
                }
            }
#endif
            return(null);
        }
Beispiel #3
0
        /// <summary>
        /// 路径单选
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void checkPath(object sender, ItemCheckEventArgs e)
        {
            permission value = (permission)pathCheckedListBox.Items[e.Index];

            pathTextBox.Text = value.Path;
            readPermissionCheckBox.Checked  = (value.Type & permissionType.Read) != permissionType.None;
            writePermissionCheckBox.Checked = (value.Type & permissionType.Write) != permissionType.None;
        }
Beispiel #4
0
            /// <summary>
            /// 获取文件上传器
            /// </summary>
            /// <param name="permission">权限路径</param>
            /// <param name="onUpload">上传结束委托</param>
            /// <param name="listName">列表名称</param>
            /// <param name="fileStream">文件流</param>
            /// <returns>文件上传器</returns>
            public static Action GetUpload(permission permission, Func <returnValue <fileState>, bool> onUpload
                                           , listName listName, Stream fileStream, int backupIdentity, bool isTimeVersion)
            {
                uploader uploader = typePool <uploader> .Pop() ?? new uploader();

                uploader.permission     = permission;
                uploader.listName       = listName;
                uploader.onUpload       = onUpload;
                uploader.fileStream     = fileStream;
                uploader.backupIdentity = backupIdentity;
                uploader.isTimeVersion  = isTimeVersion;
                return(uploader.uploadHandle);
            }
Beispiel #5
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);
        }
Beispiel #6
0
        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);
            }
        }
Beispiel #7
0
        /// <summary>
        /// 获取文件权限
        /// </summary>
        /// <param name="client">客户端标识</param>
        /// <param name="fileName">文件名</param>
        /// <returns>文件权限</returns>
        private unsafe static permissionType getPermissionType(fastCSharp.net.tcp.commandServer.socket client, string fileName)
        {
            permission permission = getPermission(client, fileName);

            return(permission != null ? permission.Type : permissionType.None);
        }
Beispiel #8
0
 /// <summary>
 /// 获取文件权限
 /// </summary>
 /// <param name="permission">文件权限</param>
 /// <param name="permissionType">文件权限</param>
 /// <returns>是否成功</returns>
 private unsafe static bool verify(permission permission, permissionType permissionType)
 {
     return(permission != null && (permission.Type & permissionType) != permissionType.None);
 }
Beispiel #9
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);
 }
Beispiel #10
0
        /// <summary>
        /// 修改或者添加用户权限
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void permissionButton_Click(object sender, EventArgs e)
        {
            string userName = userComboBox.Text.Trim();

            if (userName.Length != 0)
            {
                string password = passwordTextBox.Text.Trim();
                user   user     = user.Table.Cache.Get(userName);
                if (user == null)
                {
                    if (password.Length != 0)
                    {
                        user = user.Table.Insert(new user {
                            Name = userName, Password = password
                        });
                    }
                }
                else if (user.Password != password && password.Length != 0)
                {
                    user = user.Table.Update(new user {
                        Name = userName, Password = password
                    }, updatePasswordMember);
                }
                if (user != null)
                {
                    string path = pathTextBox.Text.Trim(), backupPath = backupPathTextBox.Text.Trim();
                    if (path.Length != 0)
                    {
#if NotFastCSharpCode
                        fastCSharp.log.Error.Throw(fastCSharp.log.exceptionType.NotFastCSharpCode);
#else
                        try
                        {
                            path = new DirectoryInfo(path).fullName().toLower();
                            if (backupPath.Length != 0)
                            {
                                backupPath = new DirectoryInfo(backupPath).fullName().toLower();
                            }
                            permissionType permissionType = permissionType.List;
                            if (readPermissionCheckBox.Checked)
                            {
                                permissionType |= permissionType.Read;
                            }
                            if (writePermissionCheckBox.Checked)
                            {
                                permissionType |= permissionType.Write;
                            }
                            permission permissionValue = permission.Table.Cache.Get(new permission.primaryKey {
                                UserName = userName, Path = path
                            });
                            if (permissionValue == null)
                            {
                                permission.Table.Insert(new permission {
                                    UserName = userName, Path = path, BackupPath = backupPath, Type = permissionType
                                });
                                setPath(userName);
                            }
                            else if (permissionValue.Type != permissionType || permissionValue.BackupPath != backupPath)
                            {
                                permission.Table.Update(new permission {
                                    UserName = userName, Path = path, BackupPath = backupPath, Type = permissionType
                                }, updatePermissionMember);
                                setPath(userName);
                            }
                        }
                        catch (Exception error)
                        {
                            MessageBox.Show(error.ToString());
                        }
#endif
                    }
                }
            }
        }