filepath.py 57 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766
  1. # -*- test-case-name: twisted.test.test_paths -*-
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Object-oriented filesystem path representation.
  6. """
  7. from __future__ import division, absolute_import
  8. import os
  9. import sys
  10. import errno
  11. import base64
  12. from os.path import isabs, exists, normpath, abspath, splitext
  13. from os.path import basename, dirname, join as joinpath
  14. from os import listdir, utime, stat
  15. from stat import S_ISREG, S_ISDIR, S_IMODE, S_ISBLK, S_ISSOCK
  16. from stat import S_IRUSR, S_IWUSR, S_IXUSR
  17. from stat import S_IRGRP, S_IWGRP, S_IXGRP
  18. from stat import S_IROTH, S_IWOTH, S_IXOTH
  19. from zope.interface import Interface, Attribute, implementer
  20. # Please keep this as light as possible on other Twisted imports; many, many
  21. # things import this module, and it would be good if it could easily be
  22. # modified for inclusion in the standard library. --glyph
  23. from twisted.python.compat import comparable, cmp, unicode
  24. from twisted.python.deprecate import deprecated
  25. from twisted.python.runtime import platform
  26. from incremental import Version
  27. from twisted.python.win32 import ERROR_FILE_NOT_FOUND, ERROR_PATH_NOT_FOUND
  28. from twisted.python.win32 import ERROR_INVALID_NAME, ERROR_DIRECTORY, O_BINARY
  29. from twisted.python.win32 import WindowsError
  30. from twisted.python.util import FancyEqMixin
  31. _CREATE_FLAGS = (os.O_EXCL |
  32. os.O_CREAT |
  33. os.O_RDWR |
  34. O_BINARY)
  35. def _stub_islink(path):
  36. """
  37. Always return C{False} if the operating system does not support symlinks.
  38. @param path: A path string.
  39. @type path: L{str}
  40. @return: C{False}
  41. @rtype: L{bool}
  42. """
  43. return False
  44. islink = getattr(os.path, 'islink', _stub_islink)
  45. randomBytes = os.urandom
  46. armor = base64.urlsafe_b64encode
  47. class IFilePath(Interface):
  48. """
  49. File path object.
  50. A file path represents a location for a file-like-object and can be
  51. organized into a hierarchy; a file path can can children which are
  52. themselves file paths.
  53. A file path has a name which unique identifies it in the context of its
  54. parent (if it has one); a file path can not have two children with the same
  55. name. This name is referred to as the file path's "base name".
  56. A series of such names can be used to locate nested children of a file
  57. path; such a series is referred to as the child's "path", relative to the
  58. parent. In this case, each name in the path is referred to as a "path
  59. segment"; the child's base name is the segment in the path.
  60. When representing a file path as a string, a "path separator" is used to
  61. delimit the path segments within the string. For a file system path, that
  62. would be C{os.sep}.
  63. Note that the values of child names may be restricted. For example, a file
  64. system path will not allow the use of the path separator in a name, and
  65. certain names (e.g. C{"."} and C{".."}) may be reserved or have special
  66. meanings.
  67. @since: 12.1
  68. """
  69. sep = Attribute("The path separator to use in string representations")
  70. def child(name):
  71. """
  72. Obtain a direct child of this file path. The child may or may not
  73. exist.
  74. @param name: the name of a child of this path. C{name} must be a direct
  75. child of this path and may not contain a path separator.
  76. @return: the child of this path with the given C{name}.
  77. @raise InsecurePath: if C{name} describes a file path that is not a
  78. direct child of this file path.
  79. """
  80. def open(mode="r"):
  81. """
  82. Opens this file path with the given mode.
  83. @return: a file-like object.
  84. @raise Exception: if this file path cannot be opened.
  85. """
  86. def changed():
  87. """
  88. Clear any cached information about the state of this path on disk.
  89. """
  90. def getsize():
  91. """
  92. Retrieve the size of this file in bytes.
  93. @return: the size of the file at this file path in bytes.
  94. @raise Exception: if the size cannot be obtained.
  95. """
  96. def getModificationTime():
  97. """
  98. Retrieve the time of last access from this file.
  99. @return: a number of seconds from the epoch.
  100. @rtype: L{float}
  101. """
  102. def getStatusChangeTime():
  103. """
  104. Retrieve the time of the last status change for this file.
  105. @return: a number of seconds from the epoch.
  106. @rtype: L{float}
  107. """
  108. def getAccessTime():
  109. """
  110. Retrieve the time that this file was last accessed.
  111. @return: a number of seconds from the epoch.
  112. @rtype: L{float}
  113. """
  114. def exists():
  115. """
  116. Check if this file path exists.
  117. @return: C{True} if the file at this file path exists, C{False}
  118. otherwise.
  119. @rtype: L{bool}
  120. """
  121. def isdir():
  122. """
  123. Check if this file path refers to a directory.
  124. @return: C{True} if the file at this file path is a directory, C{False}
  125. otherwise.
  126. """
  127. def isfile():
  128. """
  129. Check if this file path refers to a regular file.
  130. @return: C{True} if the file at this file path is a regular file,
  131. C{False} otherwise.
  132. """
  133. def children():
  134. """
  135. List the children of this path object.
  136. @return: a sequence of the children of the directory at this file path.
  137. @raise Exception: if the file at this file path is not a directory.
  138. """
  139. def basename():
  140. """
  141. Retrieve the final component of the file path's path (everything
  142. after the final path separator).
  143. @return: the base name of this file path.
  144. @rtype: L{str}
  145. """
  146. def parent():
  147. """
  148. A file path for the directory containing the file at this file path.
  149. """
  150. def sibling(name):
  151. """
  152. A file path for the directory containing the file at this file path.
  153. @param name: the name of a sibling of this path. C{name} must be a
  154. direct sibling of this path and may not contain a path separator.
  155. @return: a sibling file path of this one.
  156. """
  157. class InsecurePath(Exception):
  158. """
  159. Error that is raised when the path provided to L{FilePath} is invalid.
  160. """
  161. class LinkError(Exception):
  162. """
  163. An error with symlinks - either that there are cyclical symlinks or that
  164. symlink are not supported on this platform.
  165. """
  166. class UnlistableError(OSError):
  167. """
  168. An exception which is used to distinguish between errors which mean 'this
  169. is not a directory you can list' and other, more catastrophic errors.
  170. This error will try to look as much like the original error as possible,
  171. while still being catchable as an independent type.
  172. @ivar originalException: the actual original exception instance, either an
  173. L{OSError} or a L{WindowsError}.
  174. """
  175. def __init__(self, originalException):
  176. """
  177. Create an UnlistableError exception.
  178. @param originalException: an instance of OSError.
  179. """
  180. self.__dict__.update(originalException.__dict__)
  181. self.originalException = originalException
  182. class _WindowsUnlistableError(UnlistableError, WindowsError):
  183. """
  184. This exception is raised on Windows, for compatibility with previous
  185. releases of FilePath where unportable programs may have done "except
  186. WindowsError:" around a call to children().
  187. It is private because all application code may portably catch
  188. L{UnlistableError} instead.
  189. """
  190. def _secureEnoughString(path):
  191. """
  192. Compute a string usable as a new, temporary filename.
  193. @param path: The path that the new temporary filename should be able to be
  194. concatenated with.
  195. @return: A pseudorandom, 16 byte string for use in secure filenames.
  196. @rtype: the type of C{path}
  197. """
  198. secureishString = armor(randomBytes(16))[:16]
  199. return _coerceToFilesystemEncoding(path, secureishString)
  200. class AbstractFilePath(object):
  201. """
  202. Abstract implementation of an L{IFilePath}; must be completed by a
  203. subclass.
  204. This class primarily exists to provide common implementations of certain
  205. methods in L{IFilePath}. It is *not* a required parent class for
  206. L{IFilePath} implementations, just a useful starting point.
  207. """
  208. def getContent(self):
  209. """
  210. Retrieve the contents of the file at this path.
  211. @return: the contents of the file
  212. @rtype: L{bytes}
  213. """
  214. with self.open() as fp:
  215. return fp.read()
  216. def parents(self):
  217. """
  218. Retrieve an iterator of all the ancestors of this path.
  219. @return: an iterator of all the ancestors of this path, from the most
  220. recent (its immediate parent) to the root of its filesystem.
  221. """
  222. path = self
  223. parent = path.parent()
  224. # root.parent() == root, so this means "are we the root"
  225. while path != parent:
  226. yield parent
  227. path = parent
  228. parent = parent.parent()
  229. def children(self):
  230. """
  231. List the children of this path object.
  232. @raise OSError: If an error occurs while listing the directory. If the
  233. error is 'serious', meaning that the operation failed due to an access
  234. violation, exhaustion of some kind of resource (file descriptors or
  235. memory), OSError or a platform-specific variant will be raised.
  236. @raise UnlistableError: If the inability to list the directory is due
  237. to this path not existing or not being a directory, the more specific
  238. OSError subclass L{UnlistableError} is raised instead.
  239. @return: an iterable of all currently-existing children of this object.
  240. """
  241. try:
  242. subnames = self.listdir()
  243. except WindowsError as winErrObj:
  244. # Under Python 3.3 and higher on Windows, WindowsError is an
  245. # alias for OSError. OSError has a winerror attribute and an
  246. # errno attribute.
  247. # Under Python 2, WindowsError is an OSError subclass.
  248. # Under Python 2.5 and higher on Windows, WindowsError has a
  249. # winerror attribute and an errno attribute.
  250. # The winerror attribute is bound to the Windows error code while
  251. # the errno attribute is bound to a translation of that code to a
  252. # perhaps equivalent POSIX error number.
  253. #
  254. # For further details, refer to:
  255. # https://docs.python.org/3/library/exceptions.html#OSError
  256. # If not for this clause OSError would be handling all of these
  257. # errors on Windows. The errno attribute contains a POSIX error
  258. # code while the winerror attribute contains a Windows error code.
  259. # Windows error codes aren't the same as POSIX error codes,
  260. # so we need to handle them differently.
  261. # Under Python 2.4 on Windows, WindowsError only has an errno
  262. # attribute. It is bound to the Windows error code.
  263. # For simplicity of code and to keep the number of paths through
  264. # this suite minimal, we grab the Windows error code under either
  265. # version.
  266. # Furthermore, attempting to use os.listdir on a non-existent path
  267. # in Python 2.4 will result in a Windows error code of
  268. # ERROR_PATH_NOT_FOUND. However, in Python 2.5,
  269. # ERROR_FILE_NOT_FOUND results instead. -exarkun
  270. winerror = getattr(winErrObj, 'winerror', winErrObj.errno)
  271. if winerror not in (ERROR_PATH_NOT_FOUND,
  272. ERROR_FILE_NOT_FOUND,
  273. ERROR_INVALID_NAME,
  274. ERROR_DIRECTORY):
  275. raise
  276. raise _WindowsUnlistableError(winErrObj)
  277. except OSError as ose:
  278. if ose.errno not in (errno.ENOENT, errno.ENOTDIR):
  279. # Other possible errors here, according to linux manpages:
  280. # EACCES, EMIFLE, ENFILE, ENOMEM. None of these seem like the
  281. # sort of thing which should be handled normally. -glyph
  282. raise
  283. raise UnlistableError(ose)
  284. return map(self.child, subnames)
  285. def walk(self, descend=None):
  286. """
  287. Yield myself, then each of my children, and each of those children's
  288. children in turn.
  289. The optional argument C{descend} is a predicate that takes a FilePath,
  290. and determines whether or not that FilePath is traversed/descended
  291. into. It will be called with each path for which C{isdir} returns
  292. C{True}. If C{descend} is not specified, all directories will be
  293. traversed (including symbolic links which refer to directories).
  294. @param descend: A one-argument callable that will return True for
  295. FilePaths that should be traversed, False otherwise.
  296. @return: a generator yielding FilePath-like objects.
  297. """
  298. yield self
  299. if self.isdir():
  300. for c in self.children():
  301. # we should first see if it's what we want, then we
  302. # can walk through the directory
  303. if (descend is None or descend(c)):
  304. for subc in c.walk(descend):
  305. if os.path.realpath(self.path).startswith(
  306. os.path.realpath(subc.path)):
  307. raise LinkError("Cycle in file graph.")
  308. yield subc
  309. else:
  310. yield c
  311. def sibling(self, path):
  312. """
  313. Return a L{FilePath} with the same directory as this instance but with
  314. a basename of C{path}.
  315. @param path: The basename of the L{FilePath} to return.
  316. @type path: L{str}
  317. @return: The sibling path.
  318. @rtype: L{FilePath}
  319. """
  320. return self.parent().child(path)
  321. def descendant(self, segments):
  322. """
  323. Retrieve a child or child's child of this path.
  324. @param segments: A sequence of path segments as L{str} instances.
  325. @return: A L{FilePath} constructed by looking up the C{segments[0]}
  326. child of this path, the C{segments[1]} child of that path, and so
  327. on.
  328. @since: 10.2
  329. """
  330. path = self
  331. for name in segments:
  332. path = path.child(name)
  333. return path
  334. def segmentsFrom(self, ancestor):
  335. """
  336. Return a list of segments between a child and its ancestor.
  337. For example, in the case of a path X representing /a/b/c/d and a path Y
  338. representing /a/b, C{Y.segmentsFrom(X)} will return C{['c',
  339. 'd']}.
  340. @param ancestor: an instance of the same class as self, ostensibly an
  341. ancestor of self.
  342. @raise: ValueError if the 'ancestor' parameter is not actually an
  343. ancestor, i.e. a path for /x/y/z is passed as an ancestor for /a/b/c/d.
  344. @return: a list of strs
  345. """
  346. # this might be an unnecessarily inefficient implementation but it will
  347. # work on win32 and for zipfiles; later I will deterimine if the
  348. # obvious fast implemenation does the right thing too
  349. f = self
  350. p = f.parent()
  351. segments = []
  352. while f != ancestor and p != f:
  353. segments[0:0] = [f.basename()]
  354. f = p
  355. p = p.parent()
  356. if f == ancestor and segments:
  357. return segments
  358. raise ValueError("%r not parent of %r" % (ancestor, self))
  359. # new in 8.0
  360. def __hash__(self):
  361. """
  362. Hash the same as another L{FilePath} with the same path as mine.
  363. """
  364. return hash((self.__class__, self.path))
  365. # pending deprecation in 8.0
  366. def getmtime(self):
  367. """
  368. Deprecated. Use getModificationTime instead.
  369. """
  370. return int(self.getModificationTime())
  371. def getatime(self):
  372. """
  373. Deprecated. Use getAccessTime instead.
  374. """
  375. return int(self.getAccessTime())
  376. def getctime(self):
  377. """
  378. Deprecated. Use getStatusChangeTime instead.
  379. """
  380. return int(self.getStatusChangeTime())
  381. class RWX(FancyEqMixin, object):
  382. """
  383. A class representing read/write/execute permissions for a single user
  384. category (i.e. user/owner, group, or other/world). Instantiate with
  385. three boolean values: readable? writable? executable?.
  386. @type read: C{bool}
  387. @ivar read: Whether permission to read is given
  388. @type write: C{bool}
  389. @ivar write: Whether permission to write is given
  390. @type execute: C{bool}
  391. @ivar execute: Whether permission to execute is given
  392. @since: 11.1
  393. """
  394. compareAttributes = ('read', 'write', 'execute')
  395. def __init__(self, readable, writable, executable):
  396. self.read = readable
  397. self.write = writable
  398. self.execute = executable
  399. def __repr__(self):
  400. return "RWX(read=%s, write=%s, execute=%s)" % (
  401. self.read, self.write, self.execute)
  402. def shorthand(self):
  403. """
  404. Returns a short string representing the permission bits. Looks like
  405. part of what is printed by command line utilities such as 'ls -l'
  406. (e.g. 'rwx')
  407. @return: The shorthand string.
  408. @rtype: L{str}
  409. """
  410. returnval = ['r', 'w', 'x']
  411. i = 0
  412. for val in (self.read, self.write, self.execute):
  413. if not val:
  414. returnval[i] = '-'
  415. i += 1
  416. return ''.join(returnval)
  417. class Permissions(FancyEqMixin, object):
  418. """
  419. A class representing read/write/execute permissions. Instantiate with any
  420. portion of the file's mode that includes the permission bits.
  421. @type user: L{RWX}
  422. @ivar user: User/Owner permissions
  423. @type group: L{RWX}
  424. @ivar group: Group permissions
  425. @type other: L{RWX}
  426. @ivar other: Other/World permissions
  427. @since: 11.1
  428. """
  429. compareAttributes = ('user', 'group', 'other')
  430. def __init__(self, statModeInt):
  431. self.user, self.group, self.other = (
  432. [RWX(*[statModeInt & bit > 0 for bit in bitGroup]) for bitGroup in
  433. [[S_IRUSR, S_IWUSR, S_IXUSR],
  434. [S_IRGRP, S_IWGRP, S_IXGRP],
  435. [S_IROTH, S_IWOTH, S_IXOTH]]]
  436. )
  437. def __repr__(self):
  438. return "[%s | %s | %s]" % (
  439. str(self.user), str(self.group), str(self.other))
  440. def shorthand(self):
  441. """
  442. Returns a short string representing the permission bits. Looks like
  443. what is printed by command line utilities such as 'ls -l'
  444. (e.g. 'rwx-wx--x')
  445. @return: The shorthand string.
  446. @rtype: L{str}
  447. """
  448. return "".join(
  449. [x.shorthand() for x in (self.user, self.group, self.other)])
  450. class _SpecialNoValue(object):
  451. """
  452. An object that represents 'no value', to be used in deprecating statinfo.
  453. Please remove once statinfo is removed.
  454. """
  455. pass
  456. def _asFilesystemBytes(path, encoding=None):
  457. """
  458. Return C{path} as a string of L{bytes} suitable for use on this system's
  459. filesystem.
  460. @param path: The path to be made suitable.
  461. @type path: L{bytes} or L{unicode}
  462. @param encoding: The encoding to use if coercing to L{bytes}. If none is
  463. given, L{sys.getfilesystemencoding} is used.
  464. @return: L{bytes}
  465. """
  466. if type(path) == bytes:
  467. return path
  468. else:
  469. if encoding is None:
  470. encoding = sys.getfilesystemencoding()
  471. return path.encode(encoding)
  472. def _asFilesystemText(path, encoding=None):
  473. """
  474. Return C{path} as a string of L{unicode} suitable for use on this system's
  475. filesystem.
  476. @param path: The path to be made suitable.
  477. @type path: L{bytes} or L{unicode}
  478. @param encoding: The encoding to use if coercing to L{unicode}. If none
  479. is given, L{sys.getfilesystemencoding} is used.
  480. @return: L{unicode}
  481. """
  482. if type(path) == unicode:
  483. return path
  484. else:
  485. if encoding is None:
  486. encoding = sys.getfilesystemencoding()
  487. return path.decode(encoding)
  488. def _coerceToFilesystemEncoding(path, newpath, encoding=None):
  489. """
  490. Return a C{newpath} that is suitable for joining to C{path}.
  491. @param path: The path that it should be suitable for joining to.
  492. @param newpath: The new portion of the path to be coerced if needed.
  493. @param encoding: If coerced, the encoding that will be used.
  494. """
  495. if type(path) == bytes:
  496. return _asFilesystemBytes(newpath, encoding=encoding)
  497. else:
  498. return _asFilesystemText(newpath, encoding=encoding)
  499. @comparable
  500. @implementer(IFilePath)
  501. class FilePath(AbstractFilePath):
  502. """
  503. I am a path on the filesystem that only permits 'downwards' access.
  504. Instantiate me with a pathname (for example,
  505. FilePath('/home/myuser/public_html')) and I will attempt to only provide
  506. access to files which reside inside that path. I may be a path to a file,
  507. a directory, or a file which does not exist.
  508. The correct way to use me is to instantiate me, and then do ALL filesystem
  509. access through me. In other words, do not import the 'os' module; if you
  510. need to open a file, call my 'open' method. If you need to list a
  511. directory, call my 'path' method.
  512. Even if you pass me a relative path, I will convert that to an absolute
  513. path internally.
  514. Note: although time-related methods do return floating-point results, they
  515. may still be only second resolution depending on the platform and the last
  516. value passed to L{os.stat_float_times}. If you want greater-than-second
  517. precision, call C{os.stat_float_times(True)}, or use Python 2.5.
  518. Greater-than-second precision is only available in Windows on Python2.5 and
  519. later.
  520. The type of C{path} when instantiating decides the mode of the L{FilePath}.
  521. That is, C{FilePath(b"/")} will return a L{bytes} mode L{FilePath}, and
  522. C{FilePath(u"/")} will return a L{unicode} mode L{FilePath}.
  523. C{FilePath("/")} will return a L{bytes} mode L{FilePath} on Python 2, and a
  524. L{unicode} mode L{FilePath} on Python 3.
  525. Methods that return a new L{FilePath} use the type of the given subpath to
  526. decide its mode. For example, C{FilePath(b"/").child(u"tmp")} will return a
  527. L{unicode} mode L{FilePath}.
  528. @type alwaysCreate: L{bool}
  529. @ivar alwaysCreate: When opening this file, only succeed if the file does
  530. not already exist.
  531. @type path: L{bytes} or L{unicode}
  532. @ivar path: The path from which 'downward' traversal is permitted.
  533. @ivar statinfo: (WARNING: statinfo is deprecated as of Twisted 15.0.0 and
  534. will become a private attribute)
  535. The currently cached status information about the file on
  536. the filesystem that this L{FilePath} points to. This attribute is
  537. L{None} if the file is in an indeterminate state (either this
  538. L{FilePath} has not yet had cause to call C{stat()} yet or
  539. L{FilePath.changed} indicated that new information is required), 0 if
  540. C{stat()} was called and returned an error (i.e. the path did not exist
  541. when C{stat()} was called), or a C{stat_result} object that describes
  542. the last known status of the underlying file (or directory, as the case
  543. may be). Trust me when I tell you that you do not want to use this
  544. attribute. Instead, use the methods on L{FilePath} which give you
  545. information about it, like C{getsize()}, C{isdir()},
  546. C{getModificationTime()}, and so on.
  547. @type statinfo: L{int} or L{None} or L{os.stat_result}
  548. """
  549. _statinfo = None
  550. path = None
  551. def __init__(self, path, alwaysCreate=False):
  552. """
  553. Convert a path string to an absolute path if necessary and initialize
  554. the L{FilePath} with the result.
  555. """
  556. self.path = abspath(path)
  557. self.alwaysCreate = alwaysCreate
  558. def __getstate__(self):
  559. """
  560. Support serialization by discarding cached L{os.stat} results and
  561. returning everything else.
  562. """
  563. d = self.__dict__.copy()
  564. if '_statinfo' in d:
  565. del d['_statinfo']
  566. return d
  567. @property
  568. def sep(self):
  569. """
  570. Return a filesystem separator.
  571. @return: The native filesystem separator.
  572. @returntype: The same type as C{self.path}.
  573. """
  574. return _coerceToFilesystemEncoding(self.path, os.sep)
  575. def _asBytesPath(self, encoding=None):
  576. """
  577. Return the path of this L{FilePath} as bytes.
  578. @param encoding: The encoding to use if coercing to L{bytes}. If none is
  579. given, L{sys.getfilesystemencoding} is used.
  580. @return: L{bytes}
  581. """
  582. return _asFilesystemBytes(self.path, encoding=encoding)
  583. def _asTextPath(self, encoding=None):
  584. """
  585. Return the path of this L{FilePath} as text.
  586. @param encoding: The encoding to use if coercing to L{unicode}. If none
  587. is given, L{sys.getfilesystemencoding} is used.
  588. @return: L{unicode}
  589. """
  590. return _asFilesystemText(self.path, encoding=encoding)
  591. def asBytesMode(self, encoding=None):
  592. """
  593. Return this L{FilePath} in L{bytes}-mode.
  594. @param encoding: The encoding to use if coercing to L{bytes}. If none is
  595. given, L{sys.getfilesystemencoding} is used.
  596. @return: L{bytes} mode L{FilePath}
  597. """
  598. if type(self.path) == unicode:
  599. return self.clonePath(self._asBytesPath(encoding=encoding))
  600. return self
  601. def asTextMode(self, encoding=None):
  602. """
  603. Return this L{FilePath} in L{unicode}-mode.
  604. @param encoding: The encoding to use if coercing to L{unicode}. If none
  605. is given, L{sys.getfilesystemencoding} is used.
  606. @return: L{unicode} mode L{FilePath}
  607. """
  608. if type(self.path) == bytes:
  609. return self.clonePath(self._asTextPath(encoding=encoding))
  610. return self
  611. def _getPathAsSameTypeAs(self, pattern):
  612. """
  613. If C{pattern} is C{bytes}, return L{FilePath.path} as L{bytes}.
  614. Otherwise, return L{FilePath.path} as L{unicode}.
  615. @param pattern: The new element of the path that L{FilePath.path} may
  616. need to be coerced to match.
  617. """
  618. if type(pattern) == bytes:
  619. return self._asBytesPath()
  620. else:
  621. return self._asTextPath()
  622. def child(self, path):
  623. """
  624. Create and return a new L{FilePath} representing a path contained by
  625. C{self}.
  626. @param path: The base name of the new L{FilePath}. If this contains
  627. directory separators or parent references it will be rejected.
  628. @type path: L{bytes} or L{unicode}
  629. @raise InsecurePath: If the result of combining this path with C{path}
  630. would result in a path which is not a direct child of this path.
  631. @return: The child path.
  632. @rtype: L{FilePath} with a mode equal to the type of C{path}.
  633. """
  634. colon = _coerceToFilesystemEncoding(path, ":")
  635. sep = _coerceToFilesystemEncoding(path, os.sep)
  636. ourPath = self._getPathAsSameTypeAs(path)
  637. if platform.isWindows() and path.count(colon):
  638. # Catch paths like C:blah that don't have a slash
  639. raise InsecurePath("%r contains a colon." % (path,))
  640. norm = normpath(path)
  641. if sep in norm:
  642. raise InsecurePath("%r contains one or more directory separators" %
  643. (path,))
  644. newpath = abspath(joinpath(ourPath, norm))
  645. if not newpath.startswith(ourPath):
  646. raise InsecurePath("%r is not a child of %s" %
  647. (newpath, ourPath))
  648. return self.clonePath(newpath)
  649. def preauthChild(self, path):
  650. """
  651. Use me if C{path} might have slashes in it, but you know they're safe.
  652. @param path: A relative path (ie, a path not starting with C{"/"})
  653. which will be interpreted as a child or descendant of this path.
  654. @type path: L{bytes} or L{unicode}
  655. @return: The child path.
  656. @rtype: L{FilePath} with a mode equal to the type of C{path}.
  657. """
  658. ourPath = self._getPathAsSameTypeAs(path)
  659. newpath = abspath(joinpath(ourPath, normpath(path)))
  660. if not newpath.startswith(ourPath):
  661. raise InsecurePath("%s is not a child of %s" %
  662. (newpath, ourPath))
  663. return self.clonePath(newpath)
  664. def childSearchPreauth(self, *paths):
  665. """
  666. Return my first existing child with a name in C{paths}.
  667. C{paths} is expected to be a list of *pre-secured* path fragments;
  668. in most cases this will be specified by a system administrator and not
  669. an arbitrary user.
  670. If no appropriately-named children exist, this will return L{None}.
  671. @return: L{None} or the child path.
  672. @rtype: L{None} or L{FilePath}
  673. """
  674. for child in paths:
  675. p = self._getPathAsSameTypeAs(child)
  676. jp = joinpath(p, child)
  677. if exists(jp):
  678. return self.clonePath(jp)
  679. def siblingExtensionSearch(self, *exts):
  680. """
  681. Attempt to return a path with my name, given multiple possible
  682. extensions.
  683. Each extension in C{exts} will be tested and the first path which
  684. exists will be returned. If no path exists, L{None} will be returned.
  685. If C{''} is in C{exts}, then if the file referred to by this path
  686. exists, C{self} will be returned.
  687. The extension '*' has a magic meaning, which means "any path that
  688. begins with C{self.path + '.'} is acceptable".
  689. """
  690. for ext in exts:
  691. if not ext and self.exists():
  692. return self
  693. p = self._getPathAsSameTypeAs(ext)
  694. star = _coerceToFilesystemEncoding(ext, "*")
  695. dot = _coerceToFilesystemEncoding(ext, ".")
  696. if ext == star:
  697. basedot = basename(p) + dot
  698. for fn in listdir(dirname(p)):
  699. if fn.startswith(basedot):
  700. return self.clonePath(joinpath(dirname(p), fn))
  701. p2 = p + ext
  702. if exists(p2):
  703. return self.clonePath(p2)
  704. def realpath(self):
  705. """
  706. Returns the absolute target as a L{FilePath} if self is a link, self
  707. otherwise.
  708. The absolute link is the ultimate file or directory the
  709. link refers to (for instance, if the link refers to another link, and
  710. another...). If the filesystem does not support symlinks, or
  711. if the link is cyclical, raises a L{LinkError}.
  712. Behaves like L{os.path.realpath} in that it does not resolve link
  713. names in the middle (ex. /x/y/z, y is a link to w - realpath on z
  714. will return /x/y/z, not /x/w/z).
  715. @return: L{FilePath} of the target path.
  716. @rtype: L{FilePath}
  717. @raises LinkError: if links are not supported or links are cyclical.
  718. """
  719. if self.islink():
  720. result = os.path.realpath(self.path)
  721. if result == self.path:
  722. raise LinkError("Cyclical link - will loop forever")
  723. return self.clonePath(result)
  724. return self
  725. def siblingExtension(self, ext):
  726. """
  727. Attempt to return a path with my name, given the extension at C{ext}.
  728. @param ext: File-extension to search for.
  729. @type ext: L{bytes} or L{unicode}
  730. @return: The sibling path.
  731. @rtype: L{FilePath} with the same mode as the type of C{ext}.
  732. """
  733. ourPath = self._getPathAsSameTypeAs(ext)
  734. return self.clonePath(ourPath + ext)
  735. def linkTo(self, linkFilePath):
  736. """
  737. Creates a symlink to self to at the path in the L{FilePath}
  738. C{linkFilePath}.
  739. Only works on posix systems due to its dependence on
  740. L{os.symlink}. Propagates L{OSError}s up from L{os.symlink} if
  741. C{linkFilePath.parent()} does not exist, or C{linkFilePath} already
  742. exists.
  743. @param linkFilePath: a FilePath representing the link to be created.
  744. @type linkFilePath: L{FilePath}
  745. """
  746. os.symlink(self.path, linkFilePath.path)
  747. def open(self, mode='r'):
  748. """
  749. Open this file using C{mode} or for writing if C{alwaysCreate} is
  750. C{True}.
  751. In all cases the file is opened in binary mode, so it is not necessary
  752. to include C{"b"} in C{mode}.
  753. @param mode: The mode to open the file in. Default is C{"r"}.
  754. @type mode: L{str}
  755. @raises AssertionError: If C{"a"} is included in the mode and
  756. C{alwaysCreate} is C{True}.
  757. @rtype: L{file}
  758. @return: An open L{file} object.
  759. """
  760. if self.alwaysCreate:
  761. assert 'a' not in mode, ("Appending not supported when "
  762. "alwaysCreate == True")
  763. return self.create()
  764. # This hack is necessary because of a bug in Python 2.7 on Windows:
  765. # http://bugs.python.org/issue7686
  766. mode = mode.replace('b', '')
  767. return open(self.path, mode + 'b')
  768. # stat methods below
  769. def restat(self, reraise=True):
  770. """
  771. Re-calculate cached effects of 'stat'. To refresh information on this
  772. path after you know the filesystem may have changed, call this method.
  773. @param reraise: a boolean. If true, re-raise exceptions from
  774. L{os.stat}; otherwise, mark this path as not existing, and remove
  775. any cached stat information.
  776. @raise Exception: If C{reraise} is C{True} and an exception occurs
  777. while reloading metadata.
  778. """
  779. try:
  780. self._statinfo = stat(self.path)
  781. except OSError:
  782. self._statinfo = 0
  783. if reraise:
  784. raise
  785. def changed(self):
  786. """
  787. Clear any cached information about the state of this path on disk.
  788. @since: 10.1.0
  789. """
  790. self._statinfo = None
  791. def chmod(self, mode):
  792. """
  793. Changes the permissions on self, if possible. Propagates errors from
  794. L{os.chmod} up.
  795. @param mode: integer representing the new permissions desired (same as
  796. the command line chmod)
  797. @type mode: L{int}
  798. """
  799. os.chmod(self.path, mode)
  800. def getsize(self):
  801. """
  802. Retrieve the size of this file in bytes.
  803. @return: The size of the file at this file path in bytes.
  804. @raise Exception: if the size cannot be obtained.
  805. @rtype: L{int}
  806. """
  807. st = self._statinfo
  808. if not st:
  809. self.restat()
  810. st = self._statinfo
  811. return st.st_size
  812. def getModificationTime(self):
  813. """
  814. Retrieve the time of last access from this file.
  815. @return: a number of seconds from the epoch.
  816. @rtype: L{float}
  817. """
  818. st = self._statinfo
  819. if not st:
  820. self.restat()
  821. st = self._statinfo
  822. return float(st.st_mtime)
  823. def getStatusChangeTime(self):
  824. """
  825. Retrieve the time of the last status change for this file.
  826. @return: a number of seconds from the epoch.
  827. @rtype: L{float}
  828. """
  829. st = self._statinfo
  830. if not st:
  831. self.restat()
  832. st = self._statinfo
  833. return float(st.st_ctime)
  834. def getAccessTime(self):
  835. """
  836. Retrieve the time that this file was last accessed.
  837. @return: a number of seconds from the epoch.
  838. @rtype: L{float}
  839. """
  840. st = self._statinfo
  841. if not st:
  842. self.restat()
  843. st = self._statinfo
  844. return float(st.st_atime)
  845. def getInodeNumber(self):
  846. """
  847. Retrieve the file serial number, also called inode number, which
  848. distinguishes this file from all other files on the same device.
  849. @raise NotImplementedError: if the platform is Windows, since the
  850. inode number would be a dummy value for all files in Windows
  851. @return: a number representing the file serial number
  852. @rtype: L{int}
  853. @since: 11.0
  854. """
  855. if platform.isWindows():
  856. raise NotImplementedError
  857. st = self._statinfo
  858. if not st:
  859. self.restat()
  860. st = self._statinfo
  861. return st.st_ino
  862. def getDevice(self):
  863. """
  864. Retrieves the device containing the file. The inode number and device
  865. number together uniquely identify the file, but the device number is
  866. not necessarily consistent across reboots or system crashes.
  867. @raise NotImplementedError: if the platform is Windows, since the
  868. device number would be 0 for all partitions on a Windows platform
  869. @return: a number representing the device
  870. @rtype: L{int}
  871. @since: 11.0
  872. """
  873. if platform.isWindows():
  874. raise NotImplementedError
  875. st = self._statinfo
  876. if not st:
  877. self.restat()
  878. st = self._statinfo
  879. return st.st_dev
  880. def getNumberOfHardLinks(self):
  881. """
  882. Retrieves the number of hard links to the file.
  883. This count keeps track of how many directories have entries for this
  884. file. If the count is ever decremented to zero then the file itself is
  885. discarded as soon as no process still holds it open. Symbolic links
  886. are not counted in the total.
  887. @raise NotImplementedError: if the platform is Windows, since Windows
  888. doesn't maintain a link count for directories, and L{os.stat} does
  889. not set C{st_nlink} on Windows anyway.
  890. @return: the number of hard links to the file
  891. @rtype: L{int}
  892. @since: 11.0
  893. """
  894. if platform.isWindows():
  895. raise NotImplementedError
  896. st = self._statinfo
  897. if not st:
  898. self.restat()
  899. st = self._statinfo
  900. return st.st_nlink
  901. def getUserID(self):
  902. """
  903. Returns the user ID of the file's owner.
  904. @raise NotImplementedError: if the platform is Windows, since the UID
  905. is always 0 on Windows
  906. @return: the user ID of the file's owner
  907. @rtype: L{int}
  908. @since: 11.0
  909. """
  910. if platform.isWindows():
  911. raise NotImplementedError
  912. st = self._statinfo
  913. if not st:
  914. self.restat()
  915. st = self._statinfo
  916. return st.st_uid
  917. def getGroupID(self):
  918. """
  919. Returns the group ID of the file.
  920. @raise NotImplementedError: if the platform is Windows, since the GID
  921. is always 0 on windows
  922. @return: the group ID of the file
  923. @rtype: L{int}
  924. @since: 11.0
  925. """
  926. if platform.isWindows():
  927. raise NotImplementedError
  928. st = self._statinfo
  929. if not st:
  930. self.restat()
  931. st = self._statinfo
  932. return st.st_gid
  933. def getPermissions(self):
  934. """
  935. Returns the permissions of the file. Should also work on Windows,
  936. however, those permissions may not be what is expected in Windows.
  937. @return: the permissions for the file
  938. @rtype: L{Permissions}
  939. @since: 11.1
  940. """
  941. st = self._statinfo
  942. if not st:
  943. self.restat()
  944. st = self._statinfo
  945. return Permissions(S_IMODE(st.st_mode))
  946. def exists(self):
  947. """
  948. Check if this L{FilePath} exists.
  949. @return: C{True} if the stats of C{path} can be retrieved successfully,
  950. C{False} in the other cases.
  951. @rtype: L{bool}
  952. """
  953. if self._statinfo:
  954. return True
  955. else:
  956. self.restat(False)
  957. if self._statinfo:
  958. return True
  959. else:
  960. return False
  961. def isdir(self):
  962. """
  963. Check if this L{FilePath} refers to a directory.
  964. @return: C{True} if this L{FilePath} refers to a directory, C{False}
  965. otherwise.
  966. @rtype: L{bool}
  967. """
  968. st = self._statinfo
  969. if not st:
  970. self.restat(False)
  971. st = self._statinfo
  972. if not st:
  973. return False
  974. return S_ISDIR(st.st_mode)
  975. def isfile(self):
  976. """
  977. Check if this file path refers to a regular file.
  978. @return: C{True} if this L{FilePath} points to a regular file (not a
  979. directory, socket, named pipe, etc), C{False} otherwise.
  980. @rtype: L{bool}
  981. """
  982. st = self._statinfo
  983. if not st:
  984. self.restat(False)
  985. st = self._statinfo
  986. if not st:
  987. return False
  988. return S_ISREG(st.st_mode)
  989. def isBlockDevice(self):
  990. """
  991. Returns whether the underlying path is a block device.
  992. @return: C{True} if it is a block device, C{False} otherwise
  993. @rtype: L{bool}
  994. @since: 11.1
  995. """
  996. st = self._statinfo
  997. if not st:
  998. self.restat(False)
  999. st = self._statinfo
  1000. if not st:
  1001. return False
  1002. return S_ISBLK(st.st_mode)
  1003. def isSocket(self):
  1004. """
  1005. Returns whether the underlying path is a socket.
  1006. @return: C{True} if it is a socket, C{False} otherwise
  1007. @rtype: L{bool}
  1008. @since: 11.1
  1009. """
  1010. st = self._statinfo
  1011. if not st:
  1012. self.restat(False)
  1013. st = self._statinfo
  1014. if not st:
  1015. return False
  1016. return S_ISSOCK(st.st_mode)
  1017. def islink(self):
  1018. """
  1019. Check if this L{FilePath} points to a symbolic link.
  1020. @return: C{True} if this L{FilePath} points to a symbolic link,
  1021. C{False} otherwise.
  1022. @rtype: L{bool}
  1023. """
  1024. # We can't use cached stat results here, because that is the stat of
  1025. # the destination - (see #1773) which in *every case* but this one is
  1026. # the right thing to use. We could call lstat here and use that, but
  1027. # it seems unlikely we'd actually save any work that way. -glyph
  1028. return islink(self.path)
  1029. def isabs(self):
  1030. """
  1031. Check if this L{FilePath} refers to an absolute path.
  1032. This always returns C{True}.
  1033. @return: C{True}, always.
  1034. @rtype: L{bool}
  1035. """
  1036. return isabs(self.path)
  1037. def listdir(self):
  1038. """
  1039. List the base names of the direct children of this L{FilePath}.
  1040. @return: A L{list} of L{bytes}/L{unicode} giving the names of the
  1041. contents of the directory this L{FilePath} refers to. These names
  1042. are relative to this L{FilePath}.
  1043. @rtype: L{list}
  1044. @raise: Anything the platform L{os.listdir} implementation might raise
  1045. (typically L{OSError}).
  1046. """
  1047. return listdir(self.path)
  1048. def splitext(self):
  1049. """
  1050. Split the file path into a pair C{(root, ext)} such that
  1051. C{root + ext == path}.
  1052. @return: Tuple where the first item is the filename and second item is
  1053. the file extension. See Python docs for L{os.path.splitext}.
  1054. @rtype: L{tuple}
  1055. """
  1056. return splitext(self.path)
  1057. def __repr__(self):
  1058. return 'FilePath(%r)' % (self.path,)
  1059. def touch(self):
  1060. """
  1061. Updates the access and last modification times of the file at this
  1062. file path to the current time. Also creates the file if it does not
  1063. already exist.
  1064. @raise Exception: if unable to create or modify the last modification
  1065. time of the file.
  1066. """
  1067. try:
  1068. self.open('a').close()
  1069. except IOError:
  1070. pass
  1071. utime(self.path, None)
  1072. def remove(self):
  1073. """
  1074. Removes the file or directory that is represented by self. If
  1075. C{self.path} is a directory, recursively remove all its children
  1076. before removing the directory. If it's a file or link, just delete it.
  1077. """
  1078. if self.isdir() and not self.islink():
  1079. for child in self.children():
  1080. child.remove()
  1081. os.rmdir(self.path)
  1082. else:
  1083. os.remove(self.path)
  1084. self.changed()
  1085. def makedirs(self, ignoreExistingDirectory=False):
  1086. """
  1087. Create all directories not yet existing in C{path} segments, using
  1088. L{os.makedirs}.
  1089. @param ignoreExistingDirectory: Don't raise L{OSError} if directory
  1090. already exists.
  1091. @type ignoreExistingDirectory: L{bool}
  1092. @return: L{None}
  1093. """
  1094. try:
  1095. return os.makedirs(self.path)
  1096. except OSError as e:
  1097. if not (
  1098. e.errno == errno.EEXIST and
  1099. ignoreExistingDirectory and
  1100. self.isdir()):
  1101. raise
  1102. def globChildren(self, pattern):
  1103. """
  1104. Assuming I am representing a directory, return a list of FilePaths
  1105. representing my children that match the given pattern.
  1106. @param pattern: A glob pattern to use to match child paths.
  1107. @type pattern: L{unicode} or L{bytes}
  1108. @return: A L{list} of matching children.
  1109. @rtype: L{list} of L{FilePath}, with the mode of C{pattern}'s type
  1110. """
  1111. sep = _coerceToFilesystemEncoding(pattern, os.sep)
  1112. ourPath = self._getPathAsSameTypeAs(pattern)
  1113. import glob
  1114. path = ourPath[-1] == sep and ourPath + pattern \
  1115. or sep.join([ourPath, pattern])
  1116. return list(map(self.clonePath, glob.glob(path)))
  1117. def basename(self):
  1118. """
  1119. Retrieve the final component of the file path's path (everything
  1120. after the final path separator).
  1121. @return: The final component of the L{FilePath}'s path (Everything
  1122. after the final path separator).
  1123. @rtype: the same type as this L{FilePath}'s C{path} attribute
  1124. """
  1125. return basename(self.path)
  1126. def dirname(self):
  1127. """
  1128. Retrieve all of the components of the L{FilePath}'s path except the
  1129. last one (everything up to the final path separator).
  1130. @return: All of the components of the L{FilePath}'s path except the
  1131. last one (everything up to the final path separator).
  1132. @rtype: the same type as this L{FilePath}'s C{path} attribute
  1133. """
  1134. return dirname(self.path)
  1135. def parent(self):
  1136. """
  1137. A file path for the directory containing the file at this file path.
  1138. @return: A L{FilePath} representing the path which directly contains
  1139. this L{FilePath}.
  1140. @rtype: L{FilePath}
  1141. """
  1142. return self.clonePath(self.dirname())
  1143. def setContent(self, content, ext=b'.new'):
  1144. """
  1145. Replace the file at this path with a new file that contains the given
  1146. bytes, trying to avoid data-loss in the meanwhile.
  1147. On UNIX-like platforms, this method does its best to ensure that by the
  1148. time this method returns, either the old contents I{or} the new
  1149. contents of the file will be present at this path for subsequent
  1150. readers regardless of premature device removal, program crash, or power
  1151. loss, making the following assumptions:
  1152. - your filesystem is journaled (i.e. your filesystem will not
  1153. I{itself} lose data due to power loss)
  1154. - your filesystem's C{rename()} is atomic
  1155. - your filesystem will not discard new data while preserving new
  1156. metadata (see U{http://mjg59.livejournal.com/108257.html} for
  1157. more detail)
  1158. On most versions of Windows there is no atomic C{rename()} (see
  1159. U{http://bit.ly/win32-overwrite} for more information), so this method
  1160. is slightly less helpful. There is a small window where the file at
  1161. this path may be deleted before the new file is moved to replace it:
  1162. however, the new file will be fully written and flushed beforehand so
  1163. in the unlikely event that there is a crash at that point, it should be
  1164. possible for the user to manually recover the new version of their
  1165. data. In the future, Twisted will support atomic file moves on those
  1166. versions of Windows which I{do} support them: see U{Twisted ticket
  1167. 3004<http://twistedmatrix.com/trac/ticket/3004>}.
  1168. This method should be safe for use by multiple concurrent processes,
  1169. but note that it is not easy to predict which process's contents will
  1170. ultimately end up on disk if they invoke this method at close to the
  1171. same time.
  1172. @param content: The desired contents of the file at this path.
  1173. @type content: L{bytes}
  1174. @param ext: An extension to append to the temporary filename used to
  1175. store the bytes while they are being written. This can be used to
  1176. make sure that temporary files can be identified by their suffix,
  1177. for cleanup in case of crashes.
  1178. @type ext: L{bytes}
  1179. """
  1180. sib = self.temporarySibling(ext)
  1181. with sib.open('w') as f:
  1182. f.write(content)
  1183. if platform.isWindows() and exists(self.path):
  1184. os.unlink(self.path)
  1185. os.rename(sib.path, self.asBytesMode().path)
  1186. def __cmp__(self, other):
  1187. if not isinstance(other, FilePath):
  1188. return NotImplemented
  1189. return cmp(self.path, other.path)
  1190. def createDirectory(self):
  1191. """
  1192. Create the directory the L{FilePath} refers to.
  1193. @see: L{makedirs}
  1194. @raise OSError: If the directory cannot be created.
  1195. """
  1196. os.mkdir(self.path)
  1197. def requireCreate(self, val=1):
  1198. """
  1199. Sets the C{alwaysCreate} variable.
  1200. @param val: C{True} or C{False}, indicating whether opening this path
  1201. will be required to create the file or not.
  1202. @type val: L{bool}
  1203. @return: L{None}
  1204. """
  1205. self.alwaysCreate = val
  1206. def create(self):
  1207. """
  1208. Exclusively create a file, only if this file previously did not exist.
  1209. @return: A file-like object opened from this path.
  1210. """
  1211. fdint = os.open(self.path, _CREATE_FLAGS)
  1212. # XXX TODO: 'name' attribute of returned files is not mutable or
  1213. # settable via fdopen, so this file is slightly less functional than the
  1214. # one returned from 'open' by default. send a patch to Python...
  1215. return os.fdopen(fdint, 'w+b')
  1216. def temporarySibling(self, extension=b""):
  1217. """
  1218. Construct a path referring to a sibling of this path.
  1219. The resulting path will be unpredictable, so that other subprocesses
  1220. should neither accidentally attempt to refer to the same path before it
  1221. is created, nor they should other processes be able to guess its name
  1222. in advance.
  1223. @param extension: A suffix to append to the created filename. (Note
  1224. that if you want an extension with a '.' you must include the '.'
  1225. yourself.)
  1226. @type extension: L{bytes} or L{unicode}
  1227. @return: a path object with the given extension suffix, C{alwaysCreate}
  1228. set to True.
  1229. @rtype: L{FilePath} with a mode equal to the type of C{extension}
  1230. """
  1231. ourPath = self._getPathAsSameTypeAs(extension)
  1232. sib = self.sibling(_secureEnoughString(ourPath) +
  1233. self.clonePath(ourPath).basename() + extension)
  1234. sib.requireCreate()
  1235. return sib
  1236. _chunkSize = 2 ** 2 ** 2 ** 2
  1237. def copyTo(self, destination, followLinks=True):
  1238. """
  1239. Copies self to destination.
  1240. If self doesn't exist, an OSError is raised.
  1241. If self is a directory, this method copies its children (but not
  1242. itself) recursively to destination - if destination does not exist as a
  1243. directory, this method creates it. If destination is a file, an
  1244. IOError will be raised.
  1245. If self is a file, this method copies it to destination. If
  1246. destination is a file, this method overwrites it. If destination is a
  1247. directory, an IOError will be raised.
  1248. If self is a link (and followLinks is False), self will be copied
  1249. over as a new symlink with the same target as returned by os.readlink.
  1250. That means that if it is absolute, both the old and new symlink will
  1251. link to the same thing. If it's relative, then perhaps not (and
  1252. it's also possible that this relative link will be broken).
  1253. File/directory permissions and ownership will NOT be copied over.
  1254. If followLinks is True, symlinks are followed so that they're treated
  1255. as their targets. In other words, if self is a link, the link's target
  1256. will be copied. If destination is a link, self will be copied to the
  1257. destination's target (the actual destination will be destination's
  1258. target). Symlinks under self (if self is a directory) will be
  1259. followed and its target's children be copied recursively.
  1260. If followLinks is False, symlinks will be copied over as symlinks.
  1261. @param destination: the destination (a FilePath) to which self
  1262. should be copied
  1263. @param followLinks: whether symlinks in self should be treated as links
  1264. or as their targets
  1265. """
  1266. if self.islink() and not followLinks:
  1267. os.symlink(os.readlink(self.path), destination.path)
  1268. return
  1269. # XXX TODO: *thorough* audit and documentation of the exact desired
  1270. # semantics of this code. Right now the behavior of existent
  1271. # destination symlinks is convenient, and quite possibly correct, but
  1272. # its security properties need to be explained.
  1273. if self.isdir():
  1274. if not destination.exists():
  1275. destination.createDirectory()
  1276. for child in self.children():
  1277. destChild = destination.child(child.basename())
  1278. child.copyTo(destChild, followLinks)
  1279. elif self.isfile():
  1280. with destination.open('w') as writefile, self.open() as readfile:
  1281. while 1:
  1282. # XXX TODO: optionally use os.open, os.read and
  1283. # O_DIRECT and use os.fstatvfs to determine chunk sizes
  1284. # and make *****sure**** copy is page-atomic; the
  1285. # following is good enough for 99.9% of everybody and
  1286. # won't take a week to audit though.
  1287. chunk = readfile.read(self._chunkSize)
  1288. writefile.write(chunk)
  1289. if len(chunk) < self._chunkSize:
  1290. break
  1291. elif not self.exists():
  1292. raise OSError(errno.ENOENT, "No such file or directory")
  1293. else:
  1294. # If you see the following message because you want to copy
  1295. # symlinks, fifos, block devices, character devices, or unix
  1296. # sockets, please feel free to add support to do sensible things in
  1297. # reaction to those types!
  1298. raise NotImplementedError(
  1299. "Only copying of files and directories supported")
  1300. def moveTo(self, destination, followLinks=True):
  1301. """
  1302. Move self to destination - basically renaming self to whatever
  1303. destination is named.
  1304. If destination is an already-existing directory,
  1305. moves all children to destination if destination is empty. If
  1306. destination is a non-empty directory, or destination is a file, an
  1307. OSError will be raised.
  1308. If moving between filesystems, self needs to be copied, and everything
  1309. that applies to copyTo applies to moveTo.
  1310. @param destination: the destination (a FilePath) to which self
  1311. should be copied
  1312. @param followLinks: whether symlinks in self should be treated as links
  1313. or as their targets (only applicable when moving between
  1314. filesystems)
  1315. """
  1316. try:
  1317. os.rename(self._getPathAsSameTypeAs(destination.path),
  1318. destination.path)
  1319. except OSError as ose:
  1320. if ose.errno == errno.EXDEV:
  1321. # man 2 rename, ubuntu linux 5.10 "breezy":
  1322. # oldpath and newpath are not on the same mounted filesystem.
  1323. # (Linux permits a filesystem to be mounted at multiple
  1324. # points, but rename(2) does not work across different mount
  1325. # points, even if the same filesystem is mounted on both.)
  1326. # that means it's time to copy trees of directories!
  1327. secsib = destination.temporarySibling()
  1328. self.copyTo(secsib, followLinks) # slow
  1329. secsib.moveTo(destination, followLinks) # visible
  1330. # done creating new stuff. let's clean me up.
  1331. mysecsib = self.temporarySibling()
  1332. self.moveTo(mysecsib, followLinks) # visible
  1333. mysecsib.remove() # slow
  1334. else:
  1335. raise
  1336. else:
  1337. self.changed()
  1338. destination.changed()
  1339. def statinfo(self, value=_SpecialNoValue):
  1340. """
  1341. FilePath.statinfo is deprecated.
  1342. @param value: value to set statinfo to, if setting a value
  1343. @return: C{_statinfo} if getting, L{None} if setting
  1344. """
  1345. # This is a pretty awful hack to use the deprecated decorator to
  1346. # deprecate a class attribute. Ideally, there would just be a
  1347. # statinfo property and a statinfo property setter, but the
  1348. # 'deprecated' decorator does not produce the correct FQDN on class
  1349. # methods. So the property stuff needs to be set outside the class
  1350. # definition - but the getter and setter both need the same function
  1351. # in order for the 'deprecated' decorator to produce the right
  1352. # deprecation string.
  1353. if value is _SpecialNoValue:
  1354. return self._statinfo
  1355. else:
  1356. self._statinfo = value
  1357. # This is all a terrible hack to get statinfo deprecated
  1358. _tmp = deprecated(
  1359. Version('Twisted', 15, 0, 0),
  1360. "other FilePath methods such as getsize(), "
  1361. "isdir(), getModificationTime(), etc.")(FilePath.statinfo)
  1362. FilePath.statinfo = property(_tmp, _tmp)
  1363. FilePath.clonePath = FilePath