1
0
mirror of https://github.com/ipfs/kubo.git synced 2025-07-01 02:30:39 +08:00

Move atomicfile to gx

License: MIT
Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
This commit is contained in:
Jakub Sztandera
2016-06-08 19:25:59 +02:00
parent 6d4975d82c
commit d5702d7b3a
9 changed files with 7 additions and 226 deletions

4
Godeps/Godeps.json generated
View File

@ -25,10 +25,6 @@
"ImportPath": "github.com/codahale/metrics",
"Rev": "7c37910bc765e705301b159683480bdd44555c91"
},
{
"ImportPath": "github.com/facebookgo/atomicfile",
"Rev": "6f117f2e7f224fb03eb5e5fba370eade6e2b90c8"
},
{
"ImportPath": "github.com/hashicorp/golang-lru",
"Rev": "253b2dc1ca8bae42c3b5b6e53dd2eab1a7551116"

View File

@ -1,24 +0,0 @@
language: go
go:
- 1.2
- 1.3
matrix:
fast_finish: true
before_install:
- go get -v code.google.com/p/go.tools/cmd/vet
- go get -v github.com/golang/lint/golint
- go get -v code.google.com/p/go.tools/cmd/cover
install:
- go install -race -v std
- go get -race -t -v ./...
- go install -race -v ./...
script:
- go vet ./...
- $HOME/gopath/bin/golint .
- go test -cpu=2 -race -v ./...
- go test -cpu=2 -covermode=atomic ./...

View File

@ -1,54 +0,0 @@
// Package atomicfile provides the ability to write a file with an eventual
// rename on Close. This allows for a file to always be in a consistent state
// and never represent an in-progress write.
package atomicfile
import (
"io/ioutil"
"os"
"path/filepath"
)
// File behaves like os.File, but does an atomic rename operation at Close.
type File struct {
*os.File
path string
}
// New creates a new temporary file that will replace the file at the given
// path when Closed.
func New(path string, mode os.FileMode) (*File, error) {
f, err := ioutil.TempFile(filepath.Dir(path), filepath.Base(path))
if err != nil {
return nil, err
}
if err := os.Chmod(f.Name(), mode); err != nil {
os.Remove(f.Name())
return nil, err
}
return &File{File: f, path: path}, nil
}
// Close the file replacing the configured file.
func (f *File) Close() error {
if err := f.File.Close(); err != nil {
return err
}
if err := os.Rename(f.Name(), f.path); err != nil {
return err
}
return nil
}
// Abort closes the file and removes it instead of replacing the configured
// file. This is useful if after starting to write to the file you decide you
// don't want it anymore.
func (f *File) Abort() error {
if err := f.File.Close(); err != nil {
return err
}
if err := os.Remove(f.Name()); err != nil {
return err
}
return nil
}

View File

@ -1,86 +0,0 @@
package atomicfile_test
import (
"bytes"
"io/ioutil"
"os"
"testing"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/facebookgo/atomicfile"
)
func test(t *testing.T, dir, prefix string) {
t.Parallel()
tmpfile, err := ioutil.TempFile(dir, prefix)
if err != nil {
t.Fatal(err)
}
name := tmpfile.Name()
if err := os.Remove(name); err != nil {
t.Fatal(err)
}
defer os.Remove(name)
f, err := atomicfile.New(name, os.FileMode(0666))
if err != nil {
t.Fatal(err)
}
f.Write([]byte("foo"))
if _, err := os.Stat(name); !os.IsNotExist(err) {
t.Fatal("did not expect file to exist")
}
if err := f.Close(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(name); err != nil {
t.Fatalf("expected file to exist: %s", err)
}
}
func TestCurrentDir(t *testing.T) {
cwd, _ := os.Getwd()
test(t, cwd, "atomicfile-current-dir-")
}
func TestRootTmpDir(t *testing.T) {
test(t, "/tmp", "atomicfile-root-tmp-dir-")
}
func TestDefaultTmpDir(t *testing.T) {
test(t, "", "atomicfile-default-tmp-dir-")
}
func TestAbort(t *testing.T) {
contents := []byte("the answer is 42")
t.Parallel()
tmpfile, err := ioutil.TempFile("", "atomicfile-abort-")
if err != nil {
t.Fatal(err)
}
name := tmpfile.Name()
if _, err := tmpfile.Write(contents); err != nil {
t.Fatal(err)
}
defer os.Remove(name)
f, err := atomicfile.New(name, os.FileMode(0666))
if err != nil {
t.Fatal(err)
}
f.Write([]byte("foo"))
if err := f.Abort(); err != nil {
t.Fatal(err)
}
if _, err := os.Stat(name); err != nil {
t.Fatalf("expected file to exist: %s", err)
}
actual, err := ioutil.ReadFile(name)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(contents, actual) {
t.Fatalf(`did not find expected "%s" instead found "%s"`, contents, actual)
}
}

View File

@ -1,30 +0,0 @@
BSD License
For atomicfile software
Copyright (c) 2014, Facebook, Inc. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
* Neither the name Facebook nor the names of its contributors may be used to
endorse or promote products derived from this software without specific
prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View File

@ -1,23 +0,0 @@
Additional Grant of Patent Rights
"Software" means the atomicfile software distributed by Facebook, Inc.
Facebook hereby grants you a perpetual, worldwide, royalty-free, non-exclusive,
irrevocable (subject to the termination provision below) license under any
rights in any patent claims owned by Facebook, to make, have made, use, sell,
offer to sell, import, and otherwise transfer the Software. For avoidance of
doubt, no license is granted under Facebooks rights in any patent claims that
are infringed by (i) modifications to the Software made by you or a third party,
or (ii) the Software in combination with any software or other technology
provided by you or a third party.
The license granted hereunder will terminate, automatically and without notice,
for anyone that makes any claim (including by filing any lawsuit, assertion or
other action) alleging (a) direct, indirect, or contributory infringement or
inducement to infringe any patent: (i) by Facebook or any of its subsidiaries or
affiliates, whether or not such claim is related to the Software, (ii) by any
party if such claim arises in whole or in part from any software, product or
service of Facebook or any of its subsidiaries or affiliates, whether or not
such claim is related to the Software, or (iii) by any party relating to the
Software; or (b) that any right in any patent claim of Facebook is invalid or
unenforceable.

View File

@ -1,4 +0,0 @@
atomicfile [![Build Status](https://secure.travis-ci.org/facebookgo/atomicfile.png)](http://travis-ci.org/facebookgo/atomicfile)
==========
Documentation: http://godoc.org/github.com/facebookgo/atomicfile

View File

@ -117,6 +117,12 @@
"hash": "QmPSBJL4momYnE7DcUyk2DVhD6rH488ZmHBGLbxNdhU44K",
"name": "go-humanize",
"version": "0.0.0"
},
{
"author": "facebookgo",
"hash": "QmdYwCmx8pZRkzdcd8MhmLJqYVoVTC1aGsy5Q4reMGLNLg",
"name": "atomicfile",
"version": "0.0.0"
}
],
"gxVersion": "0.4.0",

View File

@ -8,10 +8,10 @@ import (
"os"
"path/filepath"
"github.com/ipfs/go-ipfs/Godeps/_workspace/src/github.com/facebookgo/atomicfile"
"github.com/ipfs/go-ipfs/repo/config"
"gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
logging "gx/ipfs/QmaDNZ4QMdBdku1YZWBysufYyoQt1negQGNav6PLYarbY8/go-log"
"gx/ipfs/QmdYwCmx8pZRkzdcd8MhmLJqYVoVTC1aGsy5Q4reMGLNLg/atomicfile"
)
var log = logging.Logger("fsrepo")