|
| 1 | +// |
| 2 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 3 | +// or more contributor license agreements. See the NOTICE file |
| 4 | +// distributed with this work for additional information |
| 5 | +// regarding copyright ownership. The ASF licenses this file |
| 6 | +// to you under the Apache License, Version 2.0 (the |
| 7 | +// "License"); you may not use this file except in compliance |
| 8 | +// with the License. You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, |
| 13 | +// software distributed under the License is distributed on an |
| 14 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | +// KIND, either express or implied. See the License for the |
| 16 | +// specific language governing permissions and limitations |
| 17 | +// under the License. |
| 18 | +// |
| 19 | +import Foundation |
| 20 | + |
| 21 | +/// Utility functions like `org.apache.spark.util.SparkFileUtils`. |
| 22 | +public enum SparkFileUtils { |
| 23 | + |
| 24 | + /// Return a well-formed URL for the file described by a user input string. |
| 25 | + /// |
| 26 | + /// If the supplied path does not contain a scheme, or is a relative path, it will be |
| 27 | + /// converted into an absolute path with a file:// scheme. |
| 28 | + /// |
| 29 | + /// - Parameter path: A path string. |
| 30 | + /// - Returns: An URL |
| 31 | + static func resolveURL(_ path: String) -> URL? { |
| 32 | + if let url = URL(string: path) { |
| 33 | + if url.scheme != nil { |
| 34 | + return url.absoluteURL |
| 35 | + } |
| 36 | + |
| 37 | + // make sure to handle if the path has a fragment (applies to yarn |
| 38 | + // distributed cache) |
| 39 | + if let fragment = url.fragment { |
| 40 | + var components = URLComponents() |
| 41 | + components.scheme = "file" |
| 42 | + components.path = (path as NSString).expandingTildeInPath |
| 43 | + components.fragment = fragment |
| 44 | + return components.url?.absoluteURL |
| 45 | + } |
| 46 | + } |
| 47 | + return URL(fileURLWithPath: (path as NSString).expandingTildeInPath).absoluteURL |
| 48 | + } |
| 49 | + |
| 50 | + /// Lists files recursively. |
| 51 | + /// - Parameter directory: <#directory description#> |
| 52 | + /// - Returns: <#description#> |
| 53 | + static func recursiveList(directory: URL) -> [URL] { |
| 54 | + let fileManager = FileManager.default |
| 55 | + var results: [URL] = [] |
| 56 | + if let enumerator = fileManager.enumerator(at: directory, includingPropertiesForKeys: nil) { |
| 57 | + for case let fileURL as URL in enumerator { |
| 58 | + results.append(fileURL) |
| 59 | + } |
| 60 | + } |
| 61 | + return results |
| 62 | + } |
| 63 | + |
| 64 | + /// Create a directory given the abstract pathname |
| 65 | + /// - Parameter url: An URL location. |
| 66 | + /// - Returns: Return true if the directory is successfully created; otherwise, return false. |
| 67 | + static func createDirectory(at url: URL) -> Bool { |
| 68 | + let fileManager = FileManager.default |
| 69 | + do { |
| 70 | + try fileManager.createDirectory(at: url, withIntermediateDirectories: true) |
| 71 | + var isDir: ObjCBool = false |
| 72 | + let exists = fileManager.fileExists(atPath: url.path, isDirectory: &isDir) |
| 73 | + return exists && isDir.boolValue |
| 74 | + } catch { |
| 75 | + print("Failed to create directory: \(url.path), error: \(error)") |
| 76 | + return false |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /// Create a temporary directory inside the given parent directory. |
| 81 | + /// - Parameters: |
| 82 | + /// - root: A parent directory. |
| 83 | + /// - namePrefix: A prefix for a new directory name. |
| 84 | + /// - Returns: An URL for the created directory |
| 85 | + static func createDirectory(root: String, namePrefix: String = "spark") -> URL { |
| 86 | + let tempDir = URL(fileURLWithPath: root).appendingPathComponent( |
| 87 | + "\(namePrefix)-\(UUID().uuidString)") |
| 88 | + _ = createDirectory(at: tempDir) |
| 89 | + return tempDir |
| 90 | + } |
| 91 | + |
| 92 | + /// Create a new temporary directory prefixed with `spark` inside ``NSTemporaryDirectory``. |
| 93 | + /// - Returns: An URL for the created directory |
| 94 | + static func createTempDir() -> URL { |
| 95 | + let dir = createDirectory(root: NSTemporaryDirectory(), namePrefix: "spark") |
| 96 | + |
| 97 | + return dir |
| 98 | + } |
| 99 | + |
| 100 | + /// Delete a file or directory and its contents recursively. |
| 101 | + /// Throws an exception if deletion is unsuccessful. |
| 102 | + /// - Parameter url: An URL location. |
| 103 | + static func deleteRecursively(_ url: URL) throws { |
| 104 | + let fileManager = FileManager.default |
| 105 | + if fileManager.fileExists(atPath: url.path) { |
| 106 | + try fileManager.removeItem(at: url) |
| 107 | + } else { |
| 108 | + throw SparkConnectError.InvalidArgumentException |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments