File: policy.py

package info (click to toggle)
python-b2sdk 2.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,020 kB
  • sloc: python: 30,902; sh: 13; makefile: 8
file content (520 lines) | stat: -rw-r--r-- 18,359 bytes parent folder | download
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
######################################################################
#
# File: b2sdk/_internal/sync/policy.py
#
# Copyright 2019 Backblaze Inc. All Rights Reserved.
#
# License https://www.backblaze.com/using_b2_code.html
#
######################################################################
from __future__ import annotations

import logging
from abc import ABCMeta, abstractmethod
from enum import Enum, unique
from typing import cast

from ..exception import DestFileNewer
from ..scan.exception import InvalidArgument
from ..scan.folder import AbstractFolder, B2Folder
from ..scan.path import AbstractPath, B2Path
from ..transfer.outbound.upload_source import UploadMode
from .action import (
    B2CopyAction,
    B2DeleteAction,
    B2DownloadAction,
    B2HideAction,
    B2IncrementalUploadAction,
    B2UploadAction,
    LocalDeleteAction,
)
from .encryption_provider import (
    SERVER_DEFAULT_SYNC_ENCRYPTION_SETTINGS_PROVIDER,
    AbstractSyncEncryptionSettingsProvider,
)

ONE_DAY_IN_MS = 24 * 60 * 60 * 1000

logger = logging.getLogger(__name__)


@unique
class NewerFileSyncMode(Enum):
    """Mode of handling files newer on destination than on source"""

    SKIP = 101  #: skip syncing such file
    REPLACE = 102  #: replace the file on the destination with the (older) file on source
    RAISE_ERROR = 103  #: raise a non-transient error, failing the sync operation


@unique
class CompareVersionMode(Enum):
    """Mode of comparing versions of files to determine what should be synced and what shouldn't"""

    MODTIME = 201  #: use file modification time on source filesystem
    SIZE = 202  #: compare using file size
    NONE = 203  #: compare using file name only


