mirror of
https://github.com/insidegui/VirtualBuddy.git
synced 2026-12-02 03:59:23 +08:00
50 lines
1.4 KiB
Swift
50 lines
1.4 KiB
Swift
//
|
|
// VBVirtualMachine+Metadata.swift
|
|
// VirtualCore
|
|
//
|
|
// Created by Guilherme Rambo on 24/06/22.
|
|
//
|
|
|
|
import Cocoa
|
|
|
|
public extension VBVirtualMachine {
|
|
|
|
func metadataDirectoryCreatingIfNeeded() throws -> URL {
|
|
let baseURL = metadataDirectoryURL
|
|
if !FileManager.default.fileExists(atPath: baseURL.path) {
|
|
try FileManager.default.createDirectory(at: baseURL, withIntermediateDirectories: true)
|
|
}
|
|
return baseURL
|
|
}
|
|
|
|
func write(_ data: Data, forMetadataFileNamed name: String) throws {
|
|
let baseURL = try metadataDirectoryCreatingIfNeeded()
|
|
|
|
let fileURL = baseURL.appendingPathComponent(name)
|
|
|
|
try data.write(to: fileURL, options: .atomic)
|
|
}
|
|
|
|
func deleteMetadataFile(named name: String) throws {
|
|
let baseURL = try metadataDirectoryCreatingIfNeeded()
|
|
|
|
let fileURL = baseURL.appendingPathComponent(name)
|
|
|
|
guard FileManager.default.fileExists(atPath: fileURL.path) else { return }
|
|
|
|
try FileManager.default.removeItem(at: fileURL)
|
|
}
|
|
|
|
func metadataContents(_ fileName: String) -> Data? {
|
|
guard let baseURL = try? metadataDirectoryCreatingIfNeeded() else { return nil }
|
|
|
|
let fileURL = baseURL.appendingPathComponent(fileName)
|
|
|
|
guard FileManager.default.fileExists(atPath: fileURL.path) else { return nil }
|
|
|
|
return try? Data(contentsOf: fileURL)
|
|
}
|
|
|
|
}
|
|
|