This repository has been archived by the owner on May 28, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from findy-network/enclave-backup
Enclave backup
- Loading branch information
Showing
12 changed files
with
470 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
/* | ||
Package backup implements dedicated helpers for the current backup system. | ||
*/ | ||
package backup | ||
|
||
import ( | ||
"io" | ||
"os" | ||
"path/filepath" | ||
|
||
"github.com/golang/glog" | ||
"github.com/lainio/err2" | ||
) | ||
|
||
// PrefixName builds a new prefixed file name. The file name is in format: | ||
// prefix_file.name | ||
// If prefix is empty the returned string starts with _. | ||
func PrefixName(prefix, name string) string { | ||
dir, file := filepath.Split(name) | ||
file = prefix + "_" + file | ||
backupName := filepath.Join(dir, file) | ||
glog.V(3).Infoln("backup name:", backupName) | ||
return backupName | ||
} | ||
|
||
// FileCopy copies a source file to destination file. | ||
func FileCopy(src, dst string) (err error) { | ||
defer err2.Returnf(&err, "copy %s -> %s", src, dst) | ||
|
||
r := err2.File.Try(os.Open(src)) | ||
defer r.Close() | ||
|
||
w := err2.File.Try(os.Create(dst)) | ||
defer err2.Handle(&err, func() { | ||
os.Remove(dst) | ||
}) | ||
defer w.Close() | ||
err2.Empty.Try(io.Copy(w, r)) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package backup | ||
|
||
import "testing" | ||
|
||
func TestMgd_prefixName(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
prefix string | ||
backupName string | ||
want string | ||
}{ | ||
{name: "path include", | ||
prefix: "XXXXX", | ||
backupName: "/file/path/backup.bolt", | ||
want: "/file/path/XXXXX_backup.bolt"}, | ||
{name: "no path", | ||
prefix: "XXXXX", | ||
backupName: "backup.bolt", | ||
want: "XXXXX_backup.bolt"}, | ||
{name: "no path no prefix", | ||
prefix: "XXXXX", | ||
backupName: "backup.bolt", | ||
want: "XXXXX_backup.bolt"}, | ||
{name: "path include no prefix", | ||
prefix: "", | ||
backupName: "/file/path/backup.bolt", | ||
want: "/file/path/_backup.bolt"}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if got := PrefixName(tt.prefix, tt.backupName); got != tt.want { | ||
t.Errorf("backupName() = %v, want %v", got, tt.want) | ||
} | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.