def get_etag(path_or_fd):
    """
    FIXME: It would be great to have a translator that returns the md5sum() of
    the file as an xattr that can be simply fetched.

    Since we don't have that we should yield after each chunk read and
    computed so that we don't consume the worker thread.
    """
    etag = ''
    if isinstance(path_or_fd, int):
        # We are given a file descriptor, so this is an invocation from the
        # DiskFile.open() method.
        fd = path_or_fd
        dup_fd = do_dup(fd)
        try:
            etag = _read_for_etag(dup_fd)
            do_lseek(fd, 0, os.SEEK_SET)
        finally:
            do_close(dup_fd)
    else:
        # We are given a path to the object when the DiskDir.list_objects_iter
        # method invokes us.
        path = path_or_fd
        fd = do_open(path, os.O_RDONLY)
        try:
            etag = _read_for_etag(fd)
        finally:
            do_close(fd)

    return etag
Exemple #2
0
 def app_iter_range(self, start, stop):
     """Returns an iterator over the data file for range (start, stop)"""
     if start or start == 0:
         do_lseek(self._fd, start, os.SEEK_SET)
     if stop is not None:
         length = stop - start
     else:
         length = None
     try:
         for chunk in self:
             if length is not None:
                 length -= len(chunk)
                 if length < 0:
                     # Chop off the extra:
                     yield chunk[:length]
                     break
             yield chunk
     finally:
         if not self._suppress_file_closing:
             self.close()