| 1 | package memfs |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "io/fs" |
| 6 | "os" |
| 7 | "path/filepath" |
| 8 | "strings" |
| 9 | "sync" |
| 10 | "time" |
| 11 | |
| 12 | "github.com/go-git/go-billy/v6" |
| 13 | ) |
| 14 | |
| 15 | type storage struct { |
| 16 | files map[string]*file |
| 17 | children map[string]map[string]*file |
| 18 | |
| 19 | mf sync.RWMutex |
| 20 | mc sync.RWMutex |
| 21 | } |
| 22 | |
| 23 | func newStorage() *storage { |
| 24 | return &storage{ |
| 25 | files: make(map[string]*file, 0), |
| 26 | children: make(map[string]map[string]*file, 0), |
| 27 | } |
| 28 | } |
| 29 | |
| 30 | func (s *storage) Has(path string) bool { |
| 31 | _, ok := s.get(path) |
| 32 | return ok |
| 33 | } |
| 34 | |
| 35 | func (s *storage) New(path string, mode fs.FileMode, flag int) (*file, error) { |
| 36 | path = clean(path) |
| 37 | if f, ok := s.get(path); ok { |
| 38 | if !f.mode.IsDir() { |
| 39 | return nil, fmt.Errorf("file already exists %q", path) |
| 40 | } |
| 41 | |
| 42 | return nil, nil |
| 43 | } |
| 44 | |
| 45 | name := filepath.Base(path) |
| 46 | f := &file{ |
| 47 | name: name, |
| 48 | content: &content{ |
| 49 | name: name, |
| 50 | modTime: time.Now(), |
| 51 | }, |
| 52 | mode: mode, |
| 53 | flag: flag, |
| 54 | } |
| 55 | |
| 56 | s.mf.Lock() |
| 57 | s.files[path] = f |
| 58 | s.mf.Unlock() |
| 59 | |
| 60 | err := s.createParent(path, mode, f) |
| 61 | if err != nil { |
| 62 | return nil, fmt.Errorf("failed to create parent: %w", err) |
| 63 | } |
| 64 | |
| 65 | return f, nil |
| 66 | } |
| 67 | |
| 68 | func (s *storage) createParent(path string, mode fs.FileMode, f *file) error { |
| 69 | base := filepath.Dir(path) |
| 70 | base = clean(base) |
| 71 | if f.Name() == string(separator) { |
| 72 | return nil |
| 73 | } |
| 74 | |
| 75 | if _, err := s.New(base, mode.Perm()|os.ModeDir, 0); err != nil { |
| 76 | return err |
| 77 | } |
| 78 | |
| 79 | s.mc.Lock() |
| 80 | if _, ok := s.children[base]; !ok { |
| 81 | s.children[base] = make(map[string]*file, 0) |
| 82 | } |
| 83 | |
| 84 | s.children[base][f.Name()] = f |
| 85 | s.mc.Unlock() |
| 86 | |
| 87 | return nil |
| 88 | } |
| 89 | |
| 90 | func (s *storage) Children(path string) []*file { |
| 91 | path = clean(path) |
| 92 | |
| 93 | s.mc.RLock() |
| 94 | l := make([]*file, 0, len(s.children)) |
| 95 | for _, f := range s.children[path] { |
| 96 | l = append(l, f) |
| 97 | } |
| 98 | s.mc.RUnlock() |
| 99 | |
| 100 | return l |
| 101 | } |
| 102 | |
| 103 | func (s *storage) MustGet(path string) *file { |
| 104 | f, ok := s.get(path) |
| 105 | if !ok { |
| 106 | panic(fmt.Errorf("couldn't find %q", path)) |
| 107 | } |
| 108 | |
| 109 | return f |
| 110 | } |
| 111 | |
| 112 | func (s *storage) Get(path string) (*file, bool) { |
| 113 | return s.get(path) |
| 114 | } |
| 115 | |
| 116 | func (s *storage) get(path string) (*file, bool) { |
| 117 | path = clean(path) |
| 118 | |
| 119 | s.mf.RLock() |
| 120 | file, ok := s.files[path] |
| 121 | s.mf.RUnlock() |
| 122 | if !ok { |
| 123 | return nil, false |
| 124 | } |
| 125 | |
| 126 | return file, ok |
| 127 | } |
| 128 | |
| 129 | func (s *storage) Rename(from, to string) error { |
| 130 | from = clean(from) |
| 131 | to = clean(to) |
| 132 | |
| 133 | if from == "/" || from == "." { |
| 134 | return billy.ErrBaseDirCannotBeRenamed |
| 135 | } |
| 136 | |
| 137 | if !s.Has(from) { |
| 138 | return os.ErrNotExist |
| 139 | } |
| 140 | |
| 141 | move := [][2]string{{from, to}} |
| 142 | s.mf.RLock() |
| 143 | for pathFrom := range s.files { |
| 144 | if pathFrom == from || !isPathDescendant(pathFrom, from) { |
| 145 | continue |
| 146 | } |
| 147 | |
| 148 | rel, _ := filepath.Rel(from, pathFrom) |
| 149 | pathTo := filepath.Join(to, rel) |
| 150 | |
| 151 | move = append(move, [2]string{pathFrom, pathTo}) |
| 152 | } |
| 153 | s.mf.RUnlock() |
| 154 | |
| 155 | for _, ops := range move { |
| 156 | from := ops[0] |
| 157 | to := ops[1] |
| 158 | |
| 159 | if err := s.move(from, to); err != nil { |
| 160 | return err |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | return nil |
| 165 | } |
| 166 | |
| 167 | func isPathDescendant(path, parent string) bool { |
| 168 | return strings.HasPrefix(path, parent+string(filepath.Separator)) |
| 169 | } |
| 170 | |
| 171 | func (s *storage) move(from, to string) error { |
| 172 | s.mf.Lock() |
| 173 | s.files[to] = s.files[from] |
| 174 | s.files[to].name = filepath.Base(to) |
| 175 | file := s.files[to] |
| 176 | s.mf.Unlock() |
| 177 | |
| 178 | s.mc.Lock() |
| 179 | s.children[to] = s.children[from] |
| 180 | s.mc.Unlock() |
| 181 | |
| 182 | defer func() { |
| 183 | s.mf.Lock() |
| 184 | delete(s.files, from) |
| 185 | s.mf.Unlock() |
| 186 | |
| 187 | s.mc.Lock() |
| 188 | delete(s.children, from) |
| 189 | delete(s.children[filepath.Dir(from)], filepath.Base(from)) |
| 190 | s.mc.Unlock() |
| 191 | }() |
| 192 | |
| 193 | return s.createParent(to, 0o644, file) |
| 194 | } |
| 195 | |
| 196 | func (s *storage) Remove(path string) error { |
| 197 | path = clean(path) |
| 198 | if path == "/" || path == "." { |
| 199 | return billy.ErrBaseDirCannotBeRemoved |
| 200 | } |
| 201 | |
| 202 | f, has := s.get(path) |
| 203 | if !has { |
| 204 | return os.ErrNotExist |
| 205 | } |
| 206 | |
| 207 | if f.mode.IsDir() { |
| 208 | s.mc.RLock() |
| 209 | if len(s.children[path]) != 0 { |
| 210 | s.mc.RUnlock() |
| 211 | return fmt.Errorf("dir: %s contains files", path) |
| 212 | } |
| 213 | s.mc.RUnlock() |
| 214 | } |
| 215 | |
| 216 | base, file := filepath.Split(path) |
| 217 | base = filepath.Clean(base) |
| 218 | |
| 219 | s.mf.Lock() |
| 220 | delete(s.files, path) |
| 221 | s.mf.Unlock() |
| 222 | |
| 223 | s.mc.Lock() |
| 224 | delete(s.children[base], file) |
| 225 | s.mc.Unlock() |
| 226 | return nil |
| 227 | } |
| 228 | |
| 229 | func (s *storage) Chmod(path string, mode fs.FileMode) error { |
| 230 | path = clean(path) |
| 231 | |
| 232 | f, has := s.Get(path) |
| 233 | if !has { |
| 234 | return os.ErrNotExist |
| 235 | } |
| 236 | |
| 237 | f.mode = mode |
| 238 | return nil |
| 239 | } |
| 240 | |
| 241 | func clean(path string) string { |
| 242 | return filepath.Clean(filepath.FromSlash(path)) |
| 243 | } |