/usr/share/pyshared/obnamlib/vfs_local.py is in obnam 0.24.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 | # Copyright (C) 2008 Lars Wirzenius <liw@liw.fi>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
import errno
import fcntl
import grp
import logging
import math
import os
import pwd
import tempfile
import tracing
import obnamlib
class LocalFSFile(file):
def read(self, amount=-1):
offset = self.tell()
data = file.read(self, amount)
if data:
fd = self.fileno()
obnamlib._obnam.fadvise_dontneed(fd, offset, len(data))
return data
def write(self, data):
offset = self.tell()
file.write(self, data)
fd = self.fileno()
obnamlib._obnam.fadvise_dontneed(fd, offset, len(data))
class LocalFS(obnamlib.VirtualFileSystem):
"""A VFS implementation for local filesystems."""
chunk_size = 1024 * 1024
def __init__(self, baseurl, create=False):
tracing.trace('baseurl=%s', baseurl)
tracing.trace('create=%s', create)
obnamlib.VirtualFileSystem.__init__(self, baseurl)
self.reinit(baseurl, create=create)
def reinit(self, baseurl, create=False):
# We fake chdir so that it doesn't mess with the caller's
# perception of current working directory. This also benefits
# unit tests. To do this, we store the baseurl as the cwd.
tracing.trace('baseurl=%s', baseurl)
tracing.trace('create=%s', create)
self.cwd = os.path.abspath(baseurl)
if not self.isdir('.'):
if create:
tracing.trace('creating %s', baseurl)
os.mkdir(baseurl)
else:
raise OSError(errno.ENOENT, self.cwd)
def getcwd(self):
return self.cwd
def chdir(self, pathname):
tracing.trace('LocalFS(%s).chdir(%s)', self.baseurl, pathname)
newcwd = os.path.abspath(self.join(pathname))
if not os.path.isdir(newcwd):
raise OSError('%s is not a directory' % newcwd)
self.cwd = newcwd
def lock(self, lockname):
tracing.trace('lockname=%s', lockname)
try:
self.write_file(lockname, "")
except OSError, e:
if e.errno == errno.EEXIST:
raise obnamlib.Error("Lock %s already exists" % lockname)
else:
raise
def unlock(self, lockname):
tracing.trace('lockname=%s', lockname)
if self.exists(lockname):
self.remove(lockname)
def join(self, pathname):
return os.path.join(self.cwd, pathname)
def remove(self, pathname):
tracing.trace('remove %s', pathname)
os.remove(self.join(pathname))
def rename(self, old, new):
tracing.trace('rename %s %s', old, new)
os.rename(self.join(old), self.join(new))
def lstat(self, pathname):
(ret, dev, ino, mode, nlink, uid, gid, rdev, size, blksize, blocks,
atime_sec, atime_nsec, mtime_sec, mtime_nsec,
ctime_sec, ctime_nsec) = obnamlib._obnam.lstat(self.join(pathname))
if ret != 0:
raise OSError((ret, os.strerror(ret), pathname))
return obnamlib.Metadata(
st_dev=dev,
st_ino=ino,
st_mode=mode,
st_nlink=nlink,
st_uid=uid,
st_gid=gid,
st_rdev=rdev,
st_size=size,
st_blksize=blksize,
st_blocks=blocks,
st_atime_sec=atime_sec,
st_atime_nsec=atime_nsec,
st_mtime_sec=mtime_sec,
st_mtime_nsec=mtime_nsec,
st_ctime_sec=ctime_sec,
st_ctime_nsec=ctime_nsec
)
def get_username(self, uid):
return pwd.getpwuid(uid)[0]
def get_groupname(self, gid):
return grp.getgrgid(gid)[0]
def llistxattr(self, filename): # pragma: no cover
ret = obnamlib._obnam.llistxattr(self.join(filename))
if type(ret) is int:
raise OSError((ret, os.strerror(ret), filename))
return [s for s in ret.split('\0') if s]
def lgetxattr(self, filename, attrname): # pragma: no cover
ret = obnamlib._obnam.lgetxattr(self.join(filename), attrname)
if type(ret) is int:
raise OSError((ret, os.strerror(ret), filename))
return ret
def lsetxattr(self, filename, attrname, attrvalue): # pragma: no cover
ret = obnamlib._obnam.lsetxattr(self.join(filename),
attrname, attrvalue)
if ret != 0:
raise OSError((ret, os.strerror(ret), filename))
def lchown(self, pathname, uid, gid): # pragma: no cover
tracing.trace('lchown %s %d %d', pathname, uid, gid)
os.lchown(self.join(pathname), uid, gid)
def chmod(self, pathname, mode):
tracing.trace('chmod %s %o', pathname, mode)
os.chmod(self.join(pathname), mode)
def lutimes(self, pathname, atime_sec, atime_nsec, mtime_sec, mtime_nsec):
assert atime_sec is not None
assert atime_nsec is not None
assert mtime_sec is not None
assert mtime_nsec is not None
ret = obnamlib._obnam.lutimes(self.join(pathname),
atime_sec, int(atime_nsec / 1000),
mtime_sec, int(mtime_nsec / 1000))
if ret != 0:
raise OSError(ret, os.strerror(ret), pathname)
def link(self, existing, new):
tracing.trace('existing=%s', existing)
tracing.trace('new=%s', new)
os.link(self.join(existing), self.join(new))
def readlink(self, pathname):
return os.readlink(self.join(pathname))
def symlink(self, existing, new):
tracing.trace('existing=%s', existing)
tracing.trace('new=%s', new)
os.symlink(existing, self.join(new))
def open(self, pathname, mode):
tracing.trace('pathname=%s', pathname)
tracing.trace('mode=%s', mode)
f = LocalFSFile(self.join(pathname), mode)
try:
flags = fcntl.fcntl(f.fileno(), fcntl.F_GETFL)
flags |= os.O_NOATIME
fcntl.fcntl(f.fileno(), fcntl.F_SETFL, flags)
except IOError, e: # pragma: no cover
tracing.trace('fcntl F_SETFL failed: %s', repr(e))
return f # ignore any problems setting flags
return f
def exists(self, pathname):
return os.path.exists(self.join(pathname))
def isdir(self, pathname):
return os.path.isdir(self.join(pathname))
def mknod(self, pathname, mode):
tracing.trace('pathmame=%s', pathname)
tracing.trace('mode=%o', mode)
os.mknod(self.join(pathname), mode)
def mkdir(self, pathname):
tracing.trace('mkdir %s', pathname)
os.mkdir(self.join(pathname))
def makedirs(self, pathname):
tracing.trace('makedirs %s', pathname)
os.makedirs(self.join(pathname))
def rmdir(self, pathname):
tracing.trace('rmdir %s', pathname)
os.rmdir(self.join(pathname))
def cat(self, pathname):
pathname = self.join(pathname)
f = self.open(pathname, 'rb')
chunks = []
while True:
chunk = f.read(self.chunk_size)
if not chunk:
break
chunks.append(chunk)
self.bytes_read += len(chunk)
f.close()
data = ''.join(chunks)
return data
def write_file(self, pathname, contents):
tracing.trace('write_file %s', pathname)
tempname = self._write_to_tempfile(pathname, contents)
path = self.join(pathname)
try:
os.link(tempname, path)
except OSError, e: # pragma: no cover
# sshfs does not implement link(2), so we fudge it here.
# FIXME: This has race conditions and should be made atomic.
if e.errno != errno.ENOSYS:
os.remove(tempname)
raise
if os.path.exists(path):
raise OSError(errno.EEXIST, os.strerror(errno.EEXIST), path)
os.rename(tempname, path)
os.remove(tempname)
def overwrite_file(self, pathname, contents, make_backup=True):
tracing.trace('overwrite_file %s', pathname)
tempname = self._write_to_tempfile(pathname, contents)
path = self.join(pathname)
# Rename existing to have a .bak suffix. If _that_ file already
# exists, remove that.
bak = path + ".bak"
try:
os.remove(bak)
except OSError:
pass
try:
os.link(path, bak)
except OSError:
pass
os.rename(tempname, path)
if not make_backup:
try:
os.remove(bak)
except OSError:
pass
def _write_to_tempfile(self, pathname, contents):
path = self.join(pathname)
dirname = os.path.dirname(path)
if not os.path.exists(dirname):
os.makedirs(dirname)
fd, tempname = tempfile.mkstemp(dir=dirname)
os.close(fd)
f = self.open(tempname, 'wb')
pos = 0
while pos < len(contents):
chunk = contents[pos:pos+self.chunk_size]
f.write(chunk)
pos += len(chunk)
self.bytes_written += len(chunk)
f.close()
return tempname
def listdir(self, dirname):
return os.listdir(self.join(dirname))
def listdir2(self, dirname):
return [(name, self.lstat(os.path.join(dirname, name)))
for name in os.listdir(self.join(dirname))]
|