Exemplo n.º 1
0
        public static int RenameFile(int packageId, string oldPath, string newPath)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // place log record
            TaskManager.StartTask("FILES", "RENAME_FILE", oldPath, packageId);

            TaskManager.WriteParameter("New name", newPath);

            try
            {
                OS.OperatingSystem os           = GetOS(packageId);
                string             oldFullPath  = GetFullPackagePath(packageId, oldPath);
                string             destFullPath = GetFullPackagePath(packageId, newPath);

                os.MoveFile(oldFullPath, destFullPath);

                return(0);
            }
            catch (Exception ex)
            {
                //Log and return a generic error rather than throwing an exception
                TaskManager.WriteError(ex);
                return(BusinessErrorCodes.ERROR_FILE_GENERIC_LOGGED);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }
Exemplo n.º 2
0
        public static int MoveFiles(int packageId, string[] files, string destFolder)
        {
            // check account
            int accountCheck = SecurityContext.CheckAccount(DemandAccount.NotDemo);

            if (accountCheck < 0)
            {
                return(accountCheck);
            }

            // check dest folder exists
            if (!DirectoryExists(packageId, destFolder))
            {
                return(BusinessErrorCodes.ERROR_FILE_DEST_FOLDER_NONEXISTENT);
            }

            // place log record
            TaskManager.StartTask("FILES", "MOVE_FILES", packageId);

            TaskManager.WriteParameter("Destination folder", destFolder);
            if (files != null)
            {
                foreach (string file in files)
                {
                    TaskManager.Write(file);
                }
            }

            try
            {
                OS.OperatingSystem os             = GetOS(packageId);
                string             destFullFolder = GetFullPackagePath(packageId, destFolder);

                for (int i = 0; i < files.Length; i++)
                {
                    string srcFilePath  = GetFullPackagePath(packageId, files[i]);
                    string destFilePath = Path.Combine(destFullFolder,
                                                       srcFilePath.Substring(srcFilePath.LastIndexOf("\\") + 1));
                    if (srcFilePath == destFilePath)
                    {
                        return(BusinessErrorCodes.ERROR_FILE_COPY_TO_SELF);
                    }
                    //Check that we're not trying to copy a folder into its own subfolder
                    else if (destFilePath.StartsWith(srcFilePath + "\\"))
                    {
                        return(BusinessErrorCodes.ERROR_FILE_COPY_TO_OWN_SUBFOLDER);
                    }
                    else if (os.FileExists(destFilePath) || os.DirectoryExists(destFilePath))
                    {
                        return(BusinessErrorCodes.ERROR_FILE_MOVE_PATH_ALREADY_EXISTS);
                    }
                    else
                    {
                        os.MoveFile(srcFilePath, destFilePath);
                    }
                }

                return(0);
            }
            catch (Exception ex)
            {
                //Log and return a generic error rather than throwing an exception
                TaskManager.WriteError(ex);
                return(BusinessErrorCodes.ERROR_FILE_GENERIC_LOGGED);
            }
            finally
            {
                TaskManager.CompleteTask();
            }
        }