Merge branch '8008-package-testing' refs #8008
[arvados.git] / services / fuse / arvados_fuse / fusedir.py
1 import logging
2 import re
3 import time
4 import llfuse
5 import arvados
6 import apiclient
7 import functools
8 import threading
9 from apiclient import errors as apiclient_errors
10 import errno
11 import time
12
13 from fusefile import StringFile, ObjectFile, FuncToJSONFile, FuseArvadosFile
14 from fresh import FreshBase, convertTime, use_counter, check_update
15
16 import arvados.collection
17 from arvados.util import portable_data_hash_pattern, uuid_pattern, collection_uuid_pattern, group_uuid_pattern, user_uuid_pattern, link_uuid_pattern
18
19 _logger = logging.getLogger('arvados.arvados_fuse')
20
21
22 # Match any character which FUSE or Linux cannot accommodate as part
23 # of a filename. (If present in a collection filename, they will
24 # appear as underscores in the fuse mount.)
25 _disallowed_filename_characters = re.compile('[\x00/]')
26
27 # '.' and '..' are not reachable if API server is newer than #6277
28 def sanitize_filename(dirty):
29     """Replace disallowed filename characters with harmless "_"."""
30     if dirty is None:
31         return None
32     elif dirty == '':
33         return '_'
34     elif dirty == '.':
35         return '_'
36     elif dirty == '..':
37         return '__'
38     else:
39         return _disallowed_filename_characters.sub('_', dirty)
40
41
42 class Directory(FreshBase):
43     """Generic directory object, backed by a dict.
44
45     Consists of a set of entries with the key representing the filename
46     and the value referencing a File or Directory object.
47     """
48
49     def __init__(self, parent_inode, inodes):
50         """parent_inode is the integer inode number"""
51
52         super(Directory, self).__init__()
53
54         self.inode = None
55         if not isinstance(parent_inode, int):
56             raise Exception("parent_inode should be an int")
57         self.parent_inode = parent_inode
58         self.inodes = inodes
59         self._entries = {}
60         self._mtime = time.time()
61
62     #  Overriden by subclasses to implement logic to update the entries dict
63     #  when the directory is stale
64     @use_counter
65     def update(self):
66         pass
67
68     # Only used when computing the size of the disk footprint of the directory
69     # (stub)
70     def size(self):
71         return 0
72
73     def persisted(self):
74         return False
75
76     def checkupdate(self):
77         if self.stale():
78             try:
79                 self.update()
80             except apiclient.errors.HttpError as e:
81                 _logger.warn(e)
82
83     @use_counter
84     @check_update
85     def __getitem__(self, item):
86         return self._entries[item]
87
88     @use_counter
89     @check_update
90     def items(self):
91         return list(self._entries.items())
92
93     @use_counter
94     @check_update
95     def __contains__(self, k):
96         return k in self._entries
97
98     @use_counter
99     @check_update
100     def __len__(self):
101         return len(self._entries)
102
103     def fresh(self):
104         self.inodes.touch(self)
105         super(Directory, self).fresh()
106
107     def merge(self, items, fn, same, new_entry):
108         """Helper method for updating the contents of the directory.
109
110         Takes a list describing the new contents of the directory, reuse
111         entries that are the same in both the old and new lists, create new
112         entries, and delete old entries missing from the new list.
113
114         :items: iterable with new directory contents
115
116         :fn: function to take an entry in 'items' and return the desired file or
117         directory name, or None if this entry should be skipped
118
119         :same: function to compare an existing entry (a File or Directory
120         object) with an entry in the items list to determine whether to keep
121         the existing entry.
122
123         :new_entry: function to create a new directory entry (File or Directory
124         object) from an entry in the items list.
125
126         """
127
128         oldentries = self._entries
129         self._entries = {}
130         changed = False
131         for i in items:
132             name = sanitize_filename(fn(i))
133             if name:
134                 if name in oldentries and same(oldentries[name], i):
135                     # move existing directory entry over
136                     self._entries[name] = oldentries[name]
137                     del oldentries[name]
138                 else:
139                     _logger.debug("Adding entry '%s' to inode %i", name, self.inode)
140                     # create new directory entry
141                     ent = new_entry(i)
142                     if ent is not None:
143                         self._entries[name] = self.inodes.add_entry(ent)
144                         changed = True
145
146         # delete any other directory entries that were not in found in 'items'
147         for i in oldentries:
148             _logger.debug("Forgetting about entry '%s' on inode %i", i, self.inode)
149             self.inodes.invalidate_entry(self.inode, i.encode(self.inodes.encoding))
150             self.inodes.del_entry(oldentries[i])
151             changed = True
152
153         if changed:
154             self.inodes.invalidate_inode(self.inode)
155             self._mtime = time.time()
156
157         self.fresh()
158
159     def clear(self, force=False):
160         """Delete all entries"""
161
162         if not self.in_use() or force:
163             oldentries = self._entries
164             self._entries = {}
165             for n in oldentries:
166                 if not oldentries[n].clear(force):
167                     self._entries = oldentries
168                     return False
169             for n in oldentries:
170                 self.inodes.invalidate_entry(self.inode, n.encode(self.inodes.encoding))
171                 self.inodes.del_entry(oldentries[n])
172             self.inodes.invalidate_inode(self.inode)
173             self.invalidate()
174             return True
175         else:
176             return False
177
178     def mtime(self):
179         return self._mtime
180
181     def writable(self):
182         return False
183
184     def flush(self):
185         pass
186
187     def create(self, name):
188         raise NotImplementedError()
189
190     def mkdir(self, name):
191         raise NotImplementedError()
192
193     def unlink(self, name):
194         raise NotImplementedError()
195
196     def rmdir(self, name):
197         raise NotImplementedError()
198
199     def rename(self, name_old, name_new, src):
200         raise NotImplementedError()
201
202
203 class CollectionDirectoryBase(Directory):
204     """Represent an Arvados Collection as a directory.
205
206     This class is used for Subcollections, and is also the base class for
207     CollectionDirectory, which implements collection loading/saving on
208     Collection records.
209
210     Most operations act only the underlying Arvados `Collection` object.  The
211     `Collection` object signals via a notify callback to
212     `CollectionDirectoryBase.on_event` that an item was added, removed or
213     modified.  FUSE inodes and directory entries are created, deleted or
214     invalidated in response to these events.
215
216     """
217
218     def __init__(self, parent_inode, inodes, collection):
219         super(CollectionDirectoryBase, self).__init__(parent_inode, inodes)
220         self.collection = collection
221
222     def new_entry(self, name, item, mtime):
223         name = sanitize_filename(name)
224         if hasattr(item, "fuse_entry") and item.fuse_entry is not None:
225             if item.fuse_entry.dead is not True:
226                 raise Exception("Can only reparent dead inode entry")
227             if item.fuse_entry.inode is None:
228                 raise Exception("Reparented entry must still have valid inode")
229             item.fuse_entry.dead = False
230             self._entries[name] = item.fuse_entry
231         elif isinstance(item, arvados.collection.RichCollectionBase):
232             self._entries[name] = self.inodes.add_entry(CollectionDirectoryBase(self.inode, self.inodes, item))
233             self._entries[name].populate(mtime)
234         else:
235             self._entries[name] = self.inodes.add_entry(FuseArvadosFile(self.inode, item, mtime))
236         item.fuse_entry = self._entries[name]
237
238     def on_event(self, event, collection, name, item):
239         if collection == self.collection:
240             name = sanitize_filename(name)
241             _logger.debug("collection notify %s %s %s %s", event, collection, name, item)
242             with llfuse.lock:
243                 if event == arvados.collection.ADD:
244                     self.new_entry(name, item, self.mtime())
245                 elif event == arvados.collection.DEL:
246                     ent = self._entries[name]
247                     del self._entries[name]
248                     self.inodes.invalidate_entry(self.inode, name.encode(self.inodes.encoding))
249                     self.inodes.del_entry(ent)
250                 elif event == arvados.collection.MOD:
251                     if hasattr(item, "fuse_entry") and item.fuse_entry is not None:
252                         self.inodes.invalidate_inode(item.fuse_entry.inode)
253                     elif name in self._entries:
254                         self.inodes.invalidate_inode(self._entries[name].inode)
255
256     def populate(self, mtime):
257         self._mtime = mtime
258         self.collection.subscribe(self.on_event)
259         for entry, item in self.collection.items():
260             self.new_entry(entry, item, self.mtime())
261
262     def writable(self):
263         return self.collection.writable()
264
265     @use_counter
266     def flush(self):
267         with llfuse.lock_released:
268             self.collection.root_collection().save()
269
270     @use_counter
271     @check_update
272     def create(self, name):
273         with llfuse.lock_released:
274             self.collection.open(name, "w").close()
275
276     @use_counter
277     @check_update
278     def mkdir(self, name):
279         with llfuse.lock_released:
280             self.collection.mkdirs(name)
281
282     @use_counter
283     @check_update
284     def unlink(self, name):
285         with llfuse.lock_released:
286             self.collection.remove(name)
287         self.flush()
288
289     @use_counter
290     @check_update
291     def rmdir(self, name):
292         with llfuse.lock_released:
293             self.collection.remove(name)
294         self.flush()
295
296     @use_counter
297     @check_update
298     def rename(self, name_old, name_new, src):
299         if not isinstance(src, CollectionDirectoryBase):
300             raise llfuse.FUSEError(errno.EPERM)
301
302         if name_new in self:
303             ent = src[name_old]
304             tgt = self[name_new]
305             if isinstance(ent, FuseArvadosFile) and isinstance(tgt, FuseArvadosFile):
306                 pass
307             elif isinstance(ent, CollectionDirectoryBase) and isinstance(tgt, CollectionDirectoryBase):
308                 if len(tgt) > 0:
309                     raise llfuse.FUSEError(errno.ENOTEMPTY)
310             elif isinstance(ent, CollectionDirectoryBase) and isinstance(tgt, FuseArvadosFile):
311                 raise llfuse.FUSEError(errno.ENOTDIR)
312             elif isinstance(ent, FuseArvadosFile) and isinstance(tgt, CollectionDirectoryBase):
313                 raise llfuse.FUSEError(errno.EISDIR)
314
315         with llfuse.lock_released:
316             self.collection.rename(name_old, name_new, source_collection=src.collection, overwrite=True)
317         self.flush()
318         src.flush()
319
320
321 class CollectionDirectory(CollectionDirectoryBase):
322     """Represents the root of a directory tree representing a collection."""
323
324     def __init__(self, parent_inode, inodes, api, num_retries, collection_record=None, explicit_collection=None):
325         super(CollectionDirectory, self).__init__(parent_inode, inodes, None)
326         self.api = api
327         self.num_retries = num_retries
328         self.collection_record_file = None
329         self.collection_record = None
330         self.poll = True
331         try:
332             self.poll_time = (api._rootDesc.get('blobSignatureTtl', 60*60*2)/2)
333         except:
334             _logger.debug("Error getting blobSignatureTtl from discovery document: %s", sys.exc_info()[0])
335             self.poll_time = 60*60
336
337         if isinstance(collection_record, dict):
338             self.collection_locator = collection_record['uuid']
339             self._mtime = convertTime(collection_record.get('modified_at'))
340         else:
341             self.collection_locator = collection_record
342             self._mtime = 0
343         self._manifest_size = 0
344         if self.collection_locator:
345             self._writable = (uuid_pattern.match(self.collection_locator) is not None)
346         self._updating_lock = threading.Lock()
347
348     def same(self, i):
349         return i['uuid'] == self.collection_locator or i['portable_data_hash'] == self.collection_locator
350
351     def writable(self):
352         return self.collection.writable() if self.collection is not None else self._writable
353
354     # Used by arv-web.py to switch the contents of the CollectionDirectory
355     def change_collection(self, new_locator):
356         """Switch the contents of the CollectionDirectory.
357
358         Must be called with llfuse.lock held.
359         """
360
361         self.collection_locator = new_locator
362         self.collection_record = None
363         self.update()
364
365     def new_collection(self, new_collection_record, coll_reader):
366         if self.inode:
367             self.clear(force=True)
368
369         self.collection_record = new_collection_record
370
371         if self.collection_record:
372             self._mtime = convertTime(self.collection_record.get('modified_at'))
373             self.collection_locator = self.collection_record["uuid"]
374             if self.collection_record_file is not None:
375                 self.collection_record_file.update(self.collection_record)
376
377         self.collection = coll_reader
378         self.populate(self.mtime())
379
380     def uuid(self):
381         return self.collection_locator
382
383     @use_counter
384     def update(self, to_record_version=None):
385         try:
386             if self.collection_record is not None and portable_data_hash_pattern.match(self.collection_locator):
387                 return True
388
389             if self.collection_locator is None:
390                 self.fresh()
391                 return True
392
393             try:
394                 with llfuse.lock_released:
395                     self._updating_lock.acquire()
396                     if not self.stale():
397                         return
398
399                     _logger.debug("Updating %s", to_record_version)
400                     if self.collection is not None:
401                         if self.collection.known_past_version(to_record_version):
402                             _logger.debug("%s already processed %s", self.collection_locator, to_record_version)
403                         else:
404                             self.collection.update()
405                     else:
406                         if uuid_pattern.match(self.collection_locator):
407                             coll_reader = arvados.collection.Collection(
408                                 self.collection_locator, self.api, self.api.keep,
409                                 num_retries=self.num_retries)
410                         else:
411                             coll_reader = arvados.collection.CollectionReader(
412                                 self.collection_locator, self.api, self.api.keep,
413                                 num_retries=self.num_retries)
414                         new_collection_record = coll_reader.api_response() or {}
415                         # If the Collection only exists in Keep, there will be no API
416                         # response.  Fill in the fields we need.
417                         if 'uuid' not in new_collection_record:
418                             new_collection_record['uuid'] = self.collection_locator
419                         if "portable_data_hash" not in new_collection_record:
420                             new_collection_record["portable_data_hash"] = new_collection_record["uuid"]
421                         if 'manifest_text' not in new_collection_record:
422                             new_collection_record['manifest_text'] = coll_reader.manifest_text()
423
424                         if self.collection_record is None or self.collection_record["portable_data_hash"] != new_collection_record.get("portable_data_hash"):
425                             self.new_collection(new_collection_record, coll_reader)
426
427                         self._manifest_size = len(coll_reader.manifest_text())
428                         _logger.debug("%s manifest_size %i", self, self._manifest_size)
429                 # end with llfuse.lock_released, re-acquire lock
430
431                 self.fresh()
432                 return True
433             finally:
434                 self._updating_lock.release()
435         except arvados.errors.NotFoundError as e:
436             _logger.error("Error fetching collection '%s': %s", self.collection_locator, e)
437         except arvados.errors.ArgumentError as detail:
438             _logger.warning("arv-mount %s: error %s", self.collection_locator, detail)
439             if self.collection_record is not None and "manifest_text" in self.collection_record:
440                 _logger.warning("arv-mount manifest_text is: %s", self.collection_record["manifest_text"])
441         except Exception:
442             _logger.exception("arv-mount %s: error", self.collection_locator)
443             if self.collection_record is not None and "manifest_text" in self.collection_record:
444                 _logger.error("arv-mount manifest_text is: %s", self.collection_record["manifest_text"])
445         return False
446
447     @use_counter
448     @check_update
449     def __getitem__(self, item):
450         if item == '.arvados#collection':
451             if self.collection_record_file is None:
452                 self.collection_record_file = ObjectFile(self.inode, self.collection_record)
453                 self.inodes.add_entry(self.collection_record_file)
454             return self.collection_record_file
455         else:
456             return super(CollectionDirectory, self).__getitem__(item)
457
458     def __contains__(self, k):
459         if k == '.arvados#collection':
460             return True
461         else:
462             return super(CollectionDirectory, self).__contains__(k)
463
464     def invalidate(self):
465         self.collection_record = None
466         self.collection_record_file = None
467         super(CollectionDirectory, self).invalidate()
468
469     def persisted(self):
470         return (self.collection_locator is not None)
471
472     def objsize(self):
473         # This is an empirically-derived heuristic to estimate the memory used
474         # to store this collection's metadata.  Calculating the memory
475         # footprint directly would be more accurate, but also more complicated.
476         return self._manifest_size * 128
477
478     def finalize(self):
479         if self.collection is not None:
480             if self.writable():
481                 self.collection.save()
482             self.collection.stop_threads()
483
484
485 class TmpCollectionDirectory(CollectionDirectoryBase):
486     """A directory backed by an Arvados collection that never gets saved.
487
488     This supports using Keep as scratch space. A userspace program can
489     read the .arvados#collection file to get a current manifest in
490     order to save a snapshot of the scratch data or use it as a crunch
491     job output.
492     """
493
494     class UnsaveableCollection(arvados.collection.Collection):
495         def save(self):
496             pass
497         def save_new(self):
498             pass
499
500     def __init__(self, parent_inode, inodes, api_client, num_retries):
501         collection = self.UnsaveableCollection(
502             api_client=api_client,
503             keep_client=api_client.keep)
504         super(TmpCollectionDirectory, self).__init__(
505             parent_inode, inodes, collection)
506         self.collection_record_file = None
507         self.populate(self.mtime())
508
509     def on_event(self, *args, **kwargs):
510         super(TmpCollectionDirectory, self).on_event(*args, **kwargs)
511         if self.collection_record_file:
512             with llfuse.lock:
513                 self.collection_record_file.invalidate()
514             self.inodes.invalidate_inode(self.collection_record_file.inode)
515             _logger.debug("%s invalidated collection record", self)
516
517     def collection_record(self):
518         with llfuse.lock_released:
519             return {
520                 "uuid": None,
521                 "manifest_text": self.collection.manifest_text(),
522                 "portable_data_hash": self.collection.portable_data_hash(),
523             }
524
525     def __contains__(self, k):
526         return (k == '.arvados#collection' or
527                 super(TmpCollectionDirectory, self).__contains__(k))
528
529     @use_counter
530     def __getitem__(self, item):
531         if item == '.arvados#collection':
532             if self.collection_record_file is None:
533                 self.collection_record_file = FuncToJSONFile(
534                     self.inode, self.collection_record)
535                 self.inodes.add_entry(self.collection_record_file)
536             return self.collection_record_file
537         return super(TmpCollectionDirectory, self).__getitem__(item)
538
539     def persisted(self):
540         return False
541
542     def writable(self):
543         return True
544
545     def finalize(self):
546         self.collection.stop_threads()
547
548     def invalidate(self):
549         if self.collection_record_file:
550             self.collection_record_file.invalidate()
551         super(TmpCollectionDirectory, self).invalidate()
552
553
554 class MagicDirectory(Directory):
555     """A special directory that logically contains the set of all extant keep locators.
556
557     When a file is referenced by lookup(), it is tested to see if it is a valid
558     keep locator to a manifest, and if so, loads the manifest contents as a
559     subdirectory of this directory with the locator as the directory name.
560     Since querying a list of all extant keep locators is impractical, only
561     collections that have already been accessed are visible to readdir().
562
563     """
564
565     README_TEXT = """
566 This directory provides access to Arvados collections as subdirectories listed
567 by uuid (in the form 'zzzzz-4zz18-1234567890abcde') or portable data hash (in
568 the form '1234567890abcdef0123456789abcdef+123').
569
570 Note that this directory will appear empty until you attempt to access a
571 specific collection subdirectory (such as trying to 'cd' into it), at which
572 point the collection will actually be looked up on the server and the directory
573 will appear if it exists.
574
575 """.lstrip()
576
577     def __init__(self, parent_inode, inodes, api, num_retries, pdh_only=False):
578         super(MagicDirectory, self).__init__(parent_inode, inodes)
579         self.api = api
580         self.num_retries = num_retries
581         self.pdh_only = pdh_only
582
583     def __setattr__(self, name, value):
584         super(MagicDirectory, self).__setattr__(name, value)
585         # When we're assigned an inode, add a README.
586         if ((name == 'inode') and (self.inode is not None) and
587               (not self._entries)):
588             self._entries['README'] = self.inodes.add_entry(
589                 StringFile(self.inode, self.README_TEXT, time.time()))
590             # If we're the root directory, add an identical by_id subdirectory.
591             if self.inode == llfuse.ROOT_INODE:
592                 self._entries['by_id'] = self.inodes.add_entry(MagicDirectory(
593                         self.inode, self.inodes, self.api, self.num_retries, self.pdh_only))
594
595     def __contains__(self, k):
596         if k in self._entries:
597             return True
598
599         if not portable_data_hash_pattern.match(k) and (self.pdh_only or not uuid_pattern.match(k)):
600             return False
601
602         try:
603             e = self.inodes.add_entry(CollectionDirectory(
604                     self.inode, self.inodes, self.api, self.num_retries, k))
605
606             if e.update():
607                 if k not in self._entries:
608                     self._entries[k] = e
609                 else:
610                     self.inodes.del_entry(e)
611                 return True
612             else:
613                 self.inodes.del_entry(e)
614                 return False
615         except Exception as e:
616             _logger.debug('arv-mount exception keep %s', e)
617             self.inodes.del_entry(e)
618             return False
619
620     def __getitem__(self, item):
621         if item in self:
622             return self._entries[item]
623         else:
624             raise KeyError("No collection with id " + item)
625
626     def clear(self, force=False):
627         pass
628
629
630 class RecursiveInvalidateDirectory(Directory):
631     def invalidate(self):
632         try:
633             super(RecursiveInvalidateDirectory, self).invalidate()
634             for a in self._entries:
635                 self._entries[a].invalidate()
636         except Exception:
637             _logger.exception()
638
639
640 class TagsDirectory(RecursiveInvalidateDirectory):
641     """A special directory that contains as subdirectories all tags visible to the user."""
642
643     def __init__(self, parent_inode, inodes, api, num_retries, poll_time=60):
644         super(TagsDirectory, self).__init__(parent_inode, inodes)
645         self.api = api
646         self.num_retries = num_retries
647         self._poll = True
648         self._poll_time = poll_time
649
650     @use_counter
651     def update(self):
652         with llfuse.lock_released:
653             tags = self.api.links().list(
654                 filters=[['link_class', '=', 'tag']],
655                 select=['name'], distinct=True
656                 ).execute(num_retries=self.num_retries)
657         if "items" in tags:
658             self.merge(tags['items'],
659                        lambda i: i['name'],
660                        lambda a, i: a.tag == i['name'],
661                        lambda i: TagDirectory(self.inode, self.inodes, self.api, self.num_retries, i['name'], poll=self._poll, poll_time=self._poll_time))
662
663
664 class TagDirectory(Directory):
665     """A special directory that contains as subdirectories all collections visible
666     to the user that are tagged with a particular tag.
667     """
668
669     def __init__(self, parent_inode, inodes, api, num_retries, tag,
670                  poll=False, poll_time=60):
671         super(TagDirectory, self).__init__(parent_inode, inodes)
672         self.api = api
673         self.num_retries = num_retries
674         self.tag = tag
675         self._poll = poll
676         self._poll_time = poll_time
677
678     @use_counter
679     def update(self):
680         with llfuse.lock_released:
681             taggedcollections = self.api.links().list(
682                 filters=[['link_class', '=', 'tag'],
683                          ['name', '=', self.tag],
684                          ['head_uuid', 'is_a', 'arvados#collection']],
685                 select=['head_uuid']
686                 ).execute(num_retries=self.num_retries)
687         self.merge(taggedcollections['items'],
688                    lambda i: i['head_uuid'],
689                    lambda a, i: a.collection_locator == i['head_uuid'],
690                    lambda i: CollectionDirectory(self.inode, self.inodes, self.api, self.num_retries, i['head_uuid']))
691
692
693 class ProjectDirectory(Directory):
694     """A special directory that contains the contents of a project."""
695
696     def __init__(self, parent_inode, inodes, api, num_retries, project_object,
697                  poll=False, poll_time=60):
698         super(ProjectDirectory, self).__init__(parent_inode, inodes)
699         self.api = api
700         self.num_retries = num_retries
701         self.project_object = project_object
702         self.project_object_file = None
703         self.project_uuid = project_object['uuid']
704         self._poll = poll
705         self._poll_time = poll_time
706         self._updating_lock = threading.Lock()
707         self._current_user = None
708
709     def createDirectory(self, i):
710         if collection_uuid_pattern.match(i['uuid']):
711             return CollectionDirectory(self.inode, self.inodes, self.api, self.num_retries, i)
712         elif group_uuid_pattern.match(i['uuid']):
713             return ProjectDirectory(self.inode, self.inodes, self.api, self.num_retries, i, self._poll, self._poll_time)
714         elif link_uuid_pattern.match(i['uuid']):
715             if i['head_kind'] == 'arvados#collection' or portable_data_hash_pattern.match(i['head_uuid']):
716                 return CollectionDirectory(self.inode, self.inodes, self.api, self.num_retries, i['head_uuid'])
717             else:
718                 return None
719         elif uuid_pattern.match(i['uuid']):
720             return ObjectFile(self.parent_inode, i)
721         else:
722             return None
723
724     def uuid(self):
725         return self.project_uuid
726
727     @use_counter
728     def update(self):
729         if self.project_object_file == None:
730             self.project_object_file = ObjectFile(self.inode, self.project_object)
731             self.inodes.add_entry(self.project_object_file)
732
733         def namefn(i):
734             if 'name' in i:
735                 if i['name'] is None or len(i['name']) == 0:
736                     return None
737                 elif collection_uuid_pattern.match(i['uuid']) or group_uuid_pattern.match(i['uuid']):
738                     # collection or subproject
739                     return i['name']
740                 elif link_uuid_pattern.match(i['uuid']) and i['head_kind'] == 'arvados#collection':
741                     # name link
742                     return i['name']
743                 elif 'kind' in i and i['kind'].startswith('arvados#'):
744                     # something else
745                     return "{}.{}".format(i['name'], i['kind'][8:])
746             else:
747                 return None
748
749         def samefn(a, i):
750             if isinstance(a, CollectionDirectory) or isinstance(a, ProjectDirectory):
751                 return a.uuid() == i['uuid']
752             elif isinstance(a, ObjectFile):
753                 return a.uuid() == i['uuid'] and not a.stale()
754             return False
755
756         try:
757             with llfuse.lock_released:
758                 self._updating_lock.acquire()
759                 if not self.stale():
760                     return
761
762                 if group_uuid_pattern.match(self.project_uuid):
763                     self.project_object = self.api.groups().get(
764                         uuid=self.project_uuid).execute(num_retries=self.num_retries)
765                 elif user_uuid_pattern.match(self.project_uuid):
766                     self.project_object = self.api.users().get(
767                         uuid=self.project_uuid).execute(num_retries=self.num_retries)
768
769                 contents = arvados.util.list_all(self.api.groups().contents,
770                                                  self.num_retries, uuid=self.project_uuid)
771
772             # end with llfuse.lock_released, re-acquire lock
773
774             self.merge(contents,
775                        namefn,
776                        samefn,
777                        self.createDirectory)
778         finally:
779             self._updating_lock.release()
780
781     @use_counter
782     @check_update
783     def __getitem__(self, item):
784         if item == '.arvados#project':
785             return self.project_object_file
786         else:
787             return super(ProjectDirectory, self).__getitem__(item)
788
789     def __contains__(self, k):
790         if k == '.arvados#project':
791             return True
792         else:
793             return super(ProjectDirectory, self).__contains__(k)
794
795     @use_counter
796     @check_update
797     def writable(self):
798         with llfuse.lock_released:
799             if not self._current_user:
800                 self._current_user = self.api.users().current().execute(num_retries=self.num_retries)
801             return self._current_user["uuid"] in self.project_object["writable_by"]
802
803     def persisted(self):
804         return True
805
806     @use_counter
807     @check_update
808     def mkdir(self, name):
809         try:
810             with llfuse.lock_released:
811                 self.api.collections().create(body={"owner_uuid": self.project_uuid,
812                                                     "name": name,
813                                                     "manifest_text": ""}).execute(num_retries=self.num_retries)
814             self.invalidate()
815         except apiclient_errors.Error as error:
816             _logger.error(error)
817             raise llfuse.FUSEError(errno.EEXIST)
818
819     @use_counter
820     @check_update
821     def rmdir(self, name):
822         if name not in self:
823             raise llfuse.FUSEError(errno.ENOENT)
824         if not isinstance(self[name], CollectionDirectory):
825             raise llfuse.FUSEError(errno.EPERM)
826         if len(self[name]) > 0:
827             raise llfuse.FUSEError(errno.ENOTEMPTY)
828         with llfuse.lock_released:
829             self.api.collections().delete(uuid=self[name].uuid()).execute(num_retries=self.num_retries)
830         self.invalidate()
831
832     @use_counter
833     @check_update
834     def rename(self, name_old, name_new, src):
835         if not isinstance(src, ProjectDirectory):
836             raise llfuse.FUSEError(errno.EPERM)
837
838         ent = src[name_old]
839
840         if not isinstance(ent, CollectionDirectory):
841             raise llfuse.FUSEError(errno.EPERM)
842
843         if name_new in self:
844             # POSIX semantics for replacing one directory with another is
845             # tricky (the target directory must be empty, the operation must be
846             # atomic which isn't possible with the Arvados API as of this
847             # writing) so don't support that.
848             raise llfuse.FUSEError(errno.EPERM)
849
850         self.api.collections().update(uuid=ent.uuid(),
851                                       body={"owner_uuid": self.uuid(),
852                                             "name": name_new}).execute(num_retries=self.num_retries)
853
854         # Acually move the entry from source directory to this directory.
855         del src._entries[name_old]
856         self._entries[name_new] = ent
857         self.inodes.invalidate_entry(src.inode, name_old.encode(self.inodes.encoding))
858
859
860 class SharedDirectory(Directory):
861     """A special directory that represents users or groups who have shared projects with me."""
862
863     def __init__(self, parent_inode, inodes, api, num_retries, exclude,
864                  poll=False, poll_time=60):
865         super(SharedDirectory, self).__init__(parent_inode, inodes)
866         self.api = api
867         self.num_retries = num_retries
868         self.current_user = api.users().current().execute(num_retries=num_retries)
869         self._poll = True
870         self._poll_time = poll_time
871
872     @use_counter
873     def update(self):
874         with llfuse.lock_released:
875             all_projects = arvados.util.list_all(
876                 self.api.groups().list, self.num_retries,
877                 filters=[['group_class','=','project']])
878             objects = {}
879             for ob in all_projects:
880                 objects[ob['uuid']] = ob
881
882             roots = []
883             root_owners = {}
884             for ob in all_projects:
885                 if ob['owner_uuid'] != self.current_user['uuid'] and ob['owner_uuid'] not in objects:
886                     roots.append(ob)
887                     root_owners[ob['owner_uuid']] = True
888
889             lusers = arvados.util.list_all(
890                 self.api.users().list, self.num_retries,
891                 filters=[['uuid','in', list(root_owners)]])
892             lgroups = arvados.util.list_all(
893                 self.api.groups().list, self.num_retries,
894                 filters=[['uuid','in', list(root_owners)]])
895
896             users = {}
897             groups = {}
898
899             for l in lusers:
900                 objects[l["uuid"]] = l
901             for l in lgroups:
902                 objects[l["uuid"]] = l
903
904             contents = {}
905             for r in root_owners:
906                 if r in objects:
907                     obr = objects[r]
908                     if obr.get("name"):
909                         contents[obr["name"]] = obr
910                     #elif obr.get("username"):
911                     #    contents[obr["username"]] = obr
912                     elif "first_name" in obr:
913                         contents[u"{} {}".format(obr["first_name"], obr["last_name"])] = obr
914
915
916             for r in roots:
917                 if r['owner_uuid'] not in objects:
918                     contents[r['name']] = r
919
920         # end with llfuse.lock_released, re-acquire lock
921
922         try:
923             self.merge(contents.items(),
924                        lambda i: i[0],
925                        lambda a, i: a.uuid() == i[1]['uuid'],
926                        lambda i: ProjectDirectory(self.inode, self.inodes, self.api, self.num_retries, i[1], poll=self._poll, poll_time=self._poll_time))
927         except Exception:
928             _logger.exception()