示例#1
0
 def write(self, path, data, offset, fh):
     """
     Write
     """
     path = path[1:]
     if path in os.listdir(LOCAL_ROOT):
         path = join(LOCAL_ROOT, path)
         fd = os.open(path, os.O_WRONLY)
         os.lseek(fd, offset, os.SEEK_SET)
         writen = os.write(fd, data)
         os.close(fd)
         return writen
     try:
         host_ip = protocol.lookup(self.bootstrap_ip, path)
         req_payload = {
             'filename': path,
             'offset': offset,
             'bytes': list(data)
         }
         req_pkt = protocol.construct_packet(protocol.Verbs.WRITE_REQ,
                                             protocol.Status.OK,
                                             req_payload)
         header, payload = protocol.sock_send_recv(host_ip, req_pkt)
         logging.debug(header)
         if header.status != protocol.Status.OK.name:
             if header.status == protocol.Status.EACCES.name:
                 raise FuseOSError(errno.EACCES)
             elif header.status == protocol.Status.ENOENT.name:
                 raise FuseOSError(errno.ENOENT)
             else:
                 raise FuseOSError(errno.EIO)
         return payload['cnt']
     except (ProtocolError, KeyError):
         raise FuseOSError(errno.EIO)
示例#2
0
    def unlink(self, path):
        """
        unlink: lookup the location of the file and send request to host. if local, remove.
        """
        path = path[1:]

        # check if local, if local send unlink req and return
        if path in os.listdir(LOCAL_ROOT):
            os.remove(join(LOCAL_ROOT, path))
            return

        req_payload = {'filename': path}
        req_pkt = protocol.construct_packet(protocol.Verbs.UNLINK_REQ,
                                            protocol.Status.OK, req_payload)
        # not local, lookup
        host_ip = protocol.lookup(self.bootstrap_ip, path)
        try:
            header, _ = protocol.sock_send_recv(host_ip, req_pkt)
            if header.status != protocol.Status.OK.name:
                if header.status == protocol.Status.ENOENT.name:
                    raise FuseOSError(errno.ENOENT)
                elif header.status == protocol.Status.EACCES.name:
                    raise FuseOSError(errno.EACCES)
                else:
                    raise FuseOSError(errno.EIO)
        except IOError:
            raise FuseOSError(errno.EIO)
示例#3
0
 def truncate(self, path, length, fh=None):
     """
     Truncate
     """
     path = path[1:]
     if path in os.listdir(LOCAL_ROOT):
         os.truncate(join(LOCAL_ROOT, path), length)
     try:
         host_ip = protocol.lookup(self.bootstrap_ip, path)
     except FileNotFoundError:
         raise FuseOSError(errno.ENOENT)
     try:
         req_payload = {'filename': path, 'len': length}
         req_pkt = protocol.construct_packet(protocol.Verbs.TRUNC_REQ,
                                             protocol.Status.OK,
                                             req_payload)
         header, payload = protocol.sock_send_recv(host_ip, req_pkt)
         if header.status != protocol.Status.OK.name:
             if header.status == protocol.Status.EACCES.name:
                 raise FuseOSError(errno.EACCES)
             elif header.status == protocol.Status.ENOENT.name:
                 raise FuseOSError(errno.ENOENT)
             else:
                 raise FuseOSError(errno.EIO)
     except ProtocolError:
         raise FuseOSError(errno.EIO)
示例#4
0
 def read(self, path, size, offset, fh):
     """
     Read
     """
     path = path[1:]
     if path in os.listdir(LOCAL_ROOT):
         with open(join(LOCAL_ROOT, path), 'rb') as f:
             f.seek(offset)
             return f.read(size)
     try:
         host_ip = protocol.lookup(self.bootstrap_ip, path)
         req_payload = {'filename': path, 'cnt': size, 'offset': offset}
         req_pkt = protocol.construct_packet(protocol.Verbs.READ_REQ,
                                             protocol.Status.OK,
                                             req_payload)
         header, payload = protocol.sock_send_recv(host_ip, req_pkt)
         if header.status != protocol.Status.OK.name:
             if header.status == protocol.Status.ENOENT.name:
                 raise FileNotFoundError
             elif header.status == protocol.Status.EACCES.name:
                 raise PermissionError
         return bytes(payload['bytes'])
     except ProtocolError as ex:
         logging.exception(ex)
         raise FuseOSError(errno.EIO)
     except FileNotFoundError:
         raise FuseOSError(errno.ENOENT)
     except PermissionError:
         raise FuseOSError(errno.EACCES)
示例#5
0
 def open(self, path, flags):
     """
     Open: if it is local, inc FD and return. it not local,
     do a lookup.
     """
     path = path[1:]
     if path in os.listdir():
         self.fd += 1
         return self.fd
     try:
         _host_ip = protocol.lookup(self.bootstrap_ip, path)  # TODO
     except ProtocolError as ex:
         raise FuseOSError(errno.EIO)
     except FileNotFoundError:
         raise FuseOSError(errno.ENOENT)
     else:
         self.fd += 1
         return self.fd
示例#6
0
    def getattr(self, path, fh=None):
        """
        stat/getattr syscall
        """
        if path == '/':
            return self._stat_dict(os.stat(LOCAL_ROOT))
        path = path[1:]
        if path in os.listdir(LOCAL_ROOT):
            stat = os.stat(join(LOCAL_ROOT, path))
            stat_dict = self._stat_dict(stat)
            stat_dict['st_uid'] = os.getuid()
            stat_dict['st_gid'] = os.getgid()
            return stat_dict

        host_ip = protocol.lookup(self.bootstrap_ip, path)

        try:
            pkt = protocol.construct_packet(protocol.Verbs.STAT_REQ,
                                            protocol.Status.OK,
                                            {'filename': path})
            header, payload = protocol.sock_send_recv(host_ip, pkt)
            logging.debug(f'getattr received {header}')
            if header.verb != protocol.Verbs.STAT_RES.name:
                raise FuseOSError(errno.EIO)
            if header.status == protocol.Status.ENOENT.name:
                raise FuseOSError(errno.ENOENT)
            stat = os.stat_result(payload['stat'])
            stat_dict = self._stat_dict(stat)
            stat_dict['st_uid'] = os.getuid()
            stat_dict['st_gid'] = os.getgid()
            return stat_dict
        except FuseOSError:
            raise
        except (ProtocolError, Exception) as ex:
            logging.exception(ex)
            raise FuseOSError(errno.EIO) from ex