21
22
def _full(self, path):
22
23
"""Returns the full path name (i.e. rootpath + path)"""
23
return os.path.join(self.rootpath, path)
24
full_path = os.path.join(self.rootpath, path)
25
if not os.path.realpath(full_path).startswith(self.rootpath):
26
raise OSError("Path not allowed:", path)
25
29
def _sanitize(self, path):
26
30
if path.startswith('/'):
28
32
path = os.path.normpath(path)
30
raise OSError("No access to anything outside CWD:", path)
34
35
def type(self, path):
35
36
"""Return the file type at path
52
53
the filter returns a true value.
54
55
path = self._sanitize(path)
57
glob_files = glob.glob(os.path.join(self.rootpath, "*"))
56
full_path = self._full(path)
57
if not os.path.exists(full_path):
58
raise OSError("Not exists:", path)
59
glob_files = glob.glob(os.path.join(self.rootpath, path, "*"))
58
60
prefix = "%s/" % self.rootpath
60
62
for filename in glob_files:
61
if os.path.isfile(filename):
62
filename = filename.replace(prefix, "")
63
if not filter or filter(filename):
63
filename = filename.replace(prefix, "")
64
filename = filename.replace("./", "")
65
if not filter or filter(filename):
67
69
def ls(self, path, filter=None):
68
70
"""Return a sequence of information objects."""
69
71
path = self._sanitize(path)
72
glob_files = glob.glob(os.path.join(self.rootpath, "*"))
72
full_path = self._full(path)
73
if not os.path.exists(full_path):
74
raise OSError("Not exists:", path)
75
glob_files = glob.glob(os.path.join(self.rootpath, path, "*"))
73
76
prefix = "%s/" % self.rootpath
75
78
for filename in glob_files:
76
if os.path.isfile(filename):
77
filename = filename.replace(prefix, "")
78
if filter is None or filter(filename):
79
infos.append(self.lsinfo(filename))
79
filename = filename.replace(prefix, "")
80
filename = filename.replace("./", "")
81
if not filter or filter(filename):
82
infos.append(self.lsinfo(filename))
82
85
def readfile(self, path, outstream, start=0, end=None):
95
98
path = self._sanitize(path)
96
99
full_path = self._full(path)
97
if os.path.exists(full_path):
98
if os.path.isdir(full_path):
99
raise OSError("Is a directory:", path)
100
if not os.path.exists(full_path):
101
101
raise OSError("Not exists:", path)
103
"owner_name": "upload",
103
info = {"owner_name": "upload",
104
104
"group_name": "upload",
105
"name": path.split("/")[-1]}
107
107
s = os.stat(full_path)
108
109
info["owner_readable"] = bool(s[stat.ST_MODE] & stat.S_IRUSR)
109
110
info["owner_writable"] = bool(s[stat.ST_MODE] & stat.S_IWUSR)
110
111
info["owner_executable"] = bool(s[stat.ST_MODE] & stat.S_IXUSR)
114
115
info["other_readable"] = bool(s[stat.ST_MODE] & stat.S_IROTH)
115
116
info["other_writable"] = bool(s[stat.ST_MODE] & stat.S_IWOTH)
116
117
info["other_executable"] = bool(s[stat.ST_MODE] & stat.S_IXOTH)
117
info["mtime"] = datetime.datetime.fromtimestamp(self.mtime(path))
118
info["mtime"] = datetime.datetime.fromtimestamp(self.mtime(path))
118
119
info["size"] = self.size(path)
120
info["type"] = self.type(path)
121
info["nlinks"] = s[stat.ST_NLINK]
121
124
def mtime(self, path):
133
136
return os.path.getsize(full_path)
135
138
def mkdir(self, path):
136
"""Create a directory.
138
Not Implemented - see upload.txt.
139
"""Create a directory."""
140
140
path = self._sanitize(path)
141
full_path = self._full(path)
142
if os.path.exists(full_path):
143
if os.path.isfile(full_path):
144
raise OSError("File already exists:", path)
145
elif os.path.isdir(full_path):
146
raise OSError("Directory already exists:", path)
147
raise OSError("OOPS, can't create:", path)
149
# XXX check leaf !?!?!
150
os.makedirs(full_path)
142
152
def remove(self, path):
143
153
"""Remove a file."""
157
167
Not Implemented - see upload.txt.
159
169
path = self._sanitize(path)
170
full_path = self._full(path)
171
if os.path.exists(full_path):
172
shutil.rmtree(full_path)
174
raise OSError("Not exists:", path)
161
176
def rename(self, old, new):
162
177
"""Rename a file."""
187
202
if os.path.exists(full_path):
188
203
if os.path.isdir(full_path):
189
204
raise OSError("Is a directory:", path)
206
dirname = os.path.dirname(full_path)
208
if not os.path.exists(dirname):
190
211
if start and start < 0:
191
212
raise ValueError("Negative start argument:", start)
192
213
if end and end < 0: