~rye/ubuntuone-client/unique-check-is-unique

« back to all changes in this revision

Viewing changes to ubuntuone/syncdaemon/filesystem_manager.py

  • Committer: Tarmac
  • Author(s): facundo at com
  • Date: 2010-09-09 13:05:46 UTC
  • mfrom: (676.2.8 move-limbo)
  • Revision ID: tarmac-20100909130546-11g5qu3beajtesgh
Move operations live in limbo until confirmed.

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
import contextlib
28
28
import errno
29
29
import stat
 
30
import uuid
30
31
 
31
32
from ubuntuone.syncdaemon import file_shelf
32
33
from ubuntuone.syncdaemon.volume_manager import VolumeDoesNotExist
33
 
import uuid
 
34
from ubuntuone.syncdaemon.interfaces import IMarker
 
35
from ubuntuone.syncdaemon.marker import MDMarker
34
36
 
35
37
METADATA_VERSION = "5"
36
38
 
175
177
class TrashFileShelf(file_shelf.CachedFileShelf):
176
178
    """Custom file shelf that supports share and node as keys."""
177
179
 
 
180
    _marker_flag = 'marker'
 
181
    _marker_len = len(_marker_flag)
 
182
 
178
183
    def key_file(self, key):
179
 
        """Supports share and node as keys."""
 
184
        """Support share and node as keys."""
180
185
        share_id, node_id = key
181
186
 
 
187
        # convert the markers to a string that flags them
 
188
        if IMarker.providedBy(share_id):
 
189
            share_id = str(share_id) + self._marker_flag
 
190
        if IMarker.providedBy(node_id):
 
191
            node_id = str(node_id) + self._marker_flag
 
192
 
182
193
        # build a string with the node_id first to have a more sparse
183
194
        # layout in disk
184
195
        key = "%s|%s" % (node_id, share_id)
185
196
        return super(TrashFileShelf, self).key_file(key)
186
197
 
187
198
    def keys(self):
188
 
        """Restores the share/node pair"""
 
199
        """Restore the share/node pair"""
189
200
        for key in super(TrashFileShelf, self).keys():
190
201
            node_id, share_id = key.split("|")
 
202
            if node_id == 'None':
 
203
                node_id = None
 
204
            elif node_id.endswith(self._marker_flag):
 
205
                node_id = MDMarker(node_id[:-self._marker_len])
 
206
            if share_id.endswith(self._marker_flag):
 
207
                share_id = MDMarker(share_id[:-self._marker_len])
191
208
            yield (share_id, node_id)
192
209
 
193
210
 
216
233
                            type(data_dir))
217
234
        fsmdir = os.path.join(data_dir, 'fsm')
218
235
        trashdir = os.path.join(data_dir, 'trash')
 
236
        movelimbodir = os.path.join(data_dir, 'move_limbo')
219
237
        self.partials_dir = partials_dir
220
238
        if not os.path.exists(self.partials_dir):
221
239
            os.makedirs(self.partials_dir)
226
244
                                             cache_compact_threshold=4)
227
245
        self.trash = TrashFileShelf(trashdir, cache_size=100,
228
246
                                    cache_compact_threshold=4)
 
247
        self.move_limbo = TrashFileShelf(movelimbodir, cache_size=100,
 
248
                                         cache_compact_threshold=4)
229
249
        self.shares = {}
230
250
        self.vm = vm
231
251
        self.eq = None  # this will be registered later
1159
1179
            if v.get('dirty'):
1160
1180
                yield _MDObject(**v)
1161
1181
 
 
1182
    def add_to_move_limbo(self, share_id, node_id, old_parent_id,
 
1183
                          new_parent_id, new_name):
 
1184
        """Add the operation info to the move limbo."""
 
1185
        log_debug("add to move limbo: share=%r, node=%r, old_parent=%r, "
 
1186
                  "new_parent=%r, new_name=%r", share_id, node_id,
 
1187
                  old_parent_id, new_parent_id, new_name)
 
1188
        self.move_limbo[(share_id, node_id)] = (old_parent_id,
 
1189
                                                new_parent_id, new_name)
 
1190
 
 
1191
    def remove_from_move_limbo(self, share_id, node_id):
 
1192
        """Remove the node from the move limbo."""
 
1193
        log_debug("remove from move limbo: share=%r, node=%r",
 
1194
                  share_id, node_id)
 
1195
        if (share_id, node_id) in self.move_limbo:
 
1196
            del self.move_limbo[(share_id, node_id)]
 
1197
 
 
1198
    def get_iter_move_limbo(self):
 
1199
        """Return the move limbo node by node."""
 
1200
        for k, v in self.move_limbo.iteritems():
 
1201
            share_id, node_id = k
 
1202
            old_parent_id, new_parent_id, new_name = v
 
1203
            yield share_id, node_id, old_parent_id, new_parent_id, new_name
 
1204
 
1162
1205
 
1163
1206
class EnableShareWrite(object):
1164
1207
    """ Context manager to allow write in ro-shares. """