| 1 |
package gitobj |
| 2 |
|
| 3 |
import ( |
| 4 |
"encoding/hex" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"io/ioutil" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
|
| 11 |
"github.com/git-lfs/gitobj/v2/errors" |
| 12 |
) |
| 13 |
|
| 14 |
// fileStorer implements the storer interface by writing to the .git/objects |
| 15 |
// directory on disc. |
| 16 |
type fileStorer struct { |
| 17 |
// root is the top level /objects directory's path on disc. |
| 18 |
root string |
| 19 |
|
| 20 |
// temp directory, defaults to os.TempDir |
| 21 |
tmp string |
| 22 |
} |
| 23 |
|
| 24 |
// NewFileStorer returns a new fileStorer instance with the given root. |
| 25 |
func newFileStorer(root, tmp string) *fileStorer { |
| 26 |
return &fileStorer{ |
| 27 |
root: root, |
| 28 |
tmp: tmp, |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// Open implements the storer.Open function, and returns a io.ReadCloser |
| 33 |
// for the given SHA. If the file does not exist, or if there was any other |
| 34 |
// error in opening the file, an error will be returned. |
| 35 |
// |
| 36 |
// It is the caller's responsibility to close the given file "f" after its use |
| 37 |
// is complete. |
| 38 |
func (fs *fileStorer) Open(sha []byte) (f io.ReadCloser, err error) { |
| 39 |
f, err = fs.open(fs.path(sha), os.O_RDONLY) |
| 40 |
if os.IsNotExist(err) { |
| 41 |
return nil, errors.NoSuchObject(sha) |
| 42 |
} |
| 43 |
return f, err |
| 44 |
} |
| 45 |
|
| 46 |
// Store implements the storer.Store function and returns the number of bytes |
| 47 |
// written, along with any error encountered in copying the given io.Reader, "r" |
| 48 |
// into the object database on disk at a path given by "sha". |
| 49 |
// |
| 50 |
// If the file could not be created, or opened, an error will be returned. |
| 51 |
func (fs *fileStorer) Store(sha []byte, r io.Reader) (n int64, err error) { |
| 52 |
path := fs.path(sha) |
| 53 |
dir := filepath.Dir(path) |
| 54 |
|
| 55 |
if stat, err := os.Stat(path); stat != nil || os.IsExist(err) { |
| 56 |
// If the file already exists, there is no work left for us to |
| 57 |
// do, since the object already exists (or there is a SHA1 |
| 58 |
// collision). |
| 59 |
_, err = io.Copy(ioutil.Discard, r) |
| 60 |
if err != nil { |
| 61 |
return 0, fmt.Errorf("discard pre-existing object data: %s", err) |
| 62 |
} |
| 63 |
|
| 64 |
return 0, nil |
| 65 |
} |
| 66 |
|
| 67 |
tmp, err := ioutil.TempFile(fs.tmp, "") |
| 68 |
if err != nil { |
| 69 |
return 0, err |
| 70 |
} |
| 71 |
|
| 72 |
n, err = io.Copy(tmp, r) |
| 73 |
if err = tmp.Close(); err != nil { |
| 74 |
return n, err |
| 75 |
} |
| 76 |
if err != nil { |
| 77 |
return n, err |
| 78 |
} |
| 79 |
|
| 80 |
// Since .git/objects partitions objects based on the first two |
| 81 |
// characters of their ASCII-encoded SHA1 object ID, ensure that |
| 82 |
// the directory exists before copying a file into it. |
| 83 |
if err = os.MkdirAll(dir, 0755); err != nil { |
| 84 |
return n, err |
| 85 |
} |
| 86 |
|
| 87 |
if err = os.Rename(tmp.Name(), path); err != nil { |
| 88 |
return n, err |
| 89 |
} |
| 90 |
|
| 91 |
return n, nil |
| 92 |
} |
| 93 |
|
| 94 |
// Root gives the absolute (fully-qualified) path to the file storer on disk. |
| 95 |
func (fs *fileStorer) Root() string { |
| 96 |
return fs.root |
| 97 |
} |
| 98 |
|
| 99 |
// Close closes the file storer. |
| 100 |
func (fs *fileStorer) Close() error { |
| 101 |
return nil |
| 102 |
} |
| 103 |
|
| 104 |
// IsCompressed returns true, because the file storer returns compressed data. |
| 105 |
func (fs *fileStorer) IsCompressed() bool { |
| 106 |
return true |
| 107 |
} |
| 108 |
|
| 109 |
// open opens a given file. |
| 110 |
func (fs *fileStorer) open(path string, flag int) (*os.File, error) { |
| 111 |
return os.OpenFile(path, flag, 0) |
| 112 |
} |
| 113 |
|
| 114 |
// path returns an absolute path on disk to the object given by the OID "sha". |
| 115 |
func (fs *fileStorer) path(sha []byte) string { |
| 116 |
encoded := hex.EncodeToString(sha) |
| 117 |
|
| 118 |
return filepath.Join(fs.root, encoded[:2], encoded[2:]) |
| 119 |
} |