class AbstractFileSyncPolicy(metaclass=ABCMeta):
    """
    Abstract policy class.
    """

    DESTINATION_PREFIX = NotImplemented
    SOURCE_PREFIX = NotImplemented

    def __init__(
        self,
        source_path: AbstractPath | None,
        source_folder: AbstractFolder,
        dest_path: AbstractPath | None,
        dest_folder: AbstractFolder,
        now_millis: int,
        keep_days: int,
        newer_file_mode: NewerFileSyncMode,
        compare_threshold: int,
        compare_version_mode: CompareVersionMode = CompareVersionMode.MODTIME,
        encryption_settings_provider: AbstractSyncEncryptionSettingsProvider = SERVER_DEFAULT_SYNC_ENCRYPTION_SETTINGS_PROVIDER,
        upload_mode: UploadMode = UploadMode.FULL,
        absolute_minimum_part_size: int | None = None,
    ):
        """
        :param source_path: source file object
        :param source_folder: source folder object
        :param dest_path: destination file object
        :param dest_folder: destination folder object
        :param now_millis: current time in milliseconds
        :param keep_days: days to keep before delete
        :param newer_file_mode: setting which determines handling for destination files newer than on the source
        :param compare_threshold: when comparing with size or time for sync
        :param compare_version_mode: how to compare source and destination files
        :param encryption_settings_provider: encryption setting provider
        :param upload_mode: file upload mode
        :param absolute_minimum_part_size: minimum file part size that can be uploaded to the server
        """
        self._source_path = source_path
        self._source_folder = source_folder
        self._dest_path = dest_path
        self._keep_days = keep_days
        self._newer_file_mode = newer_file_mode
        self._compare_version_mode = compare_version_mode
        self._compare_threshold = compare_threshold
        self._dest_folder = dest_folder
        self._now_millis = now_millis
        self._transferred = False
        self._encryption_settings_provider = encryption_settings_provider
        self._upload_mode = upload_mode
        self._absolute_minimum_part_size = absolute_minimum_part_size

    def _should_transfer(self) -> bool:
        """
        Decide whether to transfer the file from the source to the destination.
        """
        if self._source_path is None or not self._source_path.is_visible():
            # No source file.  Nothing to transfer.
            return False
        elif self._dest_path is None:
            # Source file exists, but no destination file.  Always transfer.
            return True
        else:
            # Both exist.  Transfer only if the two are different.
            return self.files_are_different(
                self._source_path,
                self._dest_path,
                self._compare_threshold,
                self._compare_version_mode,
                self._newer_file_mode,
            )

    @classmethod
    def files_are_different(
        cls,
        source_path: AbstractPath,
        dest_path: AbstractPath,
        compare_threshold: int | None = None,
        compare_version_mode: CompareVersionMode = CompareVersionMode.MODTIME,
        newer_file_mode: NewerFileSyncMode = NewerFileSyncMode.RAISE_ERROR,
    ):
        """
        Compare two files and determine if the the destination file
        should be replaced by the source file.

        :param b2sdk.v2.AbstractPath source_path: source file object
        :param b2sdk.v2.AbstractPath dest_path: destination file object
        :param int compare_threshold: compare threshold when comparing by time or size
        :param b2sdk.v2.CompareVersionMode compare_version_mode: source file version comparator method
        :param b2sdk.v2.NewerFileSyncMode newer_file_mode: newer destination handling method
        """
        # Optionally set a compare threshold for fuzzy comparison
        compare_threshold = compare_threshold or 0

        # Compare using file name only
        if compare_version_mode == CompareVersionMode.NONE:
            return False

        # Compare using modification time
        elif compare_version_mode == CompareVersionMode.MODTIME:
            # Get the modification time of the latest versions
            source_mod_time = source_path.mod_time
            dest_mod_time = dest_path.mod_time
            diff_mod_time = abs(source_mod_time - dest_mod_time)
            compare_threshold_exceeded = diff_mod_time > compare_threshold

            logger.debug(
                'File %s: source time %s, dest time %s, diff %s, threshold %s, diff > threshold %s',
                source_path.relative_path,
                source_mod_time,
                dest_mod_time,
                diff_mod_time,
                compare_threshold,
                compare_threshold_exceeded,
            )

            if compare_threshold_exceeded:
                # Source is newer
                if dest_mod_time < source_mod_time:
                    return True

                # Source is older
                elif source_mod_time < dest_mod_time:
                    if newer_file_mode == NewerFileSyncMode.REPLACE:
                        return True
                    elif newer_file_mode == NewerFileSyncMode.SKIP:
                        return False
                    else:
                        raise DestFileNewer(
                            dest_path, source_path, cls.DESTINATION_PREFIX, cls.SOURCE_PREFIX
                        )

        # Compare using file size
        elif compare_version_mode == CompareVersionMode.SIZE:
            # Get file size of the latest versions
            source_size = source_path.size
            dest_size = dest_path.size
            diff_size = abs(source_size - dest_size)
            compare_threshold_exceeded = diff_size > compare_threshold

            logger.debug(
                'File %s: source size %s, dest size %s, diff %s, threshold %s, diff > threshold %s',
                source_path.relative_path,
                source_size,
                dest_size,
                diff_size,
                compare_threshold,
                compare_threshold_exceeded,
            )

            # Replace if size difference is over threshold
            return compare_threshold_exceeded
        else:
            raise InvalidArgument('compare_version_mode', 'is invalid option')

    def get_all_actions(self):
        """
        Yield file actions.
        """
        if self._should_transfer():
            yield self._make_transfer_action()
            self._transferred = True

        assert self._dest_path is not None or self._source_path is not None

        yield from self._get_hide_delete_actions()

    def _get_hide_delete_actions(self):
        """
        Subclass policy can override this to hide or delete files.
        """
        return []

    def _get_source_mod_time(self) -> int:
        if self._source_path is None:
            return 0
        return self._source_path.mod_time

    @abstractmethod
    def _make_transfer_action(self):
        """
        Return an action representing transfer of file according to the selected policy.
        """


class DownPolicy(AbstractFileSyncPolicy):
    """
    File is synced down (from the cloud to disk).
    """

    DESTINATION_PREFIX = 'local://'
    SOURCE_PREFIX = 'b2://'

    def _make_transfer_action(self):
        return B2DownloadAction(
            cast(B2Path, self._source_path),
            self._source_folder.make_full_path(self._source_path.relative_path),
            self._dest_folder.make_full_path(self._source_path.relative_path),
            self._encryption_settings_provider,
        )


class UpPolicy(AbstractFileSyncPolicy):
    """
    File is synced up (from disk the cloud).
    """

    DESTINATION_PREFIX = 'b2://'
    SOURCE_PREFIX = 'local://'

    def _make_transfer_action(self):
        # Find out if we want to append with new bytes or replace completely
        if self._upload_mode == UploadMode.INCREMENTAL and self._dest_path:
            return B2IncrementalUploadAction(
                self._source_folder.make_full_path(self._source_path.relative_path),
                self._source_path.relative_path,
                self._dest_folder.make_full_path(self._source_path.relative_path),
                self._get_source_mod_time(),
                self._source_path.size,
                self._encryption_settings_provider,
                cast(B2Path, self._dest_path).selected_version,
                self._absolute_minimum_part_size,
            )
        else:
            return B2UploadAction(
                self._source_folder.make_full_path(self._source_path.relative_path),
                self._source_path.relative_path,
                self._dest_folder.make_full_path(self._source_path.relative_path),
                self._get_source_mod_time(),
                self._source_path.size,
                self._encryption_settings_provider,
            )


class UpAndDeletePolicy(UpPolicy):
    """
    File is synced up (from disk to the cloud) and the delete flag is SET.
    """

    def _get_hide_delete_actions(self):
        yield from super()._get_hide_delete_actions()
        yield from make_b2_delete_actions(
            self._source_path,
            self._dest_path,
            self._dest_folder,
            self._transferred,
        )


class UpAndKeepDaysPolicy(UpPolicy):
    """
    File is synced up (from disk to the cloud) and the keepDays flag is SET.
    """

    def _get_hide_delete_actions(self):
        for action in super()._get_hide_delete_actions():
            yield action
        for action in make_b2_keep_days_actions(
            self._source_path,
            self._dest_path,
            self._dest_folder,
            self._transferred,
            self._keep_days,
            self._now_millis,
        ):
            yield action


class DownAndDeletePolicy(DownPolicy):
    """
    File is synced down (from the cloud to disk) and the delete flag is SET.
    """

    def _get_hide_delete_actions(self):
        yield from super()._get_hide_delete_actions()
        if self._dest_path is not None and (
            self._source_path is None or not self._source_path.is_visible()
        ):
            yield LocalDeleteAction(
                self._dest_path.relative_path,
                self._dest_folder.make_full_path(self._dest_path.relative_path),
            )


class DownAndKeepDaysPolicy(DownPolicy):
    """
    File is synced down (from the cloud to disk) and the keepDays flag is SET.
    """

    pass


class CopyPolicy(AbstractFileSyncPolicy):
    """
    File is copied (server-side).
    """

    DESTINATION_PREFIX = 'b2://'
    SOURCE_PREFIX = 'b2://'

    def _make_transfer_action(self):
        return B2CopyAction(
            self._source_folder.make_full_path(self._source_path.relative_path),
            cast(B2Path, self._source_path),
            self._dest_folder.make_full_path(self._source_path.relative_path),
            cast(B2Folder, self._source_folder).bucket,
            cast(B2Folder, self._dest_folder).bucket,
            self._encryption_settings_provider,
        )


class CopyAndDeletePolicy(CopyPolicy):
    """
    File is copied (server-side) and the delete flag is SET.
    """

    def _get_hide_delete_actions(self):
        for action in super()._get_hide_delete_actions():
            yield action
        for action in make_b2_delete_actions(
            self._source_path,
            self._dest_path,
            self._dest_folder,
            self._transferred,
        ):
            yield action


