|
1 # Copyright 2013 Hardcoded Software (http://www.hardcoded.net) |
|
2 |
|
3 # This software is licensed under the "BSD" License as described in the "LICENSE" file, |
|
4 # which should be included with this package. The terms are also available at |
|
5 # http://www.hardcoded.net/licenses/bsd_license |
|
6 |
|
7 # This is a reimplementation of plat_other.py with reference to the |
|
8 # freedesktop.org trash specification: |
|
9 # [1] http://www.freedesktop.org/wiki/Specifications/trash-spec |
|
10 # [2] http://www.ramendik.ru/docs/trashspec.html |
|
11 # See also: |
|
12 # [3] http://standards.freedesktop.org/basedir-spec/basedir-spec-latest.html |
|
13 # |
|
14 # For external volumes this implementation will raise an exception if it can't |
|
15 # find or create the user's trash directory. |
|
16 |
|
17 from __future__ import unicode_literals |
|
18 |
|
19 import sys |
|
20 import os |
|
21 import os.path as op |
|
22 from datetime import datetime |
|
23 import stat |
|
24 try: |
|
25 from urllib.parse import quote |
|
26 except ImportError: |
|
27 # Python 2 |
|
28 from urllib import quote |
|
29 |
|
30 FILES_DIR = 'files' |
|
31 INFO_DIR = 'info' |
|
32 INFO_SUFFIX = '.trashinfo' |
|
33 |
|
34 # Default of ~/.local/share [3] |
|
35 XDG_DATA_HOME = op.expanduser(os.environ.get('XDG_DATA_HOME', '~/.local/share')) |
|
36 HOMETRASH = op.join(XDG_DATA_HOME, 'Trash') |
|
37 |
|
38 uid = os.getuid() |
|
39 TOPDIR_TRASH = '.Trash' |
|
40 TOPDIR_FALLBACK = '.Trash-' + str(uid) |
|
41 |
|
42 def is_parent(parent, path): |
|
43 path = op.realpath(path) # In case it's a symlink |
|
44 parent = op.realpath(parent) |
|
45 return path.startswith(parent) |
|
46 |
|
47 def format_date(date): |
|
48 return date.strftime("%Y-%m-%dT%H:%M:%S") |
|
49 |
|
50 def info_for(src, topdir): |
|
51 # ...it MUST not include a ".."" directory, and for files not "under" that |
|
52 # directory, absolute pathnames must be used. [2] |
|
53 if topdir is None or not is_parent(topdir, src): |
|
54 src = op.abspath(src) |
|
55 else: |
|
56 src = op.relpath(src, topdir) |
|
57 |
|
58 info = "[Trash Info]\n" |
|
59 info += "Path=" + quote(src) + "\n" |
|
60 info += "DeletionDate=" + format_date(datetime.now()) + "\n" |
|
61 return info |
|
62 |
|
63 def check_create(dir): |
|
64 # use 0700 for paths [3] |
|
65 if not op.exists(dir): |
|
66 os.makedirs(dir, 0o700) |
|
67 |
|
68 def trash_move(src, dst, topdir=None): |
|
69 filename = op.basename(src) |
|
70 filespath = op.join(dst, FILES_DIR) |
|
71 infopath = op.join(dst, INFO_DIR) |
|
72 base_name, ext = op.splitext(filename) |
|
73 |
|
74 counter = 0 |
|
75 destname = filename |
|
76 while op.exists(op.join(filespath, destname)) or op.exists(op.join(infopath, destname + INFO_SUFFIX)): |
|
77 counter += 1 |
|
78 destname = '%s %s%s' % (base_name, counter, ext) |
|
79 |
|
80 check_create(filespath) |
|
81 check_create(infopath) |
|
82 |
|
83 os.rename(src, op.join(filespath, destname)) |
|
84 f = open(op.join(infopath, destname + INFO_SUFFIX), 'w') |
|
85 f.write(info_for(src, topdir)) |
|
86 f.close() |
|
87 |
|
88 # added by detlev@die-offenbachs.de to update the Trash metadata file |
|
89 metadata = op.join(dst, "metadata") |
|
90 entriesCount = len(os.listdir(filespath)) |
|
91 f = open(metadata, 'w') |
|
92 f.write("[Cached]\nSize={0}\n".format(entriesCount)) |
|
93 f.close() |
|
94 |
|
95 def find_mount_point(path): |
|
96 # Even if something's wrong, "/" is a mount point, so the loop will exit. |
|
97 # Use realpath in case it's a symlink |
|
98 path = op.realpath(path) # Required to avoid infinite loop |
|
99 while not op.ismount(path): |
|
100 path = op.split(path)[0] |
|
101 return path |
|
102 |
|
103 def find_ext_volume_global_trash(volume_root): |
|
104 # from [2] Trash directories (1) check for a .Trash dir with the right |
|
105 # permissions set. |
|
106 trash_dir = op.join(volume_root, TOPDIR_TRASH) |
|
107 if not op.exists(trash_dir): |
|
108 return None |
|
109 |
|
110 mode = os.lstat(trash_dir).st_mode |
|
111 # vol/.Trash must be a directory, cannot be a symlink, and must have the |
|
112 # sticky bit set. |
|
113 if not op.isdir(trash_dir) or op.islink(trash_dir) or not (mode & stat.S_ISVTX): |
|
114 return None |
|
115 |
|
116 trash_dir = op.join(trash_dir, str(uid)) |
|
117 try: |
|
118 check_create(trash_dir) |
|
119 except OSError: |
|
120 return None |
|
121 return trash_dir |
|
122 |
|
123 def find_ext_volume_fallback_trash(volume_root): |
|
124 # from [2] Trash directories (1) create a .Trash-$uid dir. |
|
125 trash_dir = op.join(volume_root, TOPDIR_FALLBACK) |
|
126 # Try to make the directory, if we can't the OSError exception will escape |
|
127 # be thrown out of send2trash. |
|
128 check_create(trash_dir) |
|
129 return trash_dir |
|
130 |
|
131 def find_ext_volume_trash(volume_root): |
|
132 trash_dir = find_ext_volume_global_trash(volume_root) |
|
133 if trash_dir is None: |
|
134 trash_dir = find_ext_volume_fallback_trash(volume_root) |
|
135 return trash_dir |
|
136 |
|
137 # Pull this out so it's easy to stub (to avoid stubbing lstat itself) |
|
138 def get_dev(path): |
|
139 return os.lstat(path).st_dev |
|
140 |
|
141 def send2trash(path): |
|
142 if not isinstance(path, str): |
|
143 path = str(path, sys.getfilesystemencoding()) |
|
144 if not op.exists(path): |
|
145 raise OSError("File not found: %s" % path) |
|
146 # ...should check whether the user has the necessary permissions to delete |
|
147 # it, before starting the trashing operation itself. [2] |
|
148 if not os.access(path, os.W_OK): |
|
149 raise OSError("Permission denied: %s" % path) |
|
150 # if the file to be trashed is on the same device as HOMETRASH we |
|
151 # want to move it there. |
|
152 path_dev = get_dev(path) |
|
153 |
|
154 # If XDG_DATA_HOME or HOMETRASH do not yet exist we need to stat the |
|
155 # home directory, and these paths will be created further on if needed. |
|
156 trash_dev = get_dev(op.expanduser('~')) |
|
157 |
|
158 if path_dev == trash_dev: |
|
159 topdir = XDG_DATA_HOME |
|
160 dest_trash = HOMETRASH |
|
161 else: |
|
162 topdir = find_mount_point(path) |
|
163 trash_dev = get_dev(topdir) |
|
164 if trash_dev != path_dev: |
|
165 raise OSError("Couldn't find mount point for %s" % path) |
|
166 dest_trash = find_ext_volume_trash(topdir) |
|
167 trash_move(path, dest_trash, topdir) |