From 822e79ddd175c2276338f80d016a62186e4e6af3 Mon Sep 17 00:00:00 2001 From: Anton Novojilov Date: Mon, 1 Feb 2016 14:56:53 +0100 Subject: [PATCH] Improved error handling + Changed name pattern of temporary files and directories --- changelog.md | 5 +++++ tmp/tmp.go | 26 +++++++++++++++++++++----- tmp/tmp_test.go | 36 +++++++++++++++++++++++++++--------- 3 files changed, 53 insertions(+), 14 deletions(-) diff --git a/changelog.md b/changelog.md index 6d880db4..7ae6f535 100644 --- a/changelog.md +++ b/changelog.md @@ -1,5 +1,10 @@ ## Changelog +#### v1.5.0 + +* `[tmp]` Improved error handling +* `[tmp]` Changed name pattern of temporary files and directories + #### v1.4.5 * `[pid]` Fixed bug with pid file creation diff --git a/tmp/tmp.go b/tmp/tmp.go index eec67e12..75eea28f 100644 --- a/tmp/tmp.go +++ b/tmp/tmp.go @@ -9,6 +9,7 @@ package tmp // ////////////////////////////////////////////////////////////////////////////////// // import ( + "fmt" "os" "path" @@ -33,12 +34,26 @@ var Dir = "/tmp" // ////////////////////////////////////////////////////////////////////////////////// // // NewTemp create new Temp structure -func NewTemp(args ...string) *Temp { - if len(args) == 0 { - return &Temp{Dir: Dir} +func NewTemp(args ...string) (*Temp, error) { + tempDir := path.Clean(Dir) + + if len(args) != 0 { + tempDir = path.Clean(args[0]) + } + + if !fsutil.IsExist(tempDir) { + return nil, fmt.Errorf("Directory %s is not exist", tempDir) + } + + if !fsutil.IsDir(tempDir) { + return nil, fmt.Errorf("%s is not a directory", tempDir) + } + + if !fsutil.IsWritable(tempDir) { + return nil, fmt.Errorf("Directory %s is not writable", tempDir) } - return &Temp{Dir: path.Clean(args[0])} + return &Temp{Dir: tempDir}, nil } // ////////////////////////////////////////////////////////////////////////////////// // @@ -110,12 +125,13 @@ func (t *Temp) Clean() { // ////////////////////////////////////////////////////////////////////////////////// // +// getTempName return name of temporary file func getTempName(dir, name string) string { var result string for { if name != "" { - result = path.Join(dir, "_"+name+"_"+rand.String(12)) + result = path.Join(dir, "_"+rand.String(12)+"_"+name) } else { result = path.Join(dir, "_tmp_"+rand.String(12)) } diff --git a/tmp/tmp_test.go b/tmp/tmp_test.go index 2f261e90..15849383 100644 --- a/tmp/tmp_test.go +++ b/tmp/tmp_test.go @@ -37,8 +37,9 @@ func (ts *TmpSuite) SetUpSuite(c *C) { } func (ts *TmpSuite) TestMk(c *C) { - t := NewTemp() + t, err := NewTemp() + c.Assert(err, IsNil) c.Assert(t, NotNil) c.Assert(t.Dir, Equals, "/tmp") @@ -46,10 +47,24 @@ func (ts *TmpSuite) TestMk(c *C) { } func (ts *TmpSuite) TestErrors(c *C) { - t := NewTemp("/") + t, err := NewTemp("/") - c.Assert(t, NotNil) - c.Assert(t.Dir, Equals, "/") + c.Assert(err, NotNil) + c.Assert(t, IsNil) + + t, err = NewTemp("/tmpz") + + c.Assert(err, NotNil) + c.Assert(t, IsNil) + + os.Create(ts.TempDir + "/test_") + + t, err = NewTemp(ts.TempDir + "/test_") + + c.Assert(err, NotNil) + c.Assert(t, IsNil) + + t = &Temp{Dir: "/"} tmpDir, err := t.MkDir("test") @@ -64,8 +79,9 @@ func (ts *TmpSuite) TestErrors(c *C) { } func (ts *TmpSuite) TestMkDir(c *C) { - t := NewTemp(ts.TempDir) + t, err := NewTemp(ts.TempDir) + c.Assert(err, IsNil) c.Assert(t, NotNil) c.Assert(t.Dir, Equals, ts.TempDir) @@ -85,8 +101,9 @@ func (ts *TmpSuite) TestMkDir(c *C) { } func (ts *TmpSuite) TestMkFile(c *C) { - t := NewTemp(ts.TempDir) + t, err := NewTemp(ts.TempDir) + c.Assert(err, IsNil) c.Assert(t, NotNil) c.Assert(t.Dir, Equals, ts.TempDir) @@ -107,15 +124,16 @@ func (ts *TmpSuite) TestMkFile(c *C) { } func (ts *TmpSuite) TestMkName(c *C) { - t := NewTemp(ts.TempDir) + t, err := NewTemp(ts.TempDir) + c.Assert(err, IsNil) c.Assert(t, NotNil) c.Assert(t.Dir, Equals, ts.TempDir) c.Assert(t.MkName(), Not(Equals), "") c.Assert(t.MkName("1234"), Not(Equals), "") - ln := len(ts.TempDir + "/_1234") + ln := len(ts.TempDir + "/") - c.Assert(t.MkName("1234")[:ln], Equals, ts.TempDir+"/_1234") + c.Assert(t.MkName("1234.json")[ln+14:], Equals, "1234.json") }