| 1 |
# -*- coding: UTF-8 -*- |
| 2 |
# Bacula(R) - The Network Backup Solution |
| 3 |
# |
| 4 |
# Copyright (C) 2000-2022 Kern Sibbald |
| 5 |
# |
| 6 |
# The original author of Bacula is Kern Sibbald, with contributions |
| 7 |
# from many others, a complete list can be found in the file AUTHORS. |
| 8 |
# |
| 9 |
# You may use this file and others of this release according to the |
| 10 |
# license defined in the LICENSE file, which includes the Affero General |
| 11 |
# Public License, v3.0 ("AGPLv3") and some additional permissions and |
| 12 |
# terms pursuant to its AGPLv3 Section 7. |
| 13 |
# |
| 14 |
# This notice must be preserved when any source code is |
| 15 |
# conveyed and/or propagated. |
| 16 |
# |
| 17 |
# Bacula(R) is a registered trademark of Kern Sibbald. |
| 18 |
|
| 19 |
import fnmatch |
| 20 |
import re |
| 21 |
|
| 22 |
NOT_EMPTY_FILE = "F" |
| 23 |
EMPTY_FILE = "E" |
| 24 |
DIRECTORY = "D" |
| 25 |
SYMBOLIC_LINK = "S" |
| 26 |
HARD_LINK = "L" |
| 27 |
DEFAULT_FILE_MODE = "100640" # -rw-r----- |
| 28 |
DEFAULT_DIR_MODE = "040755" # drwxr-xr-x |
| 29 |
MEGABYTE = 1024 * 1024 |
| 30 |
|
| 31 |
|
| 32 |
class FileInfo(object): |
| 33 |
""" |
| 34 |
Entity representing information about a File |
| 35 |
""" |
| 36 |
|
| 37 |
def __init__(self, name, |
| 38 |
ftype, size, uid, gid, |
| 39 |
accessed_at, modified_at, created_at, |
| 40 |
mode, nlink, index=None, |
| 41 |
namespace=None, objtype=None, |
| 42 |
fullfname=[]): |
| 43 |
self.name = name |
| 44 |
self.type = ftype |
| 45 |
self.size = size |
| 46 |
self.uid = uid |
| 47 |
self.gid = gid |
| 48 |
self.mode = mode |
| 49 |
self.nlink = nlink |
| 50 |
self.index = index |
| 51 |
self.accessed_at = accessed_at |
| 52 |
self.modified_at = modified_at |
| 53 |
self.created_at = created_at |
| 54 |
self.namespace = namespace |
| 55 |
self.objtype = objtype |
| 56 |
self.objcache = None |
| 57 |
self.fullfname = fullfname |
| 58 |
|
| 59 |
def __str__(self): |
| 60 |
return '{{FileInfo name:{} namespace:{} type:{} objtype:{} cached:{}}}'\ |
| 61 |
.format(str(self.name), |
| 62 |
str(self.namespace), |
| 63 |
str(self.type), |
| 64 |
str(self.objtype), |
| 65 |
self.objcache is not None) |
| 66 |
|
| 67 |
|
| 68 |
def set_name(self, name: str): |
| 69 |
self.name = name |
| 70 |
|
| 71 |
|
| 72 |
def is_bucket(self): |
| 73 |
return self.type == DIRECTORY and not self.name |
| 74 |
|
| 75 |
def match_any_glob(self, globs, current_matches): |
| 76 |
""" |
| 77 |
Verifies whether this File matches any glob inside $globs$. |
| 78 |
If it does, the $current_matches$ list will be updated. |
| 79 |
""" |
| 80 |
any_match = False |
| 81 |
|
| 82 |
for glob in globs: |
| 83 |
# Glob check |
| 84 |
if fnmatch.fnmatchcase(self.name, glob): |
| 85 |
any_match = True |
| 86 |
current_matches.append(glob) |
| 87 |
break |
| 88 |
|
| 89 |
return any_match |
| 90 |
|
| 91 |
def match_any_regex(self, regexes, current_matches): |
| 92 |
""" |
| 93 |
Verifies whether this File matches any regex inside $regexs$. |
| 94 |
If it does, the $current_matches$ list will be updated. |
| 95 |
""" |
| 96 |
|
| 97 |
any_match = False |
| 98 |
|
| 99 |
for regex in regexes: |
| 100 |
if re.match(regex, self.name): |
| 101 |
any_match = True |
| 102 |
current_matches.append(regex) |
| 103 |
break |
| 104 |
|
| 105 |
return any_match |
| 106 |
|
| 107 |
def apply_regexwhere_param(self, regexwhere): |
| 108 |
""" |
| 109 |
Applies a $regexwhere$ into this File, updating it's name. |
| 110 |
""" |
| 111 |
patterns = regexwhere.split(",") |
| 112 |
|
| 113 |
for pattern in patterns: |
| 114 |
re_flags = 0 |
| 115 |
|
| 116 |
if pattern.endswith("/i"): |
| 117 |
re_flags += re.IGNORECASE |
| 118 |
|
| 119 |
separator = pattern[0] |
| 120 |
splitted = pattern \ |
| 121 |
.strip(separator) \ |
| 122 |
.split(separator) |
| 123 |
|
| 124 |
splitted[1] = splitted[1].replace("$", "\\") |
| 125 |
|
| 126 |
# We remove empty entries |
| 127 |
splitted = list(filter(None, splitted)) |
| 128 |
new_name = re.sub(r'%s' % splitted[0], |
| 129 |
r'' + splitted[1], |
| 130 |
self.name, |
| 131 |
flags=re_flags) |
| 132 |
self.name = new_name |