-
Notifications
You must be signed in to change notification settings - Fork 178
/
info_dir.py
59 lines (51 loc) · 2.02 KB
/
info_dir.py
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
import errno
import os
from trashcli.put.fs.fs import Fs
from trashcli.put.my_logger import MyLogger, LogData
from trashcli.put.suffix import Suffix
class InfoDir:
def __init__(self,
fs, # type: Fs
logger, # type: MyLogger
suffix, # type: Suffix
): # type: (...) -> None
self.fs = fs
self.logger = logger
self.suffix = suffix
def persist_trash_info(self,
basename, #type: str
content, #type: str
log_data, #type: LogData
info_dir_path, #type: str
):
"""
Create a .trashinfo file in the $trash/info directory.
returns the created TrashInfoFile.
"""
index = 0
name_too_long = False
while True:
suffix = self.suffix.suffix_for_index(index)
trashinfo_basename = create_trashinfo_basename(basename,
suffix,
name_too_long)
trashinfo_path = os.path.join(info_dir_path, trashinfo_basename)
try:
self.fs.atomic_write(trashinfo_path, content)
self.logger.debug(".trashinfo created as %s." % trashinfo_path,
log_data)
return trashinfo_path
except OSError as e:
if e.errno == errno.ENAMETOOLONG:
name_too_long = True
self.logger.debug(
"attempt for creating %s failed." % trashinfo_path,
log_data)
index += 1
def create_trashinfo_basename(basename, suffix, name_too_long):
after_basename = suffix + ".trashinfo"
if name_too_long:
truncated_basename = basename[0:len(basename) - len(after_basename)]
else:
truncated_basename = basename
return truncated_basename + after_basename