-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
145 additions
and
1 deletion.
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 |
---|---|---|
@@ -1 +1,5 @@ | ||
# overlay | ||
# overlay | ||
|
||
Simple command to overlay directory. | ||
|
||
Useful to test things and then discard all changes at the directory. |
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,98 @@ | ||
package main | ||
|
||
import ( | ||
"flag" | ||
"log" | ||
"os" | ||
"os/signal" | ||
"syscall" | ||
"time" | ||
) | ||
|
||
func getOrElse(env, def string) string { | ||
value := os.Getenv(env) | ||
if value == "" { | ||
return def | ||
} | ||
return value | ||
} | ||
|
||
// Overlaper has the methods to overlap a directory and discard the changes | ||
type Overlaper interface { | ||
Overlap() error | ||
Discard() error | ||
} | ||
|
||
func waitForClosure(timeout time.Duration) chan struct{} { | ||
waitTimeout := timeout | ||
if timeout == 0 { | ||
waitTimeout = time.Hour * 24 | ||
} | ||
signalChan := make(chan os.Signal) | ||
signal.Notify(signalChan, os.Interrupt, os.Kill, syscall.SIGTERM) | ||
|
||
out := make(chan struct{}) | ||
go func() { | ||
for { | ||
select { | ||
case <-time.After(waitTimeout): | ||
log.Printf("Timeout reached\n") | ||
if timeout > 0 { | ||
out <- struct{}{} | ||
} | ||
case s := <-signalChan: | ||
log.Printf("Received signal: %s\n", s) | ||
out <- struct{}{} | ||
} | ||
} | ||
}() | ||
return out | ||
} | ||
|
||
func main() { | ||
var timeout string | ||
var path string | ||
var leave bool | ||
|
||
flag.StringVar(&timeout, "t", getOrElse("OVERLAP_DURATION", "0s"), "Time to overlay (0 = infinity)") | ||
flag.BoolVar(&leave, "l", false, "Leave the directory overlapped and exit") | ||
flag.Parse() | ||
|
||
if args := flag.Args(); len(args) != 1 { | ||
log.Fatalf("Only one argument must be provided") | ||
} else { | ||
path = args[0] | ||
} | ||
|
||
durationTimeout, err := time.ParseDuration(timeout) | ||
if err != nil { | ||
log.Fatalf("Duration provided isn't valid: %s\n", err) | ||
} | ||
|
||
o := OverlayFS{Directory: path} | ||
if err = o.Overlap(); err != nil { | ||
log.Fatalf("Error overlapping directory: %s\n", err) | ||
} | ||
log.Printf("Directory %s overlapped correctly\n", path) | ||
|
||
if !leave { | ||
out := waitForClosure(durationTimeout) | ||
for { | ||
select { | ||
case <-out: | ||
if err := o.Discard(); err != nil { | ||
log.Printf("Error discarding directory: %s\n", err) | ||
go func() { | ||
retryTime := time.Second * 5 | ||
log.Printf("Retrying in %s\n", retryTime) | ||
time.Sleep(retryTime) | ||
out <- struct{}{} | ||
}() | ||
} else { | ||
log.Printf("Changes to directory %s discarded\n", path) | ||
return | ||
} | ||
} | ||
} | ||
} | ||
} |
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,42 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"io/ioutil" | ||
"os" | ||
"syscall" | ||
) | ||
|
||
type OverlayFS struct { | ||
Directory string | ||
upperdir string | ||
workdir string | ||
} | ||
|
||
func (o *OverlayFS) Overlap() error { | ||
file, err := os.Open(o.Directory) | ||
if err != nil { | ||
return fmt.Errorf("Can't open directory: %s", err) | ||
} | ||
fileInfo, err := file.Stat() | ||
if err != nil { | ||
return fmt.Errorf("Can't open directory: %s", err) | ||
} | ||
stats := fileInfo.Sys().(*syscall.Stat_t) | ||
|
||
o.upperdir, _ = ioutil.TempDir("", "shadowud") | ||
o.workdir, _ = ioutil.TempDir("", "shadowwd") | ||
|
||
if err := syscall.Mount("overlay", o.Directory, "overlay", uintptr(0), fmt.Sprintf("lowerdir=%s,upperdir=%s,workdir=%s", o.Directory, o.upperdir, o.workdir)); err != nil { | ||
return fmt.Errorf("Can't mount directory: %s", err) | ||
} | ||
os.Chown(o.Directory, int(stats.Uid), int(stats.Gid)) | ||
|
||
return nil | ||
} | ||
|
||
func (o *OverlayFS) Discard() error { | ||
defer os.RemoveAll(o.upperdir) | ||
defer os.RemoveAll(o.workdir) | ||
return syscall.Unmount(o.Directory, 0) | ||
} |