FilePermissions.swift 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. //
  2. // FilePermissions.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. /// The permissions of a file.
  29. public struct FilePermissions: OptionSet, CustomStringConvertible {
  30. /// The file can be read from.
  31. public static let read = FilePermissions(rawValue: 1)
  32. /// The file can be written to.
  33. public static let write = FilePermissions(rawValue: 2)
  34. /// The file can be executed.
  35. public static let execute = FilePermissions(rawValue: 4)
  36. /// All FilePermissions
  37. public static let all: [FilePermissions] = [.read, .write, .execute]
  38. /// The raw integer value of `self`.
  39. public let rawValue: Int
  40. /// A textual representation of `self`.
  41. public var description: String {
  42. var description = ""
  43. for permission in FilePermissions.all {
  44. if self.contains(permission) {
  45. description += !description.isEmpty ? ", " : ""
  46. if permission == .read {
  47. description += "Read"
  48. } else if permission == .write {
  49. description += "Write"
  50. } else if permission == .execute {
  51. description += "Execute"
  52. }
  53. }
  54. }
  55. return String(describing: type(of: self)) + "[" + description + "]"
  56. }
  57. /// Creates a set of file permissions.
  58. ///
  59. /// - Parameter rawValue: The raw value to initialize from.
  60. ///
  61. public init(rawValue: Int) {
  62. self.rawValue = rawValue
  63. }
  64. /// Creates a set of permissions for the file at `path`.
  65. ///
  66. /// - Parameter path: The path to the file to create a set of persmissions for.
  67. ///
  68. public init(forPath path: Path) {
  69. var permissions = FilePermissions(rawValue: 0)
  70. if path.isReadable { permissions.formUnion(.read) }
  71. if path.isWritable { permissions.formUnion(.write) }
  72. if path.isExecutable { permissions.formUnion(.execute) }
  73. self = permissions
  74. }
  75. /// Creates a set of permissions for `file`.
  76. ///
  77. /// - Parameter file: The file to create a set of persmissions for.
  78. public init<DataType>(forFile file: File<DataType>) {
  79. self.init(forPath: file.path)
  80. }
  81. }