From a0cf1dea761fedbf60c0b556d64fb474923e7c57 Mon Sep 17 00:00:00 2001 From: 9seconds Date: Sun, 2 Nov 2014 09:42:28 +0300 Subject: [PATCH] Fix for invalid cross-device link (issue #3) --- app/commands/tee.go | 7 +++++-- app/environments/environments.go | 8 ++++++++ main.go | 3 +++ 3 files changed, 16 insertions(+), 2 deletions(-) diff --git a/app/commands/tee.go b/app/commands/tee.go index e33e39d..7ced20b 100644 --- a/app/commands/tee.go +++ b/app/commands/tee.go @@ -17,7 +17,7 @@ import ( // Tee implements t (trace, tee) command. func Tee(input string, interactive bool, pseudoTTY bool, env *environments.Environment) { - output, err := ioutil.TempFile(os.TempDir(), "ah") + output, err := ioutil.TempFile(env.GetTmpDir(), "ah") if err != nil { utils.Logger.Panic("Cannot create temporary file") } @@ -35,9 +35,12 @@ func Tee(input string, interactive bool, pseudoTTY bool, env *environments.Envir output.Close() if hash, err := getPreciseHash(input, env); err == nil { - err = os.Rename(output.Name(), env.GetTraceFileName(hash)) + filename := env.GetTraceFileName(hash) + err = os.Rename(output.Name(), filename) if err != nil { utils.Logger.Errorf("Cannot save trace: %v. Get it here: %s", err, output.Name()) + } else { + os.Remove(filename) } } else { utils.Logger.Errorf("Error occured on fetching command number: %v", err) diff --git a/app/environments/environments.go b/app/environments/environments.go index 2fc37c5..cc35094 100644 --- a/app/environments/environments.go +++ b/app/environments/environments.go @@ -19,6 +19,7 @@ const ( defaultAppDir = ".ah" tracesDir = "traces" bookmarksDir = "bookmarks" + defaultTmpDir = "tmp" defaultZshHistFile = ".zsh_history" defaultBashHistFile = ".bash_history" @@ -58,6 +59,13 @@ func (e *Environment) OK() bool { return e.appDir != "" && e.histFile != "" && e.shell != "" } +// GetTmpDir returns a path to temporary directory +// For the reason why it is required, please see this: +// https://github.com/9seconds/ah/issues/3 +func (e *Environment) GetTmpDir() string { + return filepath.Join(e.appDir, defaultTmpDir) +} + // GetTracesDir returns an absolute path for the directory where traces should be stored. func (e *Environment) GetTracesDir() string { return filepath.Join(e.appDir, tracesDir) diff --git a/main.go b/main.go index 4c74031..4546f03 100644 --- a/main.go +++ b/main.go @@ -133,6 +133,9 @@ func main() { utils.Logger.WithFields(logrus.Fields{ "error": os.MkdirAll(env.GetBookmarksDir(), 0777), }).Info("Create bookmarks dir") + utils.Logger.WithFields(logrus.Fields{ + "error": os.MkdirAll(env.GetTmpDir(), 0777), + }).Info("Create create temporary dir") var exec executor switch {