Skip to content

Commit

Permalink
Add separate executables to download/offload directories, reorganize …
Browse files Browse the repository at this point in the history
…project structure
  • Loading branch information
geberl committed Aug 4, 2021
1 parent 4749f34 commit 1fee3c6
Show file tree
Hide file tree
Showing 18 changed files with 712 additions and 37 deletions.
276 changes: 259 additions & 17 deletions icloud-backup/icloud-backup.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,11 @@
</CommandLineArgument>
<CommandLineArgument
argument = "--dry-run"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--dst &quot;/Volumes/Black/icloud-documents-backup/&quot;"
isEnabled = "YES">
isEnabled = "NO">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A4317FCF26BA6CAB0012CCA6"
BuildableName = "iclouddownload"
BlueprintName = "iclouddownload"
ReferencedContainer = "container:icloud-backup.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A4317FCF26BA6CAB0012CCA6"
BuildableName = "iclouddownload"
BlueprintName = "iclouddownload"
ReferencedContainer = "container:icloud-backup.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "--version"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--verbose"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--base &quot;/Users/guenther/Documents/jobs&quot;"
isEnabled = "NO">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A4317FCF26BA6CAB0012CCA6"
BuildableName = "iclouddownload"
BlueprintName = "iclouddownload"
ReferencedContainer = "container:icloud-backup.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
<?xml version="1.0" encoding="UTF-8"?>
<Scheme
LastUpgradeVersion = "1250"
version = "1.3">
<BuildAction
parallelizeBuildables = "YES"
buildImplicitDependencies = "YES">
<BuildActionEntries>
<BuildActionEntry
buildForTesting = "YES"
buildForRunning = "YES"
buildForProfiling = "YES"
buildForArchiving = "YES"
buildForAnalyzing = "YES">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A4D287BE26BAE77A00EE9157"
BuildableName = "icloudoffload"
BlueprintName = "icloudoffload"
ReferencedContainer = "container:icloud-backup.xcodeproj">
</BuildableReference>
</BuildActionEntry>
</BuildActionEntries>
</BuildAction>
<TestAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
shouldUseLaunchSchemeArgsEnv = "YES">
<Testables>
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
launchStyle = "0"
useCustomWorkingDirectory = "NO"
ignoresPersistentStateOnLaunch = "NO"
debugDocumentVersioning = "YES"
debugServiceExtension = "internal"
allowLocationSimulation = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A4D287BE26BAE77A00EE9157"
BuildableName = "icloudoffload"
BlueprintName = "icloudoffload"
ReferencedContainer = "container:icloud-backup.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
<CommandLineArguments>
<CommandLineArgument
argument = "--base &quot;/Users/guenther/Documents/music&quot;"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--verbose"
isEnabled = "NO">
</CommandLineArgument>
<CommandLineArgument
argument = "--version"
isEnabled = "NO">
</CommandLineArgument>
</CommandLineArguments>
</LaunchAction>
<ProfileAction
buildConfiguration = "Release"
shouldUseLaunchSchemeArgsEnv = "YES"
savedToolIdentifier = ""
useCustomWorkingDirectory = "NO"
debugDocumentVersioning = "YES">
<BuildableProductRunnable
runnableDebuggingMode = "0">
<BuildableReference
BuildableIdentifier = "primary"
BlueprintIdentifier = "A4D287BE26BAE77A00EE9157"
BuildableName = "icloudoffload"
BlueprintName = "icloudoffload"
ReferencedContainer = "container:icloud-backup.xcodeproj">
</BuildableReference>
</BuildableProductRunnable>
</ProfileAction>
<AnalyzeAction
buildConfiguration = "Debug">
</AnalyzeAction>
<ArchiveAction
buildConfiguration = "Release"
revealArchiveInOrganizer = "YES">
</ArchiveAction>
</Scheme>
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct IcloudbackupOptions: ParsableArguments {
// --help is automatically included
}

let cli = CLI(options: IcloudbackupOptions.parseOrExit(), version: "1.3.1")
let cli = CLI(options: IcloudbackupOptions.parseOrExit(), version: "1.4.0")

do {
try cli.run()
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import Foundation

struct URLPair {
var placeholder: URL?
var src: URL
var dst: URL
}


struct dstDirStats {
var start: Date
Expand Down
File renamed without changes.
41 changes: 41 additions & 0 deletions icloud-backup/iclouddownload/cli.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Foundation

enum ArgumentError: Error {
case BaseUnset
case BaseDoesNotExist
}

struct CLI {
private let options: IclouddownloadOptions
private let version: String

init(options: IclouddownloadOptions, version: String) {
self.options = options
self.version = version
}

func run() throws {
if options.version == true {
print("Version " + self.version)
return
}

var baseUrl: URL?
let fileManager = FileManager.default
var isDir : ObjCBool = true

if options.base == "" {
throw ArgumentError.BaseUnset
} else {
if fileManager.fileExists(atPath: options.base, isDirectory: &isDir) {
baseUrl = URL(fileURLWithPath: options.base)
} else {
throw ArgumentError.BaseDoesNotExist
}
}

if let safeBaseUrl = baseUrl {
DownloadItems(baseURL: safeBaseUrl, verbose: options.verbose)
}
}
}
24 changes: 24 additions & 0 deletions icloud-backup/iclouddownload/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation
import ArgumentParser

struct IclouddownloadOptions: ParsableArguments {
@Flag(name: .long, help: "Print version and exit.") var version = false
@Flag(name: .long, help: "Show the paths of all downloaded files.") var verbose = false
@Option(help: ArgumentHelp("Set the base path.", valueName: "path")) var base = ""
// --help is automatically included
}

let cli = CLI(options: IclouddownloadOptions.parseOrExit(), version: "1.4.0")

do {
try cli.run()
} catch ArgumentError.BaseUnset {
fputs("Base path must be set (--base <path>)", stderr)
exit(1)
} catch ArgumentError.BaseDoesNotExist {
fputs("Base path does not exist", stderr)
exit(1)
} catch {
fputs("\(error)\n", stderr)
exit(1)
}
43 changes: 43 additions & 0 deletions icloud-backup/iclouddownload/walk.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import Foundation

func DownloadItems(baseURL: URL, verbose: Bool) {
let fileManager = FileManager.default
guard let enumerator: FileManager.DirectoryEnumerator = fileManager.enumerator(atPath: baseURL.path) else {
print("Directory not found")
return
}

while let element = enumerator.nextObject() as? String {
// Build the URL that the currently iterated over element has
var elementURL: URL = URL(fileURLWithPath: baseURL.path)
elementURL.appendPathComponent(element)

if let values = try? elementURL.resourceValues(forKeys: [.isDirectoryKey]) {
if values.isDirectory! {
// Element is a directory, they already exist locally, nothing to do
continue
} else {
// Element is a file
if fileIsPlaceholder(url: elementURL) {
// Element is an offloaded file, download it, do not wait for download to finish
if verbose {
print(" Downloading \(elementURL.path)")
}
TriggerDownloadFromCloud(placeholder: elementURL)
} else {
// Elemint is a .DS_Store file
if elementURL.lastPathComponent == ".DS_Store" {
if verbose {
print(" Deleting \(elementURL.path)")
}
DeleteItems(items: [elementURL])
continue
}

// Element is an already downloaded file, nothing to do
continue
}
}
}
}
}
41 changes: 41 additions & 0 deletions icloud-backup/icloudoffload/cli.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import Foundation

enum ArgumentError: Error {
case BaseUnset
case BaseDoesNotExist
}

struct CLI {
private let options: IcloudoffloadOptions
private let version: String

init(options: IcloudoffloadOptions, version: String) {
self.options = options
self.version = version
}

func run() throws {
if options.version == true {
print("Version " + self.version)
return
}

var baseUrl: URL?
let fileManager = FileManager.default
var isDir : ObjCBool = true

if options.base == "" {
throw ArgumentError.BaseUnset
} else {
if fileManager.fileExists(atPath: options.base, isDirectory: &isDir) {
baseUrl = URL(fileURLWithPath: options.base)
} else {
throw ArgumentError.BaseDoesNotExist
}
}

if let safeBaseUrl = baseUrl {
OffloadItems(baseURL: safeBaseUrl, verbose: options.verbose)
}
}
}
24 changes: 24 additions & 0 deletions icloud-backup/icloudoffload/main.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import Foundation
import ArgumentParser

struct IcloudoffloadOptions: ParsableArguments {
@Flag(name: .long, help: "Print version and exit.") var version = false
@Flag(name: .long, help: "Show the paths of all offloaded files.") var verbose = false
@Option(help: ArgumentHelp("Set the base path.", valueName: "path")) var base = ""
// --help is automatically included
}

let cli = CLI(options: IcloudoffloadOptions.parseOrExit(), version: "1.4.0")

do {
try cli.run()
} catch ArgumentError.BaseUnset {
fputs("Base path must be set (--base <path>)", stderr)
exit(1)
} catch ArgumentError.BaseDoesNotExist {
fputs("Base path does not exist", stderr)
exit(1)
} catch {
fputs("\(error)\n", stderr)
exit(1)
}
Loading

0 comments on commit 1fee3c6

Please sign in to comment.