Cleaned up formatting

This commit is contained in:
amarcu5
2018-04-28 21:23:00 +01:00
parent 4c3d5dbc2f
commit 8a3c5847a8

View File

@@ -22,14 +22,14 @@ var snapshotDate string
func CommandWithTimeout(timeoutInSeconds int, name string, arg ...string) (output string, err error) { func CommandWithTimeout(timeoutInSeconds int, name string, arg ...string) (output string, err error) {
ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutInSeconds) * time.Second) ctx, cancel := context.WithTimeout(context.Background(), time.Duration(timeoutInSeconds) * time.Second)
defer cancel() defer cancel()
cmd := exec.CommandContext(ctx, name, arg...) cmd := exec.CommandContext(ctx, name, arg...)
out, err := cmd.Output() out, err := cmd.Output()
if ctx.Err() == context.DeadlineExceeded { if ctx.Err() == context.DeadlineExceeded {
err = errors.New("Command '" + name + "' timed out") err = errors.New("Command '" + name + "' timed out")
} }
output = string(out) output = string(out)
@@ -38,28 +38,27 @@ func CommandWithTimeout(timeoutInSeconds int, name string, arg ...string) (outpu
func DeleteShadowCopy() { func DeleteShadowCopy() {
if snapshotPath == "" { if snapshotPath == "" {
return return
} }
err := exec.Command("umount", "-f", snapshotPath).Run() err := exec.Command("umount", "-f", snapshotPath).Run()
if err != nil { if err != nil {
LOG_ERROR("VSS_DELETE", "Error while unmounting snapshot") LOG_ERROR("VSS_DELETE", "Error while unmounting snapshot")
return return
} }
err = exec.Command("tmutil", "deletelocalsnapshots", snapshotDate).Run() err = exec.Command("tmutil", "deletelocalsnapshots", snapshotDate).Run()
if err != nil { if err != nil {
LOG_ERROR("VSS_DELETE", "Error while deleting local snapshot") LOG_ERROR("VSS_DELETE", "Error while deleting local snapshot")
return return
} }
os.RemoveAll(snapshotPath) os.RemoveAll(snapshotPath)
LOG_INFO("VSS_DELETE", "Shadow copy unmounted and deleted at %s", snapshotPath) LOG_INFO("VSS_DELETE", "Shadow copy unmounted and deleted at %s", snapshotPath)
snapshotPath = ""
snapshotPath = ""
} }
func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadowTop string) { func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadowTop string) {
@@ -69,38 +68,37 @@ func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadow
} }
if timeoutInSeconds <= 60 { if timeoutInSeconds <= 60 {
timeoutInSeconds = 60 timeoutInSeconds = 60
} }
tmpDir, err := ioutil.TempDir("/tmp/", "snp_") tmpDir, err := ioutil.TempDir("/tmp/", "snp_")
if err != nil { if err != nil {
LOG_ERROR("VSS_CREATE", "Failed to create temporary mount directory") LOG_ERROR("VSS_CREATE", "Failed to create temporary mount directory")
return top return top
} }
snapshotPath = tmpDir snapshotPath = tmpDir
tmutilOutput, err := CommandWithTimeout(timeoutInSeconds, "tmutil", "snapshot", "/") tmutilOutput, err := CommandWithTimeout(timeoutInSeconds, "tmutil", "snapshot", "/")
if err != nil { if err != nil {
LOG_ERROR("VSS_CREATE", "Error while calling tmutil: " + err.Error()) LOG_ERROR("VSS_CREATE", "Error while calling tmutil: " + err.Error())
return top return top
} }
colonPos := strings.IndexByte(tmutilOutput, ':') colonPos := strings.IndexByte(tmutilOutput, ':')
if colonPos < 0 { if colonPos < 0 {
LOG_ERROR("VSS_CREATE", "Snapshot creation failed") LOG_ERROR("VSS_CREATE", "Snapshot creation failed")
return top return top
} }
snapshotDate = strings.TrimSpace(tmutilOutput[colonPos+1:]) snapshotDate = strings.TrimSpace(tmutilOutput[colonPos+1:])
_, err = CommandWithTimeout(timeoutInSeconds, "mount", "-t", "apfs", "-o", "nobrowse,-s=com.apple.TimeMachine." + snapshotDate, "/", snapshotPath) _, err = CommandWithTimeout(timeoutInSeconds,
if err != nil { "mount", "-t", "apfs", "-o", "nobrowse,-r,-s=com.apple.TimeMachine." + snapshotDate, "/", snapshotPath)
LOG_ERROR("VSS_CREATE", "Error while mounting snapshot: " + err.Error()) if err != nil {
return top LOG_ERROR("VSS_CREATE", "Error while mounting snapshot: " + err.Error())
return top
} }
LOG_DEBUG("VSS_PROPERTY", "Returned path: %s", snapshotPath + top) LOG_INFO("VSS_DONE", "Shadow copy created and mounted at %s", snapshotPath)
LOG_INFO("VSS_DONE", "Shadow copy created and mounted at %s", snapshotPath)
return snapshotPath + top return snapshotPath + top
} }