-
Notifications
You must be signed in to change notification settings - Fork 4
/
picopath.py
80 lines (61 loc) · 2.29 KB
/
picopath.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import os
class PicoPath:
S_IFDIR = 0o040000 # directory
S_IFCHR = 0o020000 # character device
S_IFBLK = 0o060000 # block device
S_IFREG = 0o100000 # regular file
S_IFIFO = 0o010000 # fifo (named pipe)
S_IFLNK = 0o120000 # symbolic link
S_IFSOCK = 0o140000 # socket file
def __init__(self, fname):
self.fname = fname
self.s = None
def stat(self):
if self.s is None:
try:
self.s = os.stat(self.fname)
except Exception as e:
return None
return self.s
def S_IFMT(self, mode):
"""Return the portion of the file's mode that describes the
file type.
"""
return mode & 0o170000
def S_ISDIR(self, mode):
"""Return True if mode is from a directory."""
return self.S_IFMT(mode) == self.S_IFDIR
def S_ISREG(self, mode):
"""Return True if mode is from a regular file."""
return self.S_IFMT(mode) == self.S_IFREG
def exists(self):
return self.stat() is not None
def is_file(self):
return self.S_ISREG(self.stat()[0])
def is_dir(self):
return self.S_ISDIR(self.stat()[0])
def invalid_path(absolute_path):
'''
Returns True if the provided absolute path is NOT valid.
Rules for validity:
- must not contain ".."
- must match an existing file/dir
- if a dir, it has to contain a gopherfile
Note that while strongly biased towards gopher, this is
currently used also for the HTTP server (as it currently
only works with the very same gopherhole contents)
'''
if '..' in absolute_path:
print('Error: attempt to access parent: {}'.format(absolute_path))
return True
path_abs = PicoPath(absolute_path)
path_gmap = PicoPath(absolute_path + '/gophermap')
if not path_abs.exists():
print('Error: attempt to access nonpublic path: {}'.format(absolute_path))
return True
elif path_abs.is_file():
return False
elif (path_abs.is_dir() and path_gmap.exists() and path_gmap.is_file()):
return False
print('Error: attempt to access something weird: {}'.format(absolute_path))
return True