Back to index

obnam  1.1
Classes | Functions | Variables
obnamlib.metadata Namespace Reference

Classes

class  Metadata

Functions

def _cached_getpwuid
def _cached_getgrgid
def get_xattrs_as_blob
def set_xattrs_from_blob
def read_metadata
def set_metadata
def encode_metadata
def decode_metadata

Variables

tuple metadata_verify_fields
tuple metadata_fields
dictionary _uid_to_username = {}
dictionary _gid_to_groupname = {}
tuple metadata_format

Function Documentation

def obnamlib.metadata._cached_getgrgid (   gid) [private]

Definition at line 123 of file metadata.py.

00123 
00124 def _cached_getgrgid(gid): # pragma: no cover
00125     if gid not in _gid_to_groupname:
00126         _gid_to_groupname[gid] = grp.getgrgid(gid)
00127     return _gid_to_groupname[gid]
00128 

def obnamlib.metadata._cached_getpwuid (   uid) [private]

Definition at line 117 of file metadata.py.

00117 
00118 def _cached_getpwuid(uid): # pragma: no cover
00119     if uid not in _uid_to_username:
00120         _uid_to_username[uid] = pwd.getpwuid(uid)
00121     return _uid_to_username[uid]
    

Definition at line 273 of file metadata.py.

00273 
00274 def decode_metadata(encoded):
00275 
00276     items = metadata_format.unpack_from(encoded)
00277     flags = items[0]
00278     pos = [1, metadata_format.size]
00279     metadata = obnamlib.Metadata()
00280     
00281     def is_present(field):
00282         i = obnamlib.metadata_fields.index(field)
00283         return (flags & (1 << i)) != 0
00284 
00285     def decode(field, num_items, inc_offset, getvalue):
00286         if is_present(field):
00287             value = getvalue(pos[0], pos[1])
00288             setattr(metadata, field, value)
00289             if inc_offset:
00290                 pos[1] += len(value)
00291         pos[0] += num_items
00292 
00293     def decode_integer(field):
00294         decode(field, 1, False, lambda i, o: items[i])
00295 
00296     def decode_string(field):
00297         decode(field, 1, True, lambda i, o: encoded[o:o + items[i]])
00298     
00299     decode_integer('st_mode')
00300     decode_integer('st_mtime_sec')
00301     decode_integer('st_mtime_nsec')
00302     decode_integer('st_atime_sec')
00303     decode_integer('st_atime_nsec')
00304     decode_integer('st_nlink')
00305     decode_integer('st_size')
00306     decode_integer('st_uid')
00307     decode_integer('st_gid')
00308     decode_integer('st_dev')
00309     decode_integer('st_ino')
00310     decode_integer('st_blocks')
00311     decode_string('groupname')
00312     decode_string('username')
00313     decode_string('target')
00314     decode_string('md5')
00315     decode_string('xattr')
00316     
00317     return metadata
00318 

Definition at line 241 of file metadata.py.

00241 
00242 def encode_metadata(metadata):
00243     flags = 0
00244     for i, name in enumerate(obnamlib.metadata_fields):
00245         if getattr(metadata, name) is not None:
00246             flags |= (1 << i)
00247 
00248     packed = metadata_format.pack(flags,
00249                                   metadata.st_mode or 0,
00250                                   metadata.st_mtime_sec or 0,
00251                                   metadata.st_mtime_nsec or 0,
00252                                   metadata.st_atime_sec or 0,
00253                                   metadata.st_atime_nsec or 0,
00254                                   metadata.st_nlink or 0,
00255                                   metadata.st_size or 0,
00256                                   metadata.st_uid or 0,
00257                                   metadata.st_gid or 0,
00258                                   metadata.st_dev or 0,
00259                                   metadata.st_ino or 0,
00260                                   metadata.st_blocks or 0,
00261                                   len(metadata.groupname or ''),
00262                                   len(metadata.username or ''),
00263                                   len(metadata.target or ''),
00264                                   len(metadata.md5 or ''),
00265                                   len(metadata.xattr or ''))
00266     return (packed + 
00267              (metadata.groupname or '') +
00268              (metadata.username or '') +
00269              (metadata.target or '') +
00270              (metadata.md5 or '') +
00271              (metadata.xattr or ''))
00272 

def obnamlib.metadata.get_xattrs_as_blob (   fs,
  filename 
)

Definition at line 129 of file metadata.py.

00129 
00130 def get_xattrs_as_blob(fs, filename): # pragma: no cover
00131     names = fs.llistxattr(filename)
00132     if not names:
00133         return None
00134     values = [fs.lgetxattr(filename, name) for name in names]
00135 
00136     name_blob = ''.join('%s\0' % name for name in names)
00137 
00138     lengths = [len(v) for v in values]
00139     fmt = '!' + 'Q' * len(values)
00140     value_blob = struct.pack(fmt, *lengths) + ''.join(values)
00141 
00142     return ('%s%s%s' % 
00143             (struct.pack('!Q', len(name_blob)),
00144              name_blob,
00145              value_blob))
00146 

Here is the caller graph for this function:

def obnamlib.metadata.read_metadata (   fs,
  filename,
  st = None,
  getpwuid = None,
  getgrgid = None 
)
Return object detailing metadata for a filesystem entry.

Definition at line 165 of file metadata.py.

00165 
00166 def read_metadata(fs, filename, st=None, getpwuid=None, getgrgid=None):
00167     '''Return object detailing metadata for a filesystem entry.'''
00168     metadata = Metadata()
00169     stat_result = st or fs.lstat(filename)
00170     for field in metadata_fields:
00171         if field.startswith('st_') and hasattr(stat_result, field):
00172             setattr(metadata, field, getattr(stat_result, field))
00173 
00174     if stat.S_ISLNK(stat_result.st_mode):
00175         metadata.target = fs.readlink(filename)
00176     else:
00177         metadata.target = ''
00178 
00179     getgrgid = getgrgid or _cached_getgrgid
00180     try:
00181         metadata.groupname = getgrgid(metadata.st_gid)[0]
00182     except KeyError:
00183         metadata.groupname = None
00184 
00185     getpwuid = getpwuid or _cached_getpwuid
00186     try:
00187         metadata.username = getpwuid(metadata.st_uid)[0]
00188     except KeyError:
00189         metadata.username = None
00190 
00191     metadata.xattr = get_xattrs_as_blob(fs, filename)
00192 
00193     return metadata
00194 

Here is the call graph for this function:

def obnamlib.metadata.set_metadata (   fs,
  filename,
  metadata,
  getuid = None 
)
Set metadata for a filesystem entry.

We only set metadata that can sensibly be set: st_atime, st_mode,
st_mtime. We also attempt to set ownership (st_gid, st_uid), but
only if we're running as root. We ignore the username, groupname
fields: we assume the caller will change st_uid, st_gid accordingly
if they want to mess with things. This makes the user take care
of error situations and looking up user preferences.

Definition at line 195 of file metadata.py.

00195 
00196 def set_metadata(fs, filename, metadata, getuid=None):
00197     '''Set metadata for a filesystem entry.
00198 
00199     We only set metadata that can sensibly be set: st_atime, st_mode,
00200     st_mtime. We also attempt to set ownership (st_gid, st_uid), but
00201     only if we're running as root. We ignore the username, groupname
00202     fields: we assume the caller will change st_uid, st_gid accordingly
00203     if they want to mess with things. This makes the user take care
00204     of error situations and looking up user preferences.
00205     
00206     '''
00207 
00208     if stat.S_ISLNK(metadata.st_mode):
00209         fs.symlink(metadata.target, filename)
00210     else:
00211         fs.chmod(filename, metadata.st_mode)
00212 
00213     if metadata.xattr: # pragma: no cover
00214         set_xattrs_from_blob(fs, filename, metadata.xattr)
00215 
00216     fs.lutimes(filename, metadata.st_atime_sec, metadata.st_atime_nsec, 
00217                metadata.st_mtime_sec, metadata.st_mtime_nsec)
00218 
00219     getuid = getuid or os.getuid
00220     if getuid() == 0:
00221         fs.lchown(filename, metadata.st_uid, metadata.st_gid)
00222     
    

Here is the call graph for this function:

def obnamlib.metadata.set_xattrs_from_blob (   fs,
  filename,
  blob 
)

Definition at line 147 of file metadata.py.

00147 
00148 def set_xattrs_from_blob(fs, filename, blob): # pragma: no cover
00149     sizesize = struct.calcsize('!Q')
00150     name_blob_size = struct.unpack('!Q', blob[:sizesize])[0]
00151     name_blob = blob[sizesize : sizesize + name_blob_size]
00152     value_blob = blob[sizesize + name_blob_size : ]
00153 
00154     names = [s for s in name_blob.split('\0')[:-1]]
00155     fmt = '!' + 'Q' * len(names)
00156     lengths_size = sizesize * len(names)
00157     lengths = struct.unpack(fmt, value_blob[:lengths_size])
00158     
00159     pos = lengths_size
00160     for i, name in enumerate(names):
00161         value = value_blob[pos:pos + lengths[i]]
00162         pos += lengths[i]
00163         fs.lsetxattr(filename, name, value)
00164 

Here is the caller graph for this function:


Variable Documentation

Definition at line 122 of file metadata.py.

Definition at line 116 of file metadata.py.

Initial value:
00001 metadata_verify_fields+(
00002     'st_blocks', 'st_dev', 'st_gid', 'st_ino',  'st_atime_sec', 
00003     'st_atime_nsec', 'md5',
00004 )

Definition at line 31 of file metadata.py.

Initial value:
00001 struct.Struct('!Q' +  # flags
00002                                 'Q' +   # st_mode
00003                                 'qQ' +  # st_mtime_sec and _nsec
00004                                 'qQ' +  # st_atime_sec and _nsec
00005                                 'Q' +   # st_nlink
00006                                 'Q' +   # st_size
00007                                 'Q' +   # st_uid
00008                                 'Q' +   # st_gid
00009                                 'Q' +   # st_dev
00010                                 'Q' +   # st_ino
00011                                 'Q' +   # st_blocks
00012                                 'Q' +   # len of groupname
00013                                 'Q' +   # len of username
00014                                 'Q' +   # len of symlink target
00015                                 'Q' +   # len of md5
00016                                 'Q' +   # len of xattr
00017                                 '')

Definition at line 223 of file metadata.py.

Initial value:
00001 (
00002     'st_mode', 'st_mtime_sec', 'st_mtime_nsec', 
00003     'st_nlink', 'st_size', 'st_uid', 'groupname', 'username', 'target',
00004     'xattr',
00005 )

Definition at line 26 of file metadata.py.