Пример #1
0
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')
Пример #2
0
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
Пример #3
0
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)