//working for relative and absolute public static Response MoveDirectory(string curPath, string newPath) { var response = ResponseFormation.GetResponse(); var directory = FileTree.GetDirectory(curPath); if (directory == null) { response.Message = $"Directory {curPath} does not exist"; } else { DirectoryNode newParent = FileTree.GetDirectory(newPath); if (newParent == null) { response.Message = $"Directory {newPath} does not exist"; } else { DirectoryNode curParent = FileTree.GetDirectory(PathHelpers.GetDirFromPath(curPath)); FileTree.RemoveDirectory(curParent, directory); FileTree.AddDirectory(newParent, directory); response.IsSuccess = true; response.Message = "Directory moved successfully"; FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); } } return(response); }
//working for relative and absolute public static Response RemoveDirectory(string path) { var response = ResponseFormation.GetResponse(); var directory = FileTree.GetDirectory(path); if (directory == null) { response.Message = "Directory does not exist"; } else { DirectoryNode parent = FileTree.GetDirectory(PathHelpers.GetDirFromPath(path)); FileTree.RemoveDirectory(parent, directory); //if local delete files from disk RemoveFilesByDirectory(directory); response.IsSuccess = true; response.Message = "Directory removed successfully"; FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); //else forward } return(response); }
//working for relative and absolute public static Response CreateDirectory(string path) { var response = ResponseFormation.GetResponse(); var directory = FileTree.GetDirectory(path); if (directory != null) { response.Message = "Directory already exists"; } else { directory = new DirectoryNode(PathHelpers.GetFilenameFromPath(path)); var parentDirStr = PathHelpers.GetDirFromPath(path); var parent = FileTree.GetDirectory(parentDirStr); if (parent == null) { response.Message = $"Directory {parentDirStr} does not exist"; } else { FileTree.AddDirectory(parent, directory); response.IsSuccess = true; response.Message = "Directory created successfully"; FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); } } return(response); }
//working for relative and absolute public static Response RemoveFile(string path) { var response = ResponseFormation.GetResponse(); string dirPath = PathHelpers.GetDirFromPath(path); string filename = PathHelpers.GetFilenameFromPath(path); var directory = FileTree.GetDirectory(dirPath); if (directory == null) { response.Message = string.Format(ResponseMessages.DirectoryNotFound, dirPath); } else { var file = FileTree.GetFile(directory, filename); if (file == null) { response.Message = string.Format(ResponseMessages.FileNotFound, filename, dirPath); } else if (file.IPAddresses.Exists(x => x.Equals(State.LocalEndPoint.ToString())) && File.Exists(Path.Combine(State.GetRootDirectory().FullName, file.ImplicitName))) { RemoveFileFromDisk(file.ImplicitName); FileTree.RemoveFile(directory, file); response.IsSuccess = true; response.Message = "Succesfully Deleted"; FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); if (file.IPAddresses.Count > 1) { var remoteIps = file.IPAddresses.Where(x => !x.Equals(State.LocalEndPoint.ToString())).ToList(); if (remoteIps != null) { foreach (var ip in remoteIps) { var socket = ServerList.GetServerList() .FirstOrDefault(x => x.IPPort.Equals(ip)).Socket; var request = new Request { Type = "FileService", Method = "RemoveFileFromDisk", Parameters = new object[] { file.ImplicitName } }; ServerCommunication.Send(socket, request.SerializeToByteArray()); } } } } else { // send request to remote server response.IsSuccess = true; response.Message = "Forwarded"; response.Command = Command.forwarded; } } return(response); }
//working for relative and absolute public static Response MoveFile(string curPath, string newPath) { var response = ResponseFormation.GetResponse(); string curDirPath = PathHelpers.GetDirFromPath(curPath); string curFilename = PathHelpers.GetFilenameFromPath(curPath); string newDirPath = PathHelpers.GetDirFromPath(newPath); string newFilename = PathHelpers.GetFilenameFromPath(newPath); var curDir = FileTree.GetDirectory(curDirPath); if (curDir == null) { response.Message = string.Format(ResponseMessages.DirectoryNotFound, curDirPath); } else { var file = FileTree.GetFile(curDir, curFilename); if (file == null) { response.Message = string.Format(ResponseMessages.FileNotFound, curFilename, curDirPath); } else { var newDir = FileTree.GetDirectory(newDirPath); if (newDir == null) { response.Message = string.Format(ResponseMessages.DirectoryNotFound, newDirPath); } else { FileTree.RemoveFile(curDir, file); FileTree.AddFile(newDir, file); file.Name = newFilename; response.IsSuccess = true; response.Message = "Successfully Moved"; FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); } } } return(response); }
//working for relative and absolute private static Response CreateFile(DirectoryNode directory, string filename, bool createLocally) { var response = ResponseFormation.GetResponse(); //query filetree to find out which server has least storage occupied var ipSpacePairs = FileTree.GetIPSpacePairs(); string ipPort = null; if (ipSpacePairs.Count == 0) { ipPort = State.LocalEndPoint.ToString(); } else { ipPort = ipSpacePairs.OrderBy(x => x.Value).First().Key; } if (createLocally || State.LocalEndPoint.ToString().Equals(ipPort)) { FileNode file = new FileNode(filename, FileTree.GetNewImplicitName(), 0); File.WriteAllText(Path.Combine(State.GetRootDirectory().FullName, file.ImplicitName), ""); file.IPAddresses.Add(State.LocalEndPoint.ToString()); FileTree.AddFile(directory, file); FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); //FileTree.AddInLocalFiles(file); response.Message = "Successfully Created"; } else { //forward response.Command = Command.forwarded; response.Message = ipPort; } response.IsSuccess = true; return(response); }
//working for relative and absolute public static Response UpdateFile(string path, string data) { var response = ResponseFormation.GetResponse(); string dirPath = PathHelpers.GetDirFromPath(path); string filename = PathHelpers.GetFilenameFromPath(path); var directory = FileTree.GetDirectory(dirPath); if (directory == null) { response.Message = string.Format(ResponseMessages.DirectoryNotFound, dirPath); } else { var file = FileTree.GetFile(directory, filename); var str = State.LocalEndPoint.ToString(); if (file == null) { response.Message = string.Format(ResponseMessages.FileNotFound, filename, dirPath); } else if (file.IPAddresses.Exists(x => x.Equals(State.LocalEndPoint.ToString()))) { File.WriteAllText(Path.Combine(State.GetRootDirectory().FullName, file.ImplicitName), data); response.IsSuccess = true; response.Message = "Successfully updated"; file.Size = data.Length; var servers = ServerList.GetServers(); bool isReplicated = false; if (servers.Count > 0) { Request request = new Request(); if (file.IPAddresses.Count < 2) { request.Type = "FileService"; request.Method = "Replicate"; request.Parameters = new object[] { data, file }; int index = new Random().Next(servers.Count); file.IPAddresses.Add(ServerList.GetServerList() .FirstOrDefault(x => x.Socket.Equals(servers[index])).IPPort); ServerCommunication.Send(servers[index], request.SerializeToByteArray()); FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); isReplicated = true; } else { request.Command = Command.updateFile; request.Method = "UpdateFile"; request.Type = "FileService"; request.Parameters = new object[] { path, data }; foreach (var ip in file.IPAddresses) { var s = ServerList.GetServerList().FirstOrDefault(x => x.IPPort.Equals(ip)); if (s != null) { ServerCommunication.Send(s.Socket, request.SerializeToByteArray()); } } } //response.Command = Command.wait; } if (!isReplicated) { FileTreeService.UpdateFileTree(FileTree.GetRootDirectory().SerializeToByteArray()); } } else { string ip = null; foreach (var ipPort in ServerList.GetIPPorts()) { if (file.IPAddresses.Exists(x => x.Equals(ipPort))) { ip = ipPort; break; } } if (ip == null) { response.Message = string.Format(ResponseMessages.FileNotFound, filename, dirPath); } else { // send request to remote server response.Message = ip; response.IsSuccess = true; response.Command = Command.forwarded; } } } return(response); }