class CopyAndKeepDaysPolicy(CopyPolicy):
    """
    File is copied (server-side) and the keepDays flag is SET.
    """

    def _get_hide_delete_actions(self):
        for action in super()._get_hide_delete_actions():
            yield action
        for action in make_b2_keep_days_actions(
            self._source_path,
            self._dest_path,
            self._dest_folder,
            self._transferred,
            self._keep_days,
            self._now_millis,
        ):
            yield action


def make_b2_delete_note(version, index, transferred):
    """
    Create a note message for delete action.

    :param b2sdk.v2.FileVersion version: an object which contains file version info
    :param int index: file version index
    :param bool transferred: if True, file has been transferred, False otherwise
    """
    note = ''
    if version.action == 'hide':
        note = '(hide marker)'
    elif transferred or 0 < index:
        note = '(old version)'
    return note


def make_b2_delete_actions(
    source_path: AbstractPath | None,
    dest_path: B2Path | None,
    dest_folder: AbstractFolder,
    transferred: bool,
):
    """
    Create the actions to delete files stored on B2, which are not present locally.

    :param source_path: source file object
    :param dest_path: destination file object
    :param dest_folder: destination folder
    :param transferred: if True, file has been transferred, False otherwise
    """
    if dest_path is None:
        # B2 does not really store folders, so there is no need to hide
        # them or delete them
        return
    for version_index, version in enumerate(dest_path.all_versions):
        keep = (version_index == 0) and (source_path is not None) and not transferred
        if not keep:
            yield B2DeleteAction(
                dest_path.relative_path,
                dest_folder.make_full_path(dest_path.relative_path),
                version.id_,
                make_b2_delete_note(version, version_index, transferred),
            )


def make_b2_keep_days_actions(
    source_path: AbstractPath | None,
    dest_path: B2Path | None,
    dest_folder: AbstractFolder,
    transferred: bool,
    keep_days: int,
    now_millis: int,
):
    """
    Create the actions to hide or delete existing versions of a file
    stored in b2.

    When keepDays is set, all files that were visible any time from
    keepDays ago until now must be kept.  If versions were uploaded 5
    days ago, 15 days ago, and 25 days ago, and the keepDays is 10,
    only the 25 day-old version can be deleted.  The 15 day-old version
    was visible 10 days ago.

    :param source_path: source file object
    :param dest_path: destination file object
    :param dest_folder: destination folder object
    :param transferred: if True, file has been transferred, False otherwise
    :param keep_days: how many days to keep a file
    :param now_millis: current time in milliseconds
    """
    deleting = False
    if dest_path is None:
        # B2 does not really store folders, so there is no need to hide
        # them or delete them
        return
    for version_index, version in enumerate(dest_path.all_versions):
        # How old is this version?
        age_days = (now_millis - version.mod_time_millis) / ONE_DAY_IN_MS

        # Mostly, the versions are ordered by time, newest first,
        # BUT NOT ALWAYS. The mod time we have is the src_last_modified_millis
        # from the file info (if present), or the upload start time
        # (if not present). The user-specified src_last_modified_millis
        # may not be in order. Because of that, we no longer
        # assert that age_days is non-decreasing.
        #
        # Note that if there is an out-of-order date that is old enough
        # to trigger deletions, all the versions uploaded before that
        # (the ones after it in the list) will be deleted, even if they
        # aren't over the age threshold.

        # Do we need to hide this version?
        if version_index == 0 and source_path is None and version.action == 'upload':
            yield B2HideAction(
                dest_path.relative_path, dest_folder.make_full_path(dest_path.relative_path)
            )

        # Can we start deleting? Once we start deleting, all older
        # versions will also be deleted.
        if version.action == 'hide':
            if keep_days < age_days:
                deleting = True

        # Delete this version
        if deleting:
            yield B2DeleteAction(
                dest_path.relative_path,
                dest_folder.make_full_path(dest_path.relative_path),
                version.id_,
                make_b2_delete_note(version, version_index, transferred),
            )

        # Can we start deleting with the next version, based on the
        # age of this one?
        if keep_days < age_days:
            deleting = True