Esempio n. 1
0
        public override void move_directory(string directoryPath, string newDirectoryPath)
        {
            if (string.IsNullOrWhiteSpace(directoryPath) || string.IsNullOrWhiteSpace(newDirectoryPath))
            {
                throw new ApplicationException("You must provide a directory to move from or to.");
            }

            //Linux/MacOS do not have a SystemDrive environment variable, instead, everything is under "/"
            var systemDrive = Platform.get_platform() == PlatformType.Windows ? Environment.GetEnvironmentVariable("SystemDrive") : "/";

            if (combine_paths(directoryPath, "").is_equal_to(combine_paths(systemDrive, "")))
            {
                throw new ApplicationException("Cannot move or delete the root of the system drive");
            }

            try
            {
                this.Log().Debug(ChocolateyLoggers.Verbose, "Moving '{0}'{1} to '{2}'".format_with(directoryPath, Environment.NewLine, newDirectoryPath));
                allow_retries(
                    () =>
                {
                    try
                    {
                        Directory.Move(directoryPath, newDirectoryPath);
                    }
                    catch (IOException)
                    {
                        System_IO.Directory.Move(directoryPath, newDirectoryPath);
                    }
                });
            }
            catch (Exception ex)
            {
                this.Log().Warn(ChocolateyLoggers.Verbose, "Move failed with message:{0} {1}{0} Attempting backup move method.".format_with(
                                    Environment.NewLine, InstallContext.NormalizeMessage(ex.Message)));

                create_directory_if_not_exists(newDirectoryPath, ignoreError: true);
                foreach (var file in get_files(directoryPath, "*.*", SearchOption.AllDirectories).or_empty_list_if_null())
                {
                    var destinationFile = file.Replace(directoryPath, newDirectoryPath);
                    if (file_exists(destinationFile))
                    {
                        delete_file(destinationFile);
                    }

                    create_directory_if_not_exists(get_directory_name(destinationFile), ignoreError: true);
                    this.Log().Debug(ChocolateyLoggers.Verbose, "Moving '{0}'{1} to '{2}'".format_with(file, Environment.NewLine, destinationFile));
                    move_file(file, destinationFile);
                }

                Thread.Sleep(1000); // let the moving files finish up
                delete_directory_if_exists(directoryPath, recursive: true);
            }

            Thread.Sleep(2000); // sleep for enough time to allow the folder to be cleared
        }
Esempio n. 2
0
        public override void delete_directory(string directoryPath, bool recursive, bool overrideAttributes, bool isSilent)
        {
            if (string.IsNullOrWhiteSpace(directoryPath))
            {
                throw new ApplicationException("You must provide a directory to delete.");
            }

            //Linux / MacOS do not have a SystemDrive environment variable, instead, everything is under "/"
            var systemDrive = Platform.get_platform() == PlatformType.Windows ? Environment.GetEnvironmentVariable("SystemDrive") : "/";

            if (combine_paths(directoryPath, "").is_equal_to(combine_paths(systemDrive, "")))
            {
                throw new ApplicationException("Cannot move or delete the root of the system drive");
            }

            if (overrideAttributes)
            {
                foreach (var file in get_files(directoryPath, "*.*", SearchOption.AllDirectories))
                {
                    var filePath = get_full_path(file);
                    var fileInfo = get_file_info_for(filePath);

                    if (is_system_file(fileInfo))
                    {
                        ensure_file_attribute_removed(filePath, FileAttributes.System);
                    }
                    if (is_readonly_file(fileInfo))
                    {
                        ensure_file_attribute_removed(filePath, FileAttributes.ReadOnly);
                    }
                    if (is_hidden_file(fileInfo))
                    {
                        ensure_file_attribute_removed(filePath, FileAttributes.Hidden);
                    }
                }
            }

            if (!isSilent)
            {
                this.Log().Debug(ChocolateyLoggers.Verbose, () => "Attempting to delete directory \"{0}\".".format_with(get_full_path(directoryPath)));
            }
            allow_retries(
                () =>
            {
                try
                {
                    Directory.Delete(directoryPath, recursive);
                }
                catch (IOException)
                {
                    System_IO.Directory.Delete(directoryPath, recursive);
                }
            }, isSilent: isSilent);
        }