def flush(sock: socket) -> bool: Logger.addHost(*sock.getpeername(), 'attempts to flush file system') FS.Flush() space = FS.GetFreeSpace() Logger.addHost(*sock.getpeername(), 'has flushed file system') SendULong(sock, space) return False
def replicate(sock: socket): host = sock.getpeername()[0] paths = RecvULong(sock) mainlog = f'Starting to replicate {paths} file(s) from storage %s' % host Logger.add(mainlog) # Start replication for _ in range(paths): # Receive path path = RecvStr(sock) log = f'Starting to replicate \'{path}\' from storage %s' % host Logger.add(log) # Prepare path validpath = FS.CreateFile(path) # Receive all bytes bts = RecvBytes(sock) # Get number of chunks chunks = ceil(len(bts) / FileChunkSize) for i in range(chunks): # Write chunk fpath = ospath.join(validpath, str(i)) with open(fpath, 'wb') as f: f.write(bts[i * FileChunkSize: (i + 1) * FileChunkSize]) # Finish file replication Logger.add(log + ' - success') SendSignal(sock) Logger.add(mainlog + ' - success')
def remove(sock: socket) -> bool: path = RecvStr(sock) Logger.addHost(*sock.getpeername(), 'attempts to remove \'%s\'' % path) FS.Remove(path) Logger.addHost(*sock.getpeername(), 'has removed \'%s\'' % path) SendResponse(sock, SUCCESS) return False
def download(sock: socket, path: str): Logger.addHost(*sock.getpeername(), 'attempts to download \'%s\'' % path) # Check if exists if not FS.Exists(path): SendResponse(sock, 'No such file on storage server') return SendResponse(sock, SUCCESS) # Get number of chunks chunks = ceil(FS.GetSize(path) / FileChunkSize) # Get valid path validpath = FS.GetValidPath(path) # Get all bytes bts = [] for i in range(chunks): fpath = ospath.join(validpath, str(i)) with open(fpath, 'rb') as f: bts.append(f.read()) # Send everything SendBytes(sock, b''.join(bts))
def move(sock: socket) -> bool: what = RecvStr(sock) to = RecvStr(sock) t = what, to Logger.addHost(*sock.getpeername(), 'attempts to move \'%s\' to \'%s\'' % t) FS.Move(what, to) Logger.addHost(*sock.getpeername(), 'has moved \'%s\' to \'%s\'' % t) SendResponse(sock, SUCCESS) return False
def rename(sock: socket) -> bool: path = RecvStr(sock) name = RecvStr(sock) t = path, name Logger.addHost(*sock.getpeername(), 'attempts to rename \'%s\' to \'%s\'' % t) FS.Rename(path, name) Logger.addHost(*sock.getpeername(), 'has renamed \'%s\' to \'%s\'' % t) SendResponse(sock, SUCCESS) return False
def mkfile(sock: socket) -> bool: path = RecvStr(sock) Logger.addHost(*sock.getpeername(), 'attempts to create an empty file \'%s\'' % path) fpath = ospath.join(FS.CreateFile(path), '0') # zero chunk with open(fpath, 'wb'): pass Logger.addHost(*sock.getpeername(), 'has created \'%s\'' % path) SendResponse(sock, SUCCESS) return False
def info(sock: socket) -> bool: path = RecvStr(sock) Logger.addHost(*sock.getpeername(), 'attempts to get stats of \'%s\'' % path) stats = FS.GetStats(path) stats = [str(st) for st in stats] stats = InfoSeparator.join(stats) Logger.addHost(*sock.getpeername(), 'has got stats of \'%s\'' % path) SendStr(sock, stats) return False
def upload(sock: socket, path: str): Logger.addHost(*sock.getpeername(), 'attempts to upload a file to \'%s\'' % path) validpath = FS.CreateFile(path) # Get file content bts = RecvBytes(sock) # Get number of chunks chunks = ceil(len(bts) / FileChunkSize) for i in range(chunks): # Write chunk fpath = ospath.join(validpath, str(i)) with open(fpath, 'wb') as f: f.write(bts[i * FileChunkSize:(i + 1) * FileChunkSize]) # Success Logger.addHost(*sock.getpeername(), 'has uploaded file \'%s\'' % path) SendResponse(sock, SUCCESS)
def locate(sock: socket) -> bool: SendStr(sock, PublicIP) SendULong(sock, FS.GetFreeSpace()) return False