|
| 1 | +package filesystem |
| 2 | + |
| 3 | +import ( |
| 4 | + "io" |
| 5 | + "io/ioutil" |
| 6 | + "os" |
| 7 | + "path" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/docker/docker-registry/storagedriver" |
| 11 | +) |
| 12 | + |
| 13 | +type FilesystemDriver struct { |
| 14 | + rootDirectory string |
| 15 | +} |
| 16 | + |
| 17 | +func NewDriver(rootDirectory string) *FilesystemDriver { |
| 18 | + return &FilesystemDriver{rootDirectory} |
| 19 | +} |
| 20 | + |
| 21 | +func (d *FilesystemDriver) subPath(subPath string) string { |
| 22 | + return path.Join(d.rootDirectory, subPath) |
| 23 | +} |
| 24 | + |
| 25 | +func (d *FilesystemDriver) GetContent(path string) ([]byte, error) { |
| 26 | + contents, err := ioutil.ReadFile(d.subPath(path)) |
| 27 | + if err != nil { |
| 28 | + return nil, storagedriver.PathNotFoundError{path} |
| 29 | + } |
| 30 | + return contents, nil |
| 31 | +} |
| 32 | + |
| 33 | +func (d *FilesystemDriver) PutContent(subPath string, contents []byte) error { |
| 34 | + fullPath := d.subPath(subPath) |
| 35 | + parentDir := path.Dir(fullPath) |
| 36 | + err := os.MkdirAll(parentDir, 0755) |
| 37 | + if err != nil { |
| 38 | + return err |
| 39 | + } |
| 40 | + |
| 41 | + err = ioutil.WriteFile(fullPath, contents, 0644) |
| 42 | + return err |
| 43 | +} |
| 44 | + |
| 45 | +func (d *FilesystemDriver) ReadStream(path string, offset uint64) (io.ReadCloser, error) { |
| 46 | + file, err := os.OpenFile(d.subPath(path), os.O_RDONLY, 0644) |
| 47 | + if err != nil { |
| 48 | + return nil, err |
| 49 | + } |
| 50 | + |
| 51 | + seekPos, err := file.Seek(int64(offset), os.SEEK_SET) |
| 52 | + if err != nil { |
| 53 | + file.Close() |
| 54 | + return nil, err |
| 55 | + } else if seekPos < int64(offset) { |
| 56 | + file.Close() |
| 57 | + return nil, storagedriver.InvalidOffsetError{path, offset} |
| 58 | + } |
| 59 | + |
| 60 | + return file, nil |
| 61 | +} |
| 62 | + |
| 63 | +func (d *FilesystemDriver) WriteStream(subPath string, offset, size uint64, reader io.ReadCloser) error { |
| 64 | + defer reader.Close() |
| 65 | + |
| 66 | + resumableOffset, err := d.ResumeWritePosition(subPath) |
| 67 | + if _, pathNotFound := err.(storagedriver.PathNotFoundError); err != nil && !pathNotFound { |
| 68 | + return err |
| 69 | + } |
| 70 | + |
| 71 | + if offset > resumableOffset { |
| 72 | + return storagedriver.InvalidOffsetError{subPath, offset} |
| 73 | + } |
| 74 | + |
| 75 | + fullPath := d.subPath(subPath) |
| 76 | + parentDir := path.Dir(fullPath) |
| 77 | + err = os.MkdirAll(parentDir, 0755) |
| 78 | + if err != nil { |
| 79 | + return err |
| 80 | + } |
| 81 | + |
| 82 | + var file *os.File |
| 83 | + if offset == 0 { |
| 84 | + file, err = os.Create(fullPath) |
| 85 | + } else { |
| 86 | + file, err = os.OpenFile(fullPath, os.O_WRONLY|os.O_APPEND, 0) |
| 87 | + } |
| 88 | + |
| 89 | + if err != nil { |
| 90 | + return err |
| 91 | + } |
| 92 | + defer file.Close() |
| 93 | + |
| 94 | + buf := make([]byte, 32*1024) |
| 95 | + for { |
| 96 | + bytesRead, er := reader.Read(buf) |
| 97 | + if bytesRead > 0 { |
| 98 | + bytesWritten, ew := file.WriteAt(buf[0:bytesRead], int64(offset)) |
| 99 | + if bytesWritten > 0 { |
| 100 | + offset += uint64(bytesWritten) |
| 101 | + } |
| 102 | + if ew != nil { |
| 103 | + err = ew |
| 104 | + break |
| 105 | + } |
| 106 | + if bytesRead != bytesWritten { |
| 107 | + err = io.ErrShortWrite |
| 108 | + break |
| 109 | + } |
| 110 | + } |
| 111 | + if er == io.EOF { |
| 112 | + break |
| 113 | + } |
| 114 | + if er != nil { |
| 115 | + err = er |
| 116 | + break |
| 117 | + } |
| 118 | + } |
| 119 | + return err |
| 120 | +} |
| 121 | + |
| 122 | +func (d *FilesystemDriver) ResumeWritePosition(subPath string) (uint64, error) { |
| 123 | + fullPath := d.subPath(subPath) |
| 124 | + |
| 125 | + fileInfo, err := os.Stat(fullPath) |
| 126 | + if err != nil && !os.IsNotExist(err) { |
| 127 | + return 0, err |
| 128 | + } else if err != nil { |
| 129 | + return 0, storagedriver.PathNotFoundError{subPath} |
| 130 | + } |
| 131 | + return uint64(fileInfo.Size()), nil |
| 132 | +} |
| 133 | + |
| 134 | +func (d *FilesystemDriver) List(prefix string) ([]string, error) { |
| 135 | + prefix = strings.TrimRight(prefix, "/") |
| 136 | + fullPath := d.subPath(prefix) |
| 137 | + |
| 138 | + dir, err := os.Open(fullPath) |
| 139 | + if err != nil { |
| 140 | + return nil, err |
| 141 | + } |
| 142 | + |
| 143 | + fileNames, err := dir.Readdirnames(0) |
| 144 | + if err != nil { |
| 145 | + return nil, err |
| 146 | + } |
| 147 | + |
| 148 | + keys := make([]string, 0, len(fileNames)) |
| 149 | + for _, fileName := range fileNames { |
| 150 | + keys = append(keys, path.Join(prefix, fileName)) |
| 151 | + } |
| 152 | + |
| 153 | + return keys, nil |
| 154 | +} |
| 155 | + |
| 156 | +func (d *FilesystemDriver) Move(sourcePath string, destPath string) error { |
| 157 | + err := os.Rename(d.subPath(sourcePath), d.subPath(destPath)) |
| 158 | + return err |
| 159 | +} |
| 160 | + |
| 161 | +func (d *FilesystemDriver) Delete(subPath string) error { |
| 162 | + fullPath := d.subPath(subPath) |
| 163 | + |
| 164 | + _, err := os.Stat(fullPath) |
| 165 | + if err != nil && !os.IsNotExist(err) { |
| 166 | + return err |
| 167 | + } else if err != nil { |
| 168 | + return storagedriver.PathNotFoundError{subPath} |
| 169 | + } |
| 170 | + |
| 171 | + err = os.RemoveAll(fullPath) |
| 172 | + return err |
| 173 | +} |
0 commit comments