2 import errno, sys, stat
3 from bup import options, git, metadata, vfs
4 from bup.helpers import *
7 bup restore [-C outdir] </branch/revision/path/to/dir ...>
9 C,outdir= change to given outdir before extracting files
10 numeric-ids restore numeric IDs (user, group, etc.) rather than names
11 v,verbose increase log output (can be used more than once)
12 q,quiet don't show progress meter
34 def print_info(n, fullname):
35 if stat.S_ISDIR(n.mode):
36 verbose1('%s/' % fullname)
37 elif stat.S_ISLNK(n.mode):
38 verbose2('%s@ -> %s' % (fullname, n.readlink()))
43 def create_path(n, fullname, meta):
45 meta.create_path(fullname)
47 # These fallbacks are important -- meta could be null if, for
48 # example, save created a "fake" item, i.e. a new strip/graft
49 # path element, etc. You can find cases like that by
50 # searching for "Metadata()".
52 if stat.S_ISDIR(n.mode):
54 elif stat.S_ISLNK(n.mode):
55 os.symlink(n.readlink(), fullname)
57 # Track a list of (restore_path, vfs_path, meta) triples for each path
58 # we've written for a given hardlink_target. This allows us to handle
59 # the case where we restore a set of hardlinks out of order (with
60 # respect to the original save call(s)) -- i.e. when we don't restore
61 # the hardlink_target path first. This data also allows us to attempt
62 # to handle other situations like hardlink sets that change on disk
63 # during a save, or between index and save.
66 def hardlink_compatible(target_path, target_vfs_path, target_meta,
69 if not os.path.exists(target_path):
71 target_node = top.lresolve(target_vfs_path)
72 if src_node.mode != target_node.mode \
73 or src_node.atime != target_node.atime \
74 or src_node.mtime != target_node.mtime \
75 or src_node.ctime != target_node.ctime \
76 or src_node.hash != target_node.hash:
78 if not src_meta.same_file(target_meta):
83 def hardlink_if_possible(fullname, node, meta):
84 """Find a suitable hardlink target, link to it, and return true,
85 otherwise return false."""
86 # Expect the caller to handle restoring the metadata if
87 # hardlinking isn't possible.
88 global targets_written
89 target = meta.hardlink_target
90 target_versions = targets_written.get(target)
92 # Check every path in the set that we've written so far for a match.
93 for (target_path, target_vfs_path, target_meta) in target_versions:
94 if hardlink_compatible(target_path, target_vfs_path, target_meta,
97 os.link(target_path, fullname)
100 if e.errno != errno.EXDEV:
104 targets_written[target] = target_versions
105 full_vfs_path = node.fullname()
106 target_versions.append((fullname, full_vfs_path, meta))
110 def write_file_content(fullname, n):
111 outf = open(fullname, 'wb')
113 for b in chunkyreader(n.open()):
120 # Very similar to do_node(), except that this function doesn't
121 # create a path for n's destination directory (and so ignores
122 # n.fullname). It assumes the destination is '.', and restores
123 # n's metadata and content there.
124 global total_restored, opt
127 # Directory metadata is the first entry in any .bupm file in
128 # the directory. Get it.
129 mfile = n.metadata_file() # VFS file -- cannot close().
131 meta_stream = mfile.open()
132 meta = metadata.Metadata.read(meta_stream)
135 plog('Restoring: %d\r' % total_restored)
138 # Don't get metadata if this is a dir -- handled in sub do_node().
139 if meta_stream and not stat.S_ISDIR(sub.mode):
140 m = metadata.Metadata.read(meta_stream)
143 meta.apply_to_path('.', restore_numeric_ids = opt.numeric_ids)
149 def do_node(top, n, meta=None):
150 # Create n.fullname(), relative to the current directory, and
151 # restore all of its metadata, when available. The meta argument
152 # will be None for dirs, or when there is no .bupm (i.e. no
154 global total_restored, opt
157 fullname = n.fullname(stop_at=top)
158 # If this is a directory, its metadata is the first entry in
159 # any .bupm file inside the directory. Get it.
160 if(stat.S_ISDIR(n.mode)):
161 mfile = n.metadata_file() # VFS file -- cannot close().
163 meta_stream = mfile.open()
164 meta = metadata.Metadata.read(meta_stream)
165 print_info(n, fullname)
167 created_hardlink = False
168 if meta and meta.hardlink_target:
169 created_hardlink = hardlink_if_possible(fullname, n, meta)
171 if not created_hardlink:
172 create_path(n, fullname, meta)
174 if stat.S_ISREG(meta.mode):
175 write_file_content(fullname, n)
176 elif stat.S_ISREG(n.mode):
177 write_file_content(fullname, n)
180 plog('Restoring: %d\r' % total_restored)
183 # Don't get metadata if this is a dir -- handled in sub do_node().
184 if meta_stream and not stat.S_ISDIR(sub.mode):
185 m = metadata.Metadata.read(meta_stream)
187 if meta and not created_hardlink:
188 meta.apply_to_path(fullname, restore_numeric_ids = opt.numeric_ids)
195 o = options.Options(optspec)
196 (opt, flags, extra) = o.parse(sys.argv[1:])
198 git.check_repo_or_die()
199 top = vfs.RefList(None)
202 o.fatal('must specify at least one filename to restore')
210 path,name = os.path.split(d)
213 except vfs.NodeError, e:
216 isdir = stat.S_ISDIR(n.mode)
217 if not name or name == '.':
218 # Source is /foo/what/ever/ or /foo/what/ever/. -- extract
219 # what/ever/* to the current directory, and if name == '.'
220 # (i.e. /foo/what/ever/.), then also restore what/ever's
221 # metadata to the current directory.
223 add_error('%r: not a directory' % d)
231 # Source is /foo/what/ever -- extract ./ever to cwd.
232 if isinstance(n, vfs.FakeSymlink):
233 # Source is actually /foo/what, i.e. a top-level commit
234 # like /foo/latest, which is a symlink to ../.commit/SHA.
235 # So dereference it, and restore ../.commit/SHA/. to
237 target = n.dereference()
245 progress('Restoring: %d, done.\n' % total_restored)
248 log('WARNING: %d errors encountered while restoring.\n' % len(saved_errors))