]> arthur.barton.de Git - bup.git/blob - lib/bup/metadata.py
Merge branch 'maint'
[bup.git] / lib / bup / metadata.py
1 """Metadata read/write support for bup."""
2
3 # Copyright (C) 2010 Rob Browning
4 #
5 # This code is covered under the terms of the GNU Library General
6 # Public License as described in the bup LICENSE file.
7
8 import errno, os, sys, stat, pwd, grp, struct, xattr, posix1e, re
9
10 from cStringIO import StringIO
11 from bup import vint
12 from bup.drecurse import recursive_dirlist
13 from bup.helpers import add_error, mkdirp, log
14 from bup.xstat import utime, lutime, lstat, FSTime
15 import bup._helpers as _helpers
16
17 if _helpers.get_linux_file_attr:
18     from bup._helpers import get_linux_file_attr, set_linux_file_attr
19
20 # WARNING: the metadata encoding is *not* stable yet.  Caveat emptor!
21
22 # Q: Consider hardlink support?
23 # Q: Is it OK to store raw linux attr (chattr) flags?
24 # Q: Can anything other than S_ISREG(x) or S_ISDIR(x) support posix1e ACLs?
25 # Q: Is the application of posix1e has_extended() correct?
26 # Q: Is one global --numeric-ids argument sufficient?
27 # Q: Do nfsv4 acls trump posix1e acls? (seems likely)
28 # Q: Add support for crtime -- ntfs, and (only internally?) ext*?
29
30 # FIXME: Fix relative/abs path detection/stripping wrt other platforms.
31 # FIXME: Add nfsv4 acl handling - see nfs4-acl-tools.
32 # FIXME: Consider other entries mentioned in stat(2) (S_IFDOOR, etc.).
33 # FIXME: Consider pack('vvvvsss', ...) optimization.
34 # FIXME: Consider caching users/groups.
35
36 ## FS notes:
37 #
38 # osx (varies between hfs and hfs+):
39 #   type - regular dir char block fifo socket ...
40 #   perms - rwxrwxrwxsgt
41 #   times - ctime atime mtime
42 #   uid
43 #   gid
44 #   hard-link-info (hfs+ only)
45 #   link-target
46 #   device-major/minor
47 #   attributes-osx see chflags
48 #   content-type
49 #   content-creator
50 #   forks
51 #
52 # ntfs
53 #   type - regular dir ...
54 #   times - creation, modification, posix change, access
55 #   hard-link-info
56 #   link-target
57 #   attributes - see attrib
58 #   ACLs
59 #   forks (alternate data streams)
60 #   crtime?
61 #
62 # fat
63 #   type - regular dir ...
64 #   perms - rwxrwxrwx (maybe - see wikipedia)
65 #   times - creation, modification, access
66 #   attributes - see attrib
67
68 verbose = 0
69
70 _have_lchmod = hasattr(os, 'lchmod')
71
72
73 def _clean_up_path_for_archive(p):
74     # Not the most efficient approach.
75     result = p
76
77     # Take everything after any '/../'.
78     pos = result.rfind('/../')
79     if(pos != -1):
80         result = result[result.rfind('/../') + 4:]
81
82     # Take everything after any remaining '../'.
83     if result.startswith("../"):
84         result = result[3:]
85
86     # Remove any '/./' sequences.
87     pos = result.find('/./')
88     while pos != -1:
89         result = result[0:pos] + '/' + result[pos + 3:]
90         pos = result.find('/./')
91
92     # Remove any leading '/'s.
93     result = result.lstrip('/')
94
95     # Replace '//' with '/' everywhere.
96     pos = result.find('//')
97     while pos != -1:
98         result = result[0:pos] + '/' + result[pos + 2:]
99         pos = result.find('//')
100
101     # Take everything after any remaining './'.
102     if result.startswith('./'):
103         result = result[2:]
104
105     # Take everything before any remaining '/.'.
106     if result.endswith('/.'):
107         result = result[:-2]
108
109     if result == '' or result.endswith('/..'):
110         result = '.'
111
112     return result
113
114
115 def _risky_path(p):
116     if p.startswith('/'):
117         return True
118     if p.find('/../') != -1:
119         return True
120     if p.startswith('../'):
121         return True
122     if p.endswith('/..'):
123         return True
124     return False
125
126
127 def _clean_up_extract_path(p):
128     result = p.lstrip('/')
129     if result == '':
130         return '.'
131     elif _risky_path(result):
132         return None
133     else:
134         return result
135
136
137 # These tags are currently conceptually private to Metadata, and they
138 # must be unique, and must *never* be changed.
139 _rec_tag_end = 0
140 _rec_tag_path = 1
141 _rec_tag_common = 2           # times, owner, group, type, perms, etc.
142 _rec_tag_symlink_target = 3
143 _rec_tag_posix1e_acl = 4      # getfacl(1), setfacl(1), etc.
144 _rec_tag_nfsv4_acl = 5        # intended to supplant posix1e acls?
145 _rec_tag_linux_attr = 6       # lsattr(1) chattr(1)
146 _rec_tag_linux_xattr = 7      # getfattr(1) setfattr(1)
147
148
149 class ApplyError(Exception):
150     # Thrown when unable to apply any given bit of metadata to a path.
151     pass
152
153
154 class Metadata:
155     # Metadata is stored as a sequence of tagged binary records.  Each
156     # record will have some subset of add, encode, load, create, and
157     # apply methods, i.e. _add_foo...
158
159     ## Common records
160
161     # Timestamps are (sec, ns), relative to 1970-01-01 00:00:00, ns
162     # must be non-negative and < 10**9.
163
164     def _add_common(self, path, st):
165         self.mode = st.st_mode
166         self.uid = st.st_uid
167         self.gid = st.st_gid
168         self.rdev = st.st_rdev
169         self.atime = st.st_atime
170         self.mtime = st.st_mtime
171         self.ctime = st.st_ctime
172         self.owner = self.group = ''
173         try:
174             self.owner = pwd.getpwuid(st.st_uid)[0]
175         except KeyError, e:
176             add_error("no user name for id %s '%s'" % (st.st_gid, path))
177         try:
178             self.group = grp.getgrgid(st.st_gid)[0]
179         except KeyError, e:
180             add_error("no group name for id %s '%s'" % (st.st_gid, path))
181
182     def _encode_common(self):
183         atime = self.atime.to_timespec()
184         mtime = self.mtime.to_timespec()
185         ctime = self.ctime.to_timespec()
186         result = vint.pack('VVsVsVvVvVvV',
187                            self.mode,
188                            self.uid,
189                            self.owner,
190                            self.gid,
191                            self.group,
192                            self.rdev,
193                            atime[0],
194                            atime[1],
195                            mtime[0],
196                            mtime[1],
197                            ctime[0],
198                            ctime[1])
199         return result
200
201     def _load_common_rec(self, port):
202         data = vint.read_bvec(port)
203         (self.mode,
204          self.uid,
205          self.owner,
206          self.gid,
207          self.group,
208          self.rdev,
209          self.atime,
210          atime_ns,
211          self.mtime,
212          mtime_ns,
213          self.ctime,
214          ctime_ns) = vint.unpack('VVsVsVvVvVvV', data)
215         self.atime = FSTime.from_timespec((self.atime, atime_ns))
216         self.mtime = FSTime.from_timespec((self.mtime, mtime_ns))
217         self.ctime = FSTime.from_timespec((self.ctime, ctime_ns))
218
219     def _create_via_common_rec(self, path, create_symlinks=True):
220         # If the path already exists and is a dir, try rmdir.
221         # If the path already exists and is anything else, try unlink.
222         st = None
223         try:
224             st = lstat(path)
225         except IOError, e:
226             if e.errno != errno.ENOENT:
227                 raise
228         if st:
229             if stat.S_ISDIR(st.st_mode):
230                 try:
231                     os.rmdir(path)
232                 except OSError, e:
233                     if e.errno == errno.ENOTEMPTY:
234                         msg = 'refusing to overwrite non-empty dir' + path
235                         raise Exception(msg)
236                     raise
237             else:
238                 os.unlink(path)
239
240         if stat.S_ISREG(self.mode):
241             os.mknod(path, 0600 | stat.S_IFREG)
242         elif stat.S_ISDIR(self.mode):
243             os.mkdir(path, 0700)
244         elif stat.S_ISCHR(self.mode):
245             os.mknod(path, 0600 | stat.S_IFCHR, self.rdev)
246         elif stat.S_ISBLK(self.mode):
247             os.mknod(path, 0600 | stat.S_IFBLK, self.rdev)
248         elif stat.S_ISFIFO(self.mode):
249             os.mknod(path, 0600 | stat.S_IFIFO)
250         elif stat.S_ISLNK(self.mode):
251             if(self.symlink_target and create_symlinks):
252                 os.symlink(self.symlink_target, path)
253         # FIXME: S_ISDOOR, S_IFMPB, S_IFCMP, S_IFNWK, ... see stat(2).
254         # Otherwise, do nothing.
255
256     def _apply_common_rec(self, path, restore_numeric_ids=False):
257         # FIXME: S_ISDOOR, S_IFMPB, S_IFCMP, S_IFNWK, ... see stat(2).
258         # EACCES errors at this stage are fatal for the current path.
259         if stat.S_ISLNK(self.mode):
260             try:
261                 lutime(path, (self.atime, self.mtime))
262             except IOError, e:
263                 if e.errno == errno.EACCES:
264                     raise ApplyError('lutime: %s' % e)
265                 else:
266                     raise
267         else:
268             try:
269                 utime(path, (self.atime, self.mtime))
270             except IOError, e:
271                 if e.errno == errno.EACCES:
272                     raise ApplyError('utime: %s' % e)
273                 else:
274                     raise
275
276         # Don't try to restore owner unless we're root, and even
277         # if asked, don't try to restore the owner or group if
278         # it doesn't exist in the system db.
279         uid = self.uid
280         gid = self.gid
281         if not restore_numeric_ids:
282             if not self.owner:
283                 uid = -1
284                 add_error('ignoring missing owner for "%s"\n' % path)
285             else:
286                 if os.geteuid() != 0:
287                     uid = -1 # Not root; assume we can't change owner.
288                 else:
289                     try:
290                         uid = pwd.getpwnam(self.owner)[2]
291                     except KeyError:
292                         uid = -1
293                         fmt = 'ignoring unknown owner %s for "%s"\n'
294                         add_error(fmt % (self.owner, path))
295             if not self.group:
296                 gid = -1
297                 add_error('ignoring missing group for "%s"\n' % path)
298             else:
299                 try:
300                     gid = grp.getgrnam(self.group)[2]
301                 except KeyError:
302                     gid = -1
303                     add_error('ignoring unknown group %s for "%s"\n'
304                               % (self.group, path))
305
306         try:
307             os.lchown(path, uid, gid)
308         except OSError, e:
309             if e.errno == errno.EPERM:
310                 add_error('lchown: %s' %  e)
311             else:
312                 raise
313
314         if _have_lchmod:
315             os.lchmod(path, stat.S_IMODE(self.mode))
316         elif not stat.S_ISLNK(self.mode):
317             os.chmod(path, stat.S_IMODE(self.mode))
318
319
320     ## Path records
321
322     def _encode_path(self):
323         if self.path:
324             return vint.pack('s', self.path)
325         else:
326             return None
327
328     def _load_path_rec(self, port):
329         self.path = vint.unpack('s', vint.read_bvec(port))[0]
330
331
332     ## Symlink targets
333
334     def _add_symlink_target(self, path, st):
335         try:
336             if(stat.S_ISLNK(st.st_mode)):
337                 self.symlink_target = os.readlink(path)
338         except OSError, e:
339             add_error('readlink: %s', e)
340
341     def _encode_symlink_target(self):
342         return self.symlink_target
343
344     def _load_symlink_target_rec(self, port):
345         self.symlink_target = vint.read_bvec(port)
346
347
348     ## POSIX1e ACL records
349
350     # Recorded as a list:
351     #   [txt_id_acl, num_id_acl]
352     # or, if a directory:
353     #   [txt_id_acl, num_id_acl, txt_id_default_acl, num_id_default_acl]
354     # The numeric/text distinction only matters when reading/restoring
355     # a stored record.
356     def _add_posix1e_acl(self, path, st):
357         if not stat.S_ISLNK(st.st_mode):
358             try:
359                 if posix1e.has_extended(path):
360                     acl = posix1e.ACL(file=path)
361                     self.posix1e_acl = [acl, acl] # txt and num are the same
362                     if stat.S_ISDIR(st.st_mode):
363                         acl = posix1e.ACL(filedef=path)
364                         self.posix1e_acl.extend([acl, acl])
365             except EnvironmentError, e:
366                 if e.errno != errno.EOPNOTSUPP:
367                     raise
368
369     def _encode_posix1e_acl(self):
370         # Encode as two strings (w/default ACL string possibly empty).
371         if self.posix1e_acl:
372             acls = self.posix1e_acl
373             txt_flags = posix1e.TEXT_ABBREVIATE
374             num_flags = posix1e.TEXT_ABBREVIATE | posix1e.TEXT_NUMERIC_IDS
375             acl_reps = [acls[0].to_any_text('', '\n', txt_flags),
376                         acls[1].to_any_text('', '\n', num_flags)]
377             if(len(acls) < 3):
378                 acl_reps += ['', '']
379             else:
380                 acl_reps.append(acls[2].to_any_text('', '\n', txt_flags))
381                 acl_reps.append(acls[3].to_any_text('', '\n', num_flags))
382             return vint.pack('ssss',
383                              acl_reps[0], acl_reps[1], acl_reps[2], acl_reps[3])
384         else:
385             return None
386
387     def _load_posix1e_acl_rec(self, port):
388         data = vint.read_bvec(port)
389         acl_reps = vint.unpack('ssss', data)
390         if(acl_reps[2] == ''):
391             acl_reps = acl_reps[:2]
392         self.posix1e_acl = [posix1e.ACL(text=x) for x in acl_reps]
393
394     def _apply_posix1e_acl_rec(self, path, restore_numeric_ids=False):
395         if(self.posix1e_acl):
396             acls = self.posix1e_acl
397             if(len(acls) > 2):
398                 if restore_numeric_ids:
399                     acls[3].applyto(path, posix1e.ACL_TYPE_DEFAULT)
400                 else:
401                     acls[2].applyto(path, posix1e.ACL_TYPE_DEFAULT)
402             if restore_numeric_ids:
403                 acls[1].applyto(path, posix1e.ACL_TYPE_ACCESS)
404             else:
405                 acls[0].applyto(path, posix1e.ACL_TYPE_ACCESS)
406
407
408     ## Linux attributes (lsattr(1), chattr(1))
409
410     def _add_linux_attr(self, path, st):
411         if stat.S_ISREG(st.st_mode) or stat.S_ISDIR(st.st_mode):
412             try:
413                 attr = get_linux_file_attr(path)
414                 if(attr != 0):
415                     self.linux_attr = attr
416             except IOError, e:
417                 if e.errno == errno.EACCES:
418                     add_error('read Linux attr: %s' % e)
419                 elif e.errno == errno.ENOTTY: # Inappropriate ioctl for device.
420                     add_error('read Linux attr: %s' % e)
421                 else:
422                     raise
423
424     def _encode_linux_attr(self):
425         if self.linux_attr:
426             return vint.pack('V', self.linux_attr)
427         else:
428             return None
429
430     def _load_linux_attr_rec(self, port):
431         data = vint.read_bvec(port)
432         self.linux_attr = vint.unpack('V', data)[0]
433
434     def _apply_linux_attr_rec(self, path, restore_numeric_ids=False):
435         if(self.linux_attr):
436             set_linux_file_attr(path, self.linux_attr)
437
438
439     ## Linux extended attributes (getfattr(1), setfattr(1))
440
441     def _add_linux_xattr(self, path, st):
442         try:
443             self.linux_xattr = xattr.get_all(path, nofollow=True)
444         except EnvironmentError, e:
445             if e.errno != errno.EOPNOTSUPP:
446                 raise
447
448     def _encode_linux_xattr(self):
449         if self.linux_xattr:
450             result = vint.pack('V', len(self.linux_xattr))
451             for name, value in self.linux_xattr:
452                 result += vint.pack('ss', name, value)
453             return result
454         else:
455             return None
456
457     def _load_linux_xattr_rec(self, file):
458         data = vint.read_bvec(file)
459         memfile = StringIO(data)
460         result = []
461         for i in range(vint.read_vuint(memfile)):
462             key = vint.read_bvec(memfile)
463             value = vint.read_bvec(memfile)
464             result.append((key, value))
465         self.linux_xattr = result
466
467     def _apply_linux_xattr_rec(self, path, restore_numeric_ids=False):
468         existing_xattrs = set(xattr.list(path, nofollow=True))
469         if(self.linux_xattr):
470             for k, v in self.linux_xattr:
471                 if k not in existing_xattrs \
472                         or v != xattr.get(path, k, nofollow=True):
473                     try:
474                         xattr.set(path, k, v, nofollow=True)
475                     except IOError, e:
476                         if e.errno == errno.EPERM:
477                             raise ApplyError('xattr.set: %s' % e)
478                         else:
479                             raise
480                 existing_xattrs -= frozenset([k])
481             for k in existing_xattrs:
482                 try:
483                     xattr.remove(path, k, nofollow=True)
484                 except IOError, e:
485                     if e.errno == errno.EPERM:
486                         raise ApplyError('xattr.remove: %s' % e)
487                     else:
488                         raise
489
490     def __init__(self):
491         # optional members
492         self.path = None
493         self.symlink_target = None
494         self.linux_attr = None
495         self.linux_xattr = None
496         self.posix1e_acl = None
497         self.posix1e_acl_default = None
498
499     def write(self, port, include_path=True):
500         records = [(_rec_tag_path, self._encode_path())] if include_path else []
501         records.extend([(_rec_tag_common, self._encode_common()),
502                         (_rec_tag_symlink_target, self._encode_symlink_target()),
503                         (_rec_tag_posix1e_acl, self._encode_posix1e_acl()),
504                         (_rec_tag_linux_attr, self._encode_linux_attr()),
505                         (_rec_tag_linux_xattr, self._encode_linux_xattr())])
506         for tag, data in records:
507             if data:
508                 vint.write_vuint(port, tag)
509                 vint.write_bvec(port, data)
510         vint.write_vuint(port, _rec_tag_end)
511
512     @staticmethod
513     def read(port):
514         # This method should either: return a valid Metadata object;
515         # throw EOFError if there was nothing at all to read; throw an
516         # Exception if a valid object could not be read completely.
517         tag = vint.read_vuint(port)
518         try: # From here on, EOF is an error.
519             result = Metadata()
520             while(True): # only exit is error (exception) or _rec_tag_end
521                 if tag == _rec_tag_path:
522                     result._load_path_rec(port)
523                 elif tag == _rec_tag_common:
524                     result._load_common_rec(port)
525                 elif tag == _rec_tag_symlink_target:
526                     result._load_symlink_target_rec(port)
527                 elif tag == _rec_tag_posix1e_acl:
528                     result._load_posix1e_acl_rec(port)
529                 elif tag ==_rec_tag_nfsv4_acl:
530                     result._load_nfsv4_acl_rec(port)
531                 elif tag == _rec_tag_linux_attr:
532                     result._load_linux_attr_rec(port)
533                 elif tag == _rec_tag_linux_xattr:
534                     result._load_linux_xattr_rec(port)
535                 elif tag == _rec_tag_end:
536                     return result
537                 else: # unknown record
538                     vint.skip_bvec(port)
539                 tag = vint.read_vuint(port)
540         except EOFError:
541             raise Exception("EOF while reading Metadata")
542
543     def isdir(self):
544         return stat.S_ISDIR(self.mode)
545
546     def create_path(self, path, create_symlinks=True):
547         self._create_via_common_rec(path, create_symlinks=create_symlinks)
548
549     def apply_to_path(self, path=None, restore_numeric_ids=False):
550         # apply metadata to path -- file must exist
551         if not path:
552             path = self.path
553         if not path:
554             raise Exception('Metadata.apply_to_path() called with no path');
555         num_ids = restore_numeric_ids
556         try:
557             self._apply_common_rec(path, restore_numeric_ids=num_ids)
558             self._apply_posix1e_acl_rec(path, restore_numeric_ids=num_ids)
559             self._apply_linux_attr_rec(path, restore_numeric_ids=num_ids)
560             self._apply_linux_xattr_rec(path, restore_numeric_ids=num_ids)
561         except ApplyError, e:
562             add_error(e)
563
564
565 def from_path(path, statinfo=None, archive_path=None, save_symlinks=True):
566     result = Metadata()
567     result.path = archive_path
568     st = statinfo if statinfo else lstat(path)
569     result._add_common(path, st)
570     if(save_symlinks):
571         result._add_symlink_target(path, st)
572     result._add_posix1e_acl(path, st)
573     result._add_linux_attr(path, st)
574     result._add_linux_xattr(path, st)
575     return result
576
577
578 def save_tree(output_file, paths,
579               recurse=False,
580               write_paths=True,
581               save_symlinks=True,
582               xdev=False):
583
584     # Issue top-level rewrite warnings.
585     for path in paths:
586         safe_path = _clean_up_path_for_archive(path)
587         if(safe_path != path):
588             log('archiving "%s" as "%s"\n' % (path, safe_path))
589
590     start_dir = os.getcwd()
591     try:
592         for (p, st) in recursive_dirlist(paths, xdev=xdev):
593             dirlist_dir = os.getcwd()
594             os.chdir(start_dir)
595             safe_path = _clean_up_path_for_archive(p)
596             m = from_path(p, statinfo=st, archive_path=safe_path,
597                           save_symlinks=save_symlinks)
598             if verbose:
599                 print >> sys.stderr, m.path
600             m.write(output_file, include_path=write_paths)
601             os.chdir(dirlist_dir)
602     finally:
603         os.chdir(start_dir)
604
605
606 def _set_up_path(meta, create_symlinks=True):
607     # Allow directories to exist as a special case -- might have
608     # been created by an earlier longer path.
609     if meta.isdir():
610         mkdirp(meta.path)
611     else:
612         parent = os.path.dirname(meta.path)
613         if parent:
614             mkdirp(parent)
615             meta.create_path(meta.path, create_symlinks=create_symlinks)
616
617
618 class _ArchiveIterator:
619     def next(self):
620         try:
621             return Metadata.read(self._file)
622         except EOFError:
623             raise StopIteration()
624
625     def __iter__(self):
626         return self
627
628     def __init__(self, file):
629         self._file = file
630
631
632 def display_archive(file):
633     for meta in _ArchiveIterator(file):
634         if verbose:
635             print meta.path # FIXME
636         else:
637             print meta.path
638
639
640 def start_extract(file, create_symlinks=True):
641     for meta in _ArchiveIterator(file):
642         if verbose:
643             print >> sys.stderr, meta.path
644         xpath = _clean_up_extract_path(meta.path)
645         if not xpath:
646             add_error(Exception('skipping risky path "%s"' % meta.path))
647         else:
648             meta.path = xpath
649             _set_up_path(meta, create_symlinks=create_symlinks)
650
651
652 def finish_extract(file, restore_numeric_ids=False):
653     all_dirs = []
654     for meta in _ArchiveIterator(file):
655         xpath = _clean_up_extract_path(meta.path)
656         if not xpath:
657             add_error(Exception('skipping risky path "%s"' % dir.path))
658         else:
659             if os.path.isdir(meta.path):
660                 all_dirs.append(meta)
661             else:
662                 if verbose:
663                     print >> sys.stderr, meta.path
664                 meta.apply_to_path(path=xpath,
665                                    restore_numeric_ids=restore_numeric_ids)
666     all_dirs.sort(key = lambda x : len(x.path), reverse=True)
667     for dir in all_dirs:
668         # Don't need to check xpath -- won't be in all_dirs if not OK.
669         xpath = _clean_up_extract_path(dir.path)
670         if verbose:
671             print >> sys.stderr, dir.path
672         dir.apply_to_path(path=xpath, restore_numeric_ids=restore_numeric_ids)
673
674
675 def extract(file, restore_numeric_ids=False, create_symlinks=True):
676     # For now, just store all the directories and handle them last,
677     # longest first.
678     all_dirs = []
679     for meta in _ArchiveIterator(file):
680         xpath = _clean_up_extract_path(meta.path)
681         if not xpath:
682             add_error(Exception('skipping risky path "%s"' % meta.path))
683         else:
684             meta.path = xpath
685             if verbose:
686                 print >> sys.stderr, '+', meta.path
687             _set_up_path(meta, create_symlinks=create_symlinks)
688             if os.path.isdir(meta.path):
689                 all_dirs.append(meta)
690             else:
691                 if verbose:
692                     print >> sys.stderr, '=', meta.path
693                 meta.apply_to_path(restore_numeric_ids=restore_numeric_ids)
694     all_dirs.sort(key = lambda x : len(x.path), reverse=True)
695     for dir in all_dirs:
696         # Don't need to check xpath -- won't be in all_dirs if not OK.
697         xpath = _clean_up_extract_path(dir.path)
698         if verbose:
699             print >> sys.stderr, '=', xpath
700         # Shouldn't have to check for risky paths here (omitted above).
701         dir.apply_to_path(path=dir.path,
702                           restore_numeric_ids=restore_numeric_ids)