FileProtection.swift 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. //
  2. // FileProtection.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. #if os(iOS) || os(watchOS) || os(tvOS)
  29. /// The values that can be obtained from `FileAttributeKey.protectionKey` on a
  30. /// file's attributes. Only available on iOS, watchOS, and tvOS.
  31. public enum FileProtection: String {
  32. /// The file has no special protections associated with it.
  33. case none
  34. /// The file is stored in an encrypted format on disk and cannot be read
  35. /// from or written to while the device is locked or booting.
  36. case complete
  37. /// The file is stored in an encrypted format on disk. Files can be created
  38. /// while the device is locked, but once closed, cannot be opened again
  39. /// until the device is unlocked.
  40. case completeUnlessOpen
  41. /// The file is stored in an encrypted format on disk and cannot be accessed
  42. /// until after the device has booted.
  43. case completeUntilFirstUserAuthentication
  44. /// Initializes `self` from a file protection value.
  45. ///
  46. /// - Parameter rawValue: The raw value to initialize from.
  47. ///
  48. public init?(rawValue: String) {
  49. switch rawValue {
  50. case FileProtectionType.none.rawValue:
  51. self = .none
  52. case FileProtectionType.complete.rawValue:
  53. self = .complete
  54. case FileProtectionType.completeUnlessOpen.rawValue:
  55. self = .completeUnlessOpen
  56. case FileProtectionType.completeUntilFirstUserAuthentication.rawValue:
  57. self = .completeUntilFirstUserAuthentication
  58. default:
  59. return nil
  60. }
  61. }
  62. /// The file protection string value of `self`.
  63. public var rawValue: String {
  64. switch self {
  65. case .none:
  66. return FileProtectionType.none.rawValue
  67. case .complete:
  68. return FileProtectionType.complete.rawValue
  69. case .completeUnlessOpen:
  70. return FileProtectionType.completeUnlessOpen.rawValue
  71. case .completeUntilFirstUserAuthentication:
  72. return FileProtectionType.completeUntilFirstUserAuthentication.rawValue
  73. }
  74. }
  75. /// Return the equivalent Data.WritingOptions
  76. public var dataWritingOption: NSData.WritingOptions {
  77. switch self {
  78. case .none:
  79. return .noFileProtection
  80. case .complete:
  81. return .completeFileProtection
  82. case .completeUnlessOpen:
  83. return .completeFileProtectionUnlessOpen
  84. case .completeUntilFirstUserAuthentication:
  85. return .completeFileProtectionUntilFirstUserAuthentication
  86. }
  87. }
  88. }
  89. extension Path {
  90. // MARK: File Protection
  91. /// The protection of the file at the path.
  92. public var fileProtection: FileProtection? {
  93. guard let value = attributes[FileAttributeKey.protectionKey] as? String,
  94. let protection = FileProtection(rawValue: value) else {
  95. return nil
  96. }
  97. return protection
  98. }
  99. /// Creates a file at path with specified file protection.
  100. ///
  101. /// - Parameter fileProtection: the protection to apply to the file.
  102. ///
  103. /// Throws an error if the file cannot be created.
  104. ///
  105. /// - Throws: `FileKitError.CreateFileFail`
  106. ///
  107. public func createFile(_ fileProtection: FileProtection) throws {
  108. let manager = FileManager()
  109. let attributes: [FileAttributeKey: Any] = [.protectionKey: fileProtection] // todo test
  110. if !manager.createFile(atPath: _safeRawValue, contents: nil, attributes: attributes) {
  111. throw FileKitError.createFileFail(path: self)
  112. }
  113. }
  114. }
  115. extension File {
  116. // MARK: File Protection
  117. /// The protection of `self`.
  118. public var protection: FileProtection? {
  119. return path.fileProtection
  120. }
  121. /// Creates the file with specified file protection.
  122. ///
  123. /// - Parameter fileProtection: the protection to apply to the file.
  124. ///
  125. /// Throws an error if the file cannot be created.
  126. ///
  127. /// - Throws: `FileKitError.CreateFileFail`
  128. ///
  129. public func create(_ fileProtection: FileProtection) throws {
  130. try path.createFile(fileProtection)
  131. }
  132. }
  133. extension File where DataType: NSData {
  134. /// Writes data to the file.
  135. ///
  136. /// - Parameter data: The data to be written to the file.
  137. /// - Parameter fileProtection: the protection to apply to the file.
  138. /// - Parameter atomically: If `true`, the data is written to an
  139. /// auxiliary file that is then renamed to the
  140. /// file. If `false`, the data is written to
  141. /// the file directly.
  142. ///
  143. /// - Throws: `FileKitError.WriteToFileFail`
  144. ///
  145. public func write(_ data: DataType, fileProtection: FileProtection, atomically: Bool = true) throws {
  146. var options = fileProtection.dataWritingOption
  147. if atomically {
  148. options.formUnion(Foundation.Data.WritingOptions.atomic)
  149. }
  150. try self.write(data, options: options)
  151. }
  152. }
  153. #endif