/// <summary> /// Copy file or directory to other directory in file system /// </summary> /// <param name="context">context for command</param> public Response Copy(CommandContext context) { if (!hasTwoParameters(context.Args)) { return(Response.Failed("1 аргумент - путь к директории или файлу, 2 - путь к директории")); } string sourcePath = context.Args[0]; string destPath = context.Args[1]; lock (_syncCommand) { IDirectory destDirectory = FindDirectory(destPath, context.User.CurrentDirectory); IDirectory sourceDirectory = FindDirectory(sourcePath, context.User.CurrentDirectory); if (destDirectory == null) { return(Response.Failed("Не найдена директория, куда нужно скопировать объект")); } if (sourceDirectory == null) { IFile sourceFile = FindFile(sourcePath, context.User.CurrentDirectory); if (sourceFile == null) { return(Response.Failed("Не найден объект для перемещения")); } IFile copiedFile = (IFile)sourceFile.DeepCopy(); copiedFile.LockedUsers.Clear(); try { destDirectory.AddFile(copiedFile); } catch (IOException) { return(Response.Failed("Файл или директория с таким именем уже существует")); } } else { if (sourceDirectory.Parent == null) { return(Response.Failed("Нельзя скопировать корневую директорию")); } IDirectory copiedDirectory = (IDirectory)sourceDirectory.DeepCopy(); copiedDirectory.DeepForEach(d => d.GetDirectories(), d => d.GetFiles().ForEach(f => f.LockedUsers.Clear())); try { destDirectory.AddDirectory(copiedDirectory); } catch (IOException) { return(Response.Failed("Файл или директория с таким именем уже существует")); } } return(Response.Success(true)); } }
public static void DeepCopy(this IDirectory system, DirectoryPath from, DirectoryPath to, bool overwrite = false) { if (!system.Exists(to.Path)) { system.CreateDirectory(to.Path); } foreach (var file in system.GetFiles(from)) { var fi = system.FileSystem.FileInfo.FromFileName(file); var rhs = Path.Combine(to.Path, Path.GetFileName(file)); fi.CopyTo(rhs, overwrite); } foreach (var dir in system.GetDirectories(from)) { system.DeepCopy(dir, Path.Combine(to.Path, Path.GetFileName(dir)), overwrite: overwrite); } }