コード例 #1
0
ファイル: file_test.go プロジェクト: juju/utils
func (*fileSuite) TestMoveFile(c *gc.C) {
	d := c.MkDir()
	dest := filepath.Join(d, "foo")
	f1Name := filepath.Join(d, ".foo1")
	f2Name := filepath.Join(d, ".foo2")
	err := ioutil.WriteFile(f1Name, []byte("macaroni"), 0644)
	c.Assert(err, gc.IsNil)
	err = ioutil.WriteFile(f2Name, []byte("cheese"), 0644)
	c.Assert(err, gc.IsNil)

	ok, err := utils.MoveFile(f1Name, dest)
	c.Assert(ok, gc.Equals, true)
	c.Assert(err, gc.IsNil)

	ok, err = utils.MoveFile(f2Name, dest)
	c.Assert(ok, gc.Equals, false)
	c.Assert(err, gc.NotNil)

	contents, err := ioutil.ReadFile(dest)
	c.Assert(err, gc.IsNil)
	c.Assert(contents, gc.DeepEquals, []byte("macaroni"))
}
コード例 #2
0
ファイル: metrics.go プロジェクト: AlexisBruemmer/juju
// Close implements io.Closer.
func (f *metricFile) Close() error {
	err := f.File.Close()
	if err != nil {
		return errors.Trace(err)
	}
	ok, err := utils.MoveFile(f.Name(), f.finalName)
	if err != nil {
		// ok can be true even when there is an error completing the move, on
		// platforms that implement it in multiple steps that can fail
		// separately. POSIX for example, uses link(2) to claim the new
		// location atomically, followed by an unlink(2) to release the old
		// location.
		if !ok {
			return errors.Trace(err)
		}
		logger.Errorf("failed to remove temporary file %q: %v", f.Name(), err)
	}
	return nil
}