File.swift 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // File.swift
  3. // FileKit
  4. //
  5. // The MIT License (MIT)
  6. //
  7. // Copyright (c) 2015-2017 Nikolai Vazquez
  8. //
  9. // Permission is hereby granted, free of charge, to any person obtaining a copy
  10. // of this software and associated documentation files (the "Software"), to deal
  11. // in the Software without restriction, including without limitation the rights
  12. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  13. // copies of the Software, and to permit persons to whom the Software is
  14. // furnished to do so, subject to the following conditions:
  15. //
  16. // The above copyright notice and this permission notice shall be included in
  17. // all copies or substantial portions of the Software.
  18. //
  19. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  20. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  21. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  22. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  23. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  24. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  25. // THE SOFTWARE.
  26. //
  27. import Foundation
  28. /// A representation of a filesystem file of a given data type.
  29. ///
  30. /// - Precondition: The data type must conform to ReadableWritable.
  31. ///
  32. /// All method do not follow links.
  33. open class File<DataType: ReadableWritable>: Comparable {
  34. // MARK: - Properties
  35. /// The file's filesystem path.
  36. open var path: Path
  37. /// The file's name.
  38. open var name: String {
  39. return path.fileName
  40. }
  41. /// The file's name without extension.
  42. open var nameWithoutExtension: String {
  43. return path.fileNameWithoutExtension
  44. }
  45. /// The file's filesystem path extension.
  46. public final var pathExtension: String {
  47. get {
  48. return path.pathExtension
  49. }
  50. set {
  51. path.pathExtension = newValue
  52. }
  53. }
  54. /// True if the item exists and is a regular file.
  55. ///
  56. /// this method does not follow links.
  57. open var exists: Bool {
  58. return path.isRegular
  59. }
  60. /// The size of `self` in bytes.
  61. open var size: UInt64? {
  62. return path.fileSize
  63. }
  64. // MARK: - Initialization
  65. /// Initializes a file from a path.
  66. ///
  67. /// - Parameter path: The path a file to initialize from.
  68. public init(path: Path) {
  69. self.path = path
  70. }
  71. // MARK: - Filesystem Operations
  72. /// Reads the file and returns its data.
  73. ///
  74. /// - Throws: `FileKitError.ReadFromFileFail`
  75. /// - Returns: The data read from file.
  76. open func read() throws -> DataType {
  77. return try DataType.read(from: path)
  78. }
  79. /// Writes data to the file.
  80. ///
  81. /// Writing is done atomically by default.
  82. ///
  83. /// - Parameter data: The data to be written to the file.
  84. ///
  85. /// - Throws: `FileKitError.WriteToFileFail`
  86. ///
  87. open func write(_ data: DataType) throws {
  88. try self.write(data, atomically: true)
  89. }
  90. /// Writes data to the file.
  91. ///
  92. /// - Parameter data: The data to be written to the file.
  93. /// - Parameter useAuxiliaryFile: If `true`, the data is written to an
  94. /// auxiliary file that is then renamed to the
  95. /// file. If `false`, the data is written to
  96. /// the file directly.
  97. ///
  98. /// - Throws: `FileKitError.WriteToFileFail`
  99. ///
  100. open func write(_ data: DataType, atomically useAuxiliaryFile: Bool) throws {
  101. try data.write(to: path, atomically: useAuxiliaryFile)
  102. }
  103. /// Creates the file.
  104. ///
  105. /// Throws an error if the file cannot be created.
  106. ///
  107. /// - Throws: `FileKitError.CreateFileFail`
  108. ///
  109. open func create() throws {
  110. try path.createFile()
  111. }
  112. /// Deletes the file.
  113. ///
  114. /// Throws an error if the file could not be deleted.
  115. ///
  116. /// - Throws: `FileKitError.DeleteFileFail`
  117. ///
  118. open func delete() throws {
  119. try path.deleteFile()
  120. }
  121. /// Moves the file to a path.
  122. ///
  123. /// Changes the path property to the given path.
  124. ///
  125. /// Throws an error if the file cannot be moved.
  126. ///
  127. /// - Parameter path: The path to move the file to.
  128. /// - Throws: `FileKitError.MoveFileFail`
  129. ///
  130. open func move(to path: Path) throws {
  131. try self.path.moveFile(to: path)
  132. self.path = path
  133. }
  134. /// Copies the file to a path.
  135. ///
  136. /// Throws an error if the file could not be copied or if a file already
  137. /// exists at the destination path.
  138. ///
  139. ///
  140. /// - Parameter path: The path to copy the file to.
  141. /// - Throws: `FileKitError.FileDoesNotExist`, `FileKitError.CopyFileFail`
  142. ///
  143. open func copy(to path: Path) throws {
  144. try self.path.copyFile(to: path)
  145. }
  146. /// Symlinks the file to a path.
  147. ///
  148. /// If the path already exists and _is not_ a directory, an error will be
  149. /// thrown and a link will not be created.
  150. ///
  151. /// If the path already exists and _is_ a directory, the link will be made
  152. /// to `self` in that directory.
  153. ///
  154. ///
  155. /// - Parameter path: The path to symlink the file to.
  156. /// - Throws:
  157. /// `FileKitError.FileDoesNotExist`,
  158. /// `FileKitError.CreateSymlinkFail`
  159. ///
  160. open func symlink(to path: Path) throws {
  161. try self.path.symlinkFile(to: path)
  162. }
  163. /// Hardlinks the file to a path.
  164. ///
  165. /// If the path already exists and _is not_ a directory, an error will be
  166. /// thrown and a link will not be created.
  167. ///
  168. /// If the path already exists and _is_ a directory, the link will be made
  169. /// to `self` in that directory.
  170. ///
  171. ///
  172. /// - Parameter path: The path to hardlink the file to.
  173. /// - Throws:
  174. /// `FileKitError.FileDoesNotExist`,
  175. /// `FileKitError.CreateHardlinkFail`
  176. ///
  177. open func hardlink(to path: Path) throws {
  178. try self.path.hardlinkFile(to: path)
  179. }
  180. // MARK: - FileType
  181. /// The FileType attribute for `self`.
  182. open var type: FileType? {
  183. return path.fileType
  184. }
  185. // MARK: - FilePermissions
  186. /// The file permissions for `self`.
  187. open var permissions: FilePermissions {
  188. return FilePermissions(forFile: self)
  189. }
  190. // MARK: - FileHandle
  191. /// Returns a file handle for reading from `self`, or `nil` if `self`
  192. /// doesn't exist.
  193. open var handleForReading: FileHandle? {
  194. return path.fileHandleForReading
  195. }
  196. /// Returns a file handle for writing to `self`, or `nil` if `self` doesn't
  197. /// exist.
  198. open var handleForWriting: FileHandle? {
  199. return path.fileHandleForWriting
  200. }
  201. /// Returns a file handle for reading from and writing to `self`, or `nil`
  202. /// if `self` doesn't exist.
  203. open var handleForUpdating: FileHandle? {
  204. return path.fileHandleForUpdating
  205. }
  206. // MARK: - Stream
  207. /// Returns an input stream that reads data from `self`, or `nil` if `self`
  208. /// doesn't exist.
  209. open func inputStream() -> InputStream? {
  210. return path.inputStream()
  211. }
  212. /// Returns an input stream that writes data to `self`, or `nil` if `self`
  213. /// doesn't exist.
  214. ///
  215. /// - Parameter shouldAppend: `true` if newly written data should be
  216. /// appended to any existing file contents,
  217. /// `false` otherwise. Default value is `false`.
  218. ///
  219. open func outputStream(append shouldAppend: Bool = false) -> OutputStream? {
  220. return path.outputStream(append: shouldAppend)
  221. }
  222. }
  223. extension File: CustomStringConvertible {
  224. // MARK: - CustomStringConvertible
  225. /// A textual representation of `self`.
  226. public var description: String {
  227. return String(describing: Swift.type(of: self)) + "('" + path.description + "')"
  228. }
  229. }
  230. extension File: CustomDebugStringConvertible {
  231. // MARK: - CustomDebugStringConvertible
  232. /// A textual representation of `self`, suitable for debugging.
  233. public var debugDescription: String {
  234. return description
  235. }
  236. }