37 return statmod.S_IFSOCK |
37 return statmod.S_IFSOCK |
38 return mode |
38 return mode |
39 |
39 |
40 |
40 |
41 def listdir(path, stat=False, skip=None): |
41 def listdir(path, stat=False, skip=None): |
42 '''listdir(path, stat=False) -> list_of_tuples |
42 """listdir(path, stat=False) -> list_of_tuples |
43 |
43 |
44 Return a sorted list containing information about the entries |
44 Return a sorted list containing information about the entries |
45 in the directory. |
45 in the directory. |
46 |
46 |
47 If stat is True, each element is a 3-tuple: |
47 If stat is True, each element is a 3-tuple: |
49 (name, type, stat object) |
49 (name, type, stat object) |
50 |
50 |
51 Otherwise, each element is a 2-tuple: |
51 Otherwise, each element is a 2-tuple: |
52 |
52 |
53 (name, type) |
53 (name, type) |
54 ''' |
54 """ |
55 result = [] |
55 result = [] |
56 prefix = path |
56 prefix = path |
57 if not prefix.endswith(pycompat.ossep): |
57 if not prefix.endswith(pycompat.ossep): |
58 prefix += pycompat.ossep |
58 prefix += pycompat.ossep |
59 names = os.listdir(path) |
59 names = os.listdir(path) |
220 raise IOError( |
220 raise IOError( |
221 err.errno, '%s: %s' % (encoding.strfromlocal(name), err.strerror) |
221 err.errno, '%s: %s' % (encoding.strfromlocal(name), err.strerror) |
222 ) |
222 ) |
223 |
223 |
224 class posixfile(object): |
224 class posixfile(object): |
225 '''a file object aiming for POSIX-like semantics |
225 """a file object aiming for POSIX-like semantics |
226 |
226 |
227 CPython's open() returns a file that was opened *without* setting the |
227 CPython's open() returns a file that was opened *without* setting the |
228 _FILE_SHARE_DELETE flag, which causes rename and unlink to abort. |
228 _FILE_SHARE_DELETE flag, which causes rename and unlink to abort. |
229 This even happens if any hardlinked copy of the file is in open state. |
229 This even happens if any hardlinked copy of the file is in open state. |
230 We set _FILE_SHARE_DELETE here, so files opened with posixfile can be |
230 We set _FILE_SHARE_DELETE here, so files opened with posixfile can be |
231 renamed and deleted while they are held open. |
231 renamed and deleted while they are held open. |
232 Note that if a file opened with posixfile is unlinked, the file |
232 Note that if a file opened with posixfile is unlinked, the file |
233 remains but cannot be opened again or be recreated under the same name, |
233 remains but cannot be opened again or be recreated under the same name, |
234 until all reading processes have closed the file.''' |
234 until all reading processes have closed the file.""" |
235 |
235 |
236 def __init__(self, name, mode=b'r', bufsize=-1): |
236 def __init__(self, name, mode=b'r', bufsize=-1): |
237 if b'b' in mode: |
237 if b'b' in mode: |
238 flags = _O_BINARY |
238 flags = _O_BINARY |
239 else: |
239 else: |
288 |
288 |
289 def __getattr__(self, name): |
289 def __getattr__(self, name): |
290 return getattr(self._file, name) |
290 return getattr(self._file, name) |
291 |
291 |
292 def __setattr__(self, name, value): |
292 def __setattr__(self, name, value): |
293 '''mimics the read-only attributes of Python file objects |
293 """mimics the read-only attributes of Python file objects |
294 by raising 'TypeError: readonly attribute' if someone tries: |
294 by raising 'TypeError: readonly attribute' if someone tries: |
295 f = posixfile('foo.txt') |
295 f = posixfile('foo.txt') |
296 f.name = 'bla' |
296 f.name = 'bla' |
297 ''' |
297 """ |
298 return self._file.__setattr__(name, value) |
298 return self._file.__setattr__(name, value) |
299 |
299 |
300 def __enter__(self): |
300 def __enter__(self): |
301 self._file.__enter__() |
301 self._file.__enter__() |
302 return self |
302 return self |