mirror of
https://github.com/gilbertchen/duplicacy
synced 2025-12-06 00:03:38 +00:00
Cleaned up formatting
Fixed tabbing
This commit is contained in:
@@ -23,19 +23,19 @@ var snapshotDate string
|
|||||||
|
|
||||||
// Converts char array to string
|
// Converts char array to string
|
||||||
func CharsToString(ca []int8) string {
|
func CharsToString(ca []int8) string {
|
||||||
|
|
||||||
len := len(ca)
|
len := len(ca)
|
||||||
ba := make([]byte, len)
|
ba := make([]byte, len)
|
||||||
|
|
||||||
for i, v := range ca {
|
for i, v := range ca {
|
||||||
ba[i] = byte(v)
|
ba[i] = byte(v)
|
||||||
if ba[i] == 0 {
|
if ba[i] == 0 {
|
||||||
len = i
|
len = i
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return string(ba[:len])
|
return string(ba[:len])
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get ID of device containing path
|
// Get ID of device containing path
|
||||||
@@ -51,43 +51,43 @@ func GetPathDeviceId(path string) (deviceId int32, err error) {
|
|||||||
// Executes shell command with timeout and returns stdout
|
// Executes shell command with timeout and returns stdout
|
||||||
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)
|
||||||
return output, err;
|
return output, err
|
||||||
}
|
}
|
||||||
|
|
||||||
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) {
|
||||||
@@ -95,7 +95,7 @@ func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadow
|
|||||||
if !shadowCopy {
|
if !shadowCopy {
|
||||||
return top
|
return top
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check repository filesystem is APFS
|
// Check repository filesystem is APFS
|
||||||
stat := syscall.Statfs_t{}
|
stat := syscall.Statfs_t{}
|
||||||
err := syscall.Statfs(top, &stat)
|
err := syscall.Statfs(top, &stat)
|
||||||
@@ -107,7 +107,7 @@ func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadow
|
|||||||
LOG_WARN("VSS_INIT", "VSS requires APFS filesystem")
|
LOG_WARN("VSS_INIT", "VSS requires APFS filesystem")
|
||||||
return top
|
return top
|
||||||
}
|
}
|
||||||
|
|
||||||
// Check path is local as tmutil snapshots will not support APFS formatted external drives
|
// Check path is local as tmutil snapshots will not support APFS formatted external drives
|
||||||
deviceIdLocal, err := GetPathDeviceId("/")
|
deviceIdLocal, err := GetPathDeviceId("/")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -125,26 +125,26 @@ func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadow
|
|||||||
}
|
}
|
||||||
|
|
||||||
if timeoutInSeconds <= 60 {
|
if timeoutInSeconds <= 60 {
|
||||||
timeoutInSeconds = 60
|
timeoutInSeconds = 60
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create mount point
|
// Create mount point
|
||||||
snapshotPath, err = ioutil.TempDir("/tmp/", "snp_")
|
snapshotPath, 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
|
||||||
}
|
}
|
||||||
|
|
||||||
// Use tmutil to create snapshot
|
// Use tmutil to create snapshot
|
||||||
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)
|
LOG_ERROR("VSS_CREATE", "Error while calling tmutil: ", err)
|
||||||
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: ", tmutilOutput)
|
LOG_ERROR("VSS_CREATE", "Snapshot creation failed: ", tmutilOutput)
|
||||||
return top
|
return top
|
||||||
}
|
}
|
||||||
snapshotDate = strings.TrimSpace(tmutilOutput[colonPos+1:])
|
snapshotDate = strings.TrimSpace(tmutilOutput[colonPos+1:])
|
||||||
@@ -153,11 +153,11 @@ func CreateShadowCopy(top string, shadowCopy bool, timeoutInSeconds int) (shadow
|
|||||||
_, err = CommandWithTimeout(timeoutInSeconds,
|
_, err = CommandWithTimeout(timeoutInSeconds,
|
||||||
"mount", "-t", "apfs", "-o", "nobrowse,-r,-s=com.apple.TimeMachine." + snapshotDate, "/", snapshotPath)
|
"mount", "-t", "apfs", "-o", "nobrowse,-r,-s=com.apple.TimeMachine." + snapshotDate, "/", snapshotPath)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
LOG_ERROR("VSS_CREATE", "Error while mounting snapshot: ", err)
|
LOG_ERROR("VSS_CREATE", "Error while mounting snapshot: ", err)
|
||||||
return top
|
return 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
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user