Use a helper to be more sure that temp files are cleaned up at exit.

geesefs-0-30-9
Aaron Jacobs 2015-03-24 10:59:06 +11:00
parent b5e00243ba
commit 5a7e50daa6
2 changed files with 57 additions and 13 deletions

50
fsutil/fsutil.go Normal file
View File

@ -0,0 +1,50 @@
// Copyright 2015 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package fsutil
import (
"fmt"
"io/ioutil"
"os"
"path"
)
// Create a temporary file with the same semantics as ioutil.TempFile, but
// ensure that it is unlinked before returning so that it does not persist
// after the process exits.
//
// Warning: this is not production-quality code, and should only be used for
// testing purposes. In particular, there is a race between creating and
// unlinking by name.
func AnonymousFile(dir string) (f *os.File, err error) {
// Choose a prefix based on the binary name.
prefix := path.Base(os.Args[0])
// Create the file.
f, err = ioutil.TempFile(dir, prefix)
if err != nil {
err = fmt.Errorf("TempFile: %v", err)
return
}
// Unlink it.
err = os.Remove(f.Name())
if err != nil {
err = fmt.Errorf("Remove: %v", err)
return
}
return
}

View File

@ -19,7 +19,6 @@ import (
"encoding/hex"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"runtime"
@ -27,6 +26,7 @@ import (
"testing"
"github.com/jacobsa/bazilfuse"
"github.com/jacobsa/fuse/fsutil"
"github.com/jacobsa/fuse/samples"
. "github.com/jacobsa/oglematchers"
. "github.com/jacobsa/ogletest"
@ -57,10 +57,10 @@ func (t *flushFSTest) setUp(
var err error
// Set up files to receive flush and fsync reports.
t.flushes, err = ioutil.TempFile("", "")
t.flushes, err = fsutil.AnonymousFile("")
AssertEq(nil, err)
t.fsyncs, err = ioutil.TempFile("", "")
t.fsyncs, err = fsutil.AnonymousFile("")
AssertEq(nil, err)
// Set up test config.
@ -486,11 +486,8 @@ func (t *NoErrorsTest) Dup2() {
AssertEq(nil, err)
AssertEq(4, n)
// Open and unlink some temporary file.
t.f2, err = ioutil.TempFile("", "")
AssertEq(nil, err)
err = os.Remove(t.f2.Name())
// Create some anonymous temporary file.
t.f2, err = fsutil.AnonymousFile("")
AssertEq(nil, err)
// Duplicate the temporary file descriptor on top of the file from our file
@ -693,11 +690,8 @@ func (t *FlushErrorTest) Dup2() {
t.f1, err = os.OpenFile(path.Join(t.Dir, "foo"), os.O_WRONLY, 0)
AssertEq(nil, err)
// Open and unlink some temporary file.
t.f2, err = ioutil.TempFile("", "")
AssertEq(nil, err)
err = os.Remove(t.f2.Name())
// Create some anonymous temporary file.
t.f2, err = fsutil.AnonymousFile("")
AssertEq(nil, err)
// Duplicate the temporary file descriptor on top of the file from our file