Automatically create/delete directories around file creation/removal
This commit is contained in:
parent
ccb35fc7a6
commit
6f72225b5b
@ -236,6 +236,13 @@ func ProcessRemoteEvent(globals AsinkGlobals, event *asink.Event) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
//make sure containing directory exists
|
||||
err = util.EnsureDirExists(path.Dir(absolutePath))
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
err = os.Rename(tmpfilename, absolutePath)
|
||||
if err != nil {
|
||||
err2 := os.Remove(tmpfilename)
|
||||
@ -254,7 +261,8 @@ func ProcessRemoteEvent(globals AsinkGlobals, event *asink.Event) {
|
||||
} else {
|
||||
//intentionally ignore errors in case this file has been deleted out from under us
|
||||
os.Remove(absolutePath)
|
||||
//TODO delete file hierarchy beneath this file if its the last one in its directory?
|
||||
//delete the directory previously containing this file if its the last file
|
||||
util.RecursiveRemoveEmptyDirs(path.Dir(absolutePath))
|
||||
}
|
||||
|
||||
//TODO make sure file being overwritten is either unchanged or already copied off and hashed
|
||||
|
19
util/util.go
19
util/util.go
@ -11,11 +11,16 @@ import (
|
||||
func EnsureDirExists(dir string) error {
|
||||
_, err := os.Lstat(dir)
|
||||
if err != nil {
|
||||
fi, err := os.Lstat(path.Dir(dir))
|
||||
var fi os.FileInfo
|
||||
curDir := dir
|
||||
for dir != "" && err != nil {
|
||||
curDir = path.Dir(curDir)
|
||||
fi, err = os.Lstat(curDir)
|
||||
}
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = os.Mkdir(dir, fi.Mode().Perm())
|
||||
err = os.MkdirAll(dir, fi.Mode().Perm())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
@ -23,6 +28,16 @@ func EnsureDirExists(dir string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
//TODO maybe this shouldn't fail silently?
|
||||
func RecursiveRemoveEmptyDirs(dir string) {
|
||||
var err error = nil
|
||||
curDir := dir
|
||||
for err == nil {
|
||||
err = os.Remove(curDir)
|
||||
curDir = path.Dir(curDir)
|
||||
}
|
||||
}
|
||||
|
||||
func CopyToTmp(src string, tmpdir string) (string, error) {
|
||||
infile, err := os.Open(src)
|
||||
if err != nil {
|
||||
|
Loading…
Reference in New Issue
Block a user