| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package fake |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"context" |
| 10 |
"crypto/sha256" |
| 11 |
"fmt" |
| 12 |
"io/fs" |
| 13 |
"os" |
| 14 |
"path/filepath" |
| 15 |
"runtime" |
| 16 |
"slices" |
| 17 |
"sort" |
| 18 |
"strings" |
| 19 |
"sync" |
| 20 |
"time" |
| 21 |
|
| 22 |
"golang.org/x/tools/gopls/internal/protocol" |
| 23 |
"golang.org/x/tools/internal/robustio" |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
type RelativeTo string |
| 28 |
|
| 29 |
|
| 30 |
func (r RelativeTo) AbsPath(path string) string { |
| 31 |
fp := filepath.FromSlash(path) |
| 32 |
if filepath.IsAbs(fp) { |
| 33 |
return fp |
| 34 |
} |
| 35 |
return filepath.Join(string(r), filepath.FromSlash(path)) |
| 36 |
} |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
func (r RelativeTo) RelPath(fp string) string { |
| 41 |
root := string(r) |
| 42 |
if rel, err := filepath.Rel(root, fp); err == nil && !strings.HasPrefix(rel, "..") { |
| 43 |
return filepath.ToSlash(rel) |
| 44 |
} |
| 45 |
return filepath.ToSlash(fp) |
| 46 |
} |
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
func writeFileData(path string, content []byte, rel RelativeTo) error { |
| 52 |
content = bytes.ReplaceAll(content, []byte("$SANDBOX_WORKDIR"), []byte(rel)) |
| 53 |
fp := rel.AbsPath(path) |
| 54 |
if err := os.MkdirAll(filepath.Dir(fp), 0755); err != nil { |
| 55 |
return fmt.Errorf("creating nested directory: %w", err) |
| 56 |
} |
| 57 |
backoff := 1 * time.Millisecond |
| 58 |
for { |
| 59 |
err := os.WriteFile(fp, content, 0644) |
| 60 |
if err != nil { |
| 61 |
|
| 62 |
|
| 63 |
if isWindowsErrLockViolation(err) { |
| 64 |
time.Sleep(backoff) |
| 65 |
backoff *= 2 |
| 66 |
continue |
| 67 |
} |
| 68 |
return fmt.Errorf("writing %q: %w", path, err) |
| 69 |
} |
| 70 |
return nil |
| 71 |
} |
| 72 |
} |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
var isWindowsErrLockViolation = func(error) bool { return false } |
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
type Workdir struct { |
| 82 |
RelativeTo |
| 83 |
|
| 84 |
watcherMu sync.Mutex |
| 85 |
watchers []func(context.Context, []protocol.FileEvent) |
| 86 |
|
| 87 |
fileMu sync.Mutex |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
files map[string]fileID |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
func NewWorkdir(dir string, files map[string][]byte) (*Workdir, error) { |
| 109 |
w := &Workdir{RelativeTo: RelativeTo(dir)} |
| 110 |
for name, data := range files { |
| 111 |
if err := writeFileData(name, data, w.RelativeTo); err != nil { |
| 112 |
return nil, fmt.Errorf("writing to workdir: %w", err) |
| 113 |
} |
| 114 |
} |
| 115 |
_, err := w.pollFiles() |
| 116 |
return w, err |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
type fileID struct { |
| 121 |
mtime time.Time |
| 122 |
hash string |
| 123 |
} |
| 124 |
|
| 125 |
func hashFile(data []byte) string { |
| 126 |
return fmt.Sprintf("%x", sha256.Sum256(data)) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
func (w *Workdir) RootURI() protocol.DocumentURI { |
| 132 |
return protocol.URIFromPath(string(w.RelativeTo)) |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func (w *Workdir) AddWatcher(watcher func(context.Context, []protocol.FileEvent)) { |
| 137 |
w.watcherMu.Lock() |
| 138 |
w.watchers = append(w.watchers, watcher) |
| 139 |
w.watcherMu.Unlock() |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
func (w *Workdir) URI(path string) protocol.DocumentURI { |
| 144 |
return protocol.URIFromPath(w.AbsPath(path)) |
| 145 |
} |
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
func (w *Workdir) URIToPath(uri protocol.DocumentURI) string { |
| 150 |
return w.RelPath(uri.Path()) |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
func (w *Workdir) EntireFile(path string) protocol.Location { |
| 155 |
return protocol.Location{URI: w.URI(path)} |
| 156 |
} |
| 157 |
|
| 158 |
|
| 159 |
func (w *Workdir) ReadFile(path string) ([]byte, error) { |
| 160 |
backoff := 1 * time.Millisecond |
| 161 |
for { |
| 162 |
b, err := os.ReadFile(w.AbsPath(path)) |
| 163 |
if err != nil { |
| 164 |
if runtime.GOOS == "plan9" && strings.HasSuffix(err.Error(), " exclusive use file already open") { |
| 165 |
|
| 166 |
|
| 167 |
time.Sleep(backoff) |
| 168 |
backoff *= 2 |
| 169 |
continue |
| 170 |
} |
| 171 |
return nil, err |
| 172 |
} |
| 173 |
return b, nil |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
func (w *Workdir) RegexpSearch(path string, re string) (protocol.Location, error) { |
| 180 |
content, err := w.ReadFile(path) |
| 181 |
if err != nil { |
| 182 |
return protocol.Location{}, err |
| 183 |
} |
| 184 |
mapper := protocol.NewMapper(w.URI(path), content) |
| 185 |
return regexpLocation(mapper, re) |
| 186 |
} |
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
func (w *Workdir) RemoveFile(ctx context.Context, path string) error { |
| 191 |
fp := w.AbsPath(path) |
| 192 |
if err := robustio.RemoveAll(fp); err != nil { |
| 193 |
return fmt.Errorf("removing %q: %w", path, err) |
| 194 |
} |
| 195 |
|
| 196 |
return w.CheckForFileChanges(ctx) |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
func (w *Workdir) WriteFiles(ctx context.Context, files map[string]string) error { |
| 202 |
for path, content := range files { |
| 203 |
fp := w.AbsPath(path) |
| 204 |
_, err := os.Stat(fp) |
| 205 |
if err != nil && !os.IsNotExist(err) { |
| 206 |
return fmt.Errorf("checking if %q exists: %w", path, err) |
| 207 |
} |
| 208 |
if err := writeFileData(path, []byte(content), w.RelativeTo); err != nil { |
| 209 |
return err |
| 210 |
} |
| 211 |
} |
| 212 |
return w.CheckForFileChanges(ctx) |
| 213 |
} |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
func (w *Workdir) WriteFile(ctx context.Context, path, content string) error { |
| 218 |
return w.WriteFiles(ctx, map[string]string{path: content}) |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
func (w *Workdir) RenameFile(ctx context.Context, oldPath, newPath string) error { |
| 226 |
oldAbs := w.AbsPath(oldPath) |
| 227 |
newAbs := w.AbsPath(newPath) |
| 228 |
|
| 229 |
th |
| 230 |
to |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
fallbackOk := false |
| 236 |
if filepath.Dir(oldAbs) != filepath.Dir(newAbs) { |
| 237 |
fi, err := os.Stat(oldAbs) |
| 238 |
if err == nil && !fi.Mode().IsRegular() { |
| 239 |
return &os.PathError{ |
| 240 |
Op: "RenameFile", |
| 241 |
Path: oldPath, |
| 242 |
Err: fmt.Errorf("%w: file is not regular and not in the same directory as %s", os.ErrInvalid, newPath), |
| 243 |
} |
| 244 |
} |
| 245 |
fallbackOk = true |
| 246 |
} |
| 247 |
|
| 248 |
var renameErr error |
| 249 |
const debugFallback = false |
| 250 |
if fallbackOk && debugFallback { |
| 251 |
renameErr = fmt.Errorf("%w: debugging fallback path", os.ErrInvalid) |
| 252 |
} else { |
| 253 |
renameErr = robustio.Rename(oldAbs, newAbs) |
| 254 |
} |
| 255 |
if renameErr != nil { |
| 256 |
if !fallbackOk { |
| 257 |
return renameErr |
| 258 |
} |
| 259 |
|
| 260 |
content, err := w.ReadFile(oldPath) |
| 261 |
if err != nil { |
| 262 |
|
| 263 |
return renameErr |
| 264 |
} |
| 265 |
fi, err := os.Stat(newAbs) |
| 266 |
if err == nil { |
| 267 |
if fi.IsDir() { |
| 268 |
.” |
| 269 |
|
| 270 |
return renameErr |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
if err := robustio.RemoveAll(newAbs); err != nil { |
| 276 |
|
| 277 |
return renameErr |
| 278 |
} |
| 279 |
} else if !os.IsNotExist(err) { |
| 280 |
|
| 281 |
|
| 282 |
return renameErr |
| 283 |
} |
| 284 |
if writeErr := writeFileData(newPath, content, w.RelativeTo); writeErr != nil { |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
if _, statErr := os.Stat(newAbs); !os.IsNotExist(statErr) { |
| 291 |
return writeErr |
| 292 |
} |
| 293 |
return renameErr |
| 294 |
} |
| 295 |
if err := robustio.RemoveAll(oldAbs); err != nil { |
| 296 |
|
| 297 |
|
| 298 |
robustio.RemoveAll(newAbs) |
| 299 |
return renameErr |
| 300 |
} |
| 301 |
} |
| 302 |
|
| 303 |
return w.CheckForFileChanges(ctx) |
| 304 |
} |
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
func (w *Workdir) ListFiles(dir string) ([]string, error) { |
| 309 |
absDir := w.AbsPath(dir) |
| 310 |
var paths []string |
| 311 |
if err := filepath.Walk(absDir, func(fp string, info os.FileInfo, err error) error { |
| 312 |
if err != nil { |
| 313 |
return err |
| 314 |
} |
| 315 |
if info.Mode()&(fs.ModeDir|fs.ModeSymlink) == 0 { |
| 316 |
paths = append(paths, w.RelPath(fp)) |
| 317 |
} |
| 318 |
return nil |
| 319 |
}); err != nil { |
| 320 |
return nil, err |
| 321 |
} |
| 322 |
sort.Strings(paths) |
| 323 |
return paths, nil |
| 324 |
} |
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
func (w *Workdir) CheckForFileChanges(ctx context.Context) error { |
| 329 |
evts, err := w.pollFiles() |
| 330 |
if err != nil { |
| 331 |
return err |
| 332 |
} |
| 333 |
if len(evts) == 0 { |
| 334 |
return nil |
| 335 |
} |
| 336 |
w.watcherMu.Lock() |
| 337 |
watchers := slices.Clone(w.watchers) |
| 338 |
w.watcherMu.Unlock() |
| 339 |
for _, w := range watchers { |
| 340 |
w(ctx, evts) |
| 341 |
} |
| 342 |
return nil |
| 343 |
} |
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
func (w *Workdir) pollFiles() ([]protocol.FileEvent, error) { |
| 348 |
w.fileMu.Lock() |
| 349 |
defer w.fileMu.Unlock() |
| 350 |
|
| 351 |
newFiles := make(map[string]fileID) |
| 352 |
var evts []protocol.FileEvent |
| 353 |
if err := filepath.Walk(string(w.RelativeTo), func(fp string, info os.FileInfo, err error) error { |
| 354 |
if err != nil { |
| 355 |
return err |
| 356 |
} |
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
if info.Mode()&(fs.ModeDir|fs.ModeSymlink) != 0 { |
| 361 |
return nil |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
id := fileID{mtime: info.ModTime()} |
| 370 |
if time.Since(info.ModTime()) < 2*time.Second { |
| 371 |
data, err := os.ReadFile(fp) |
| 372 |
if err != nil { |
| 373 |
return err |
| 374 |
} |
| 375 |
id.hash = hashFile(data) |
| 376 |
} |
| 377 |
path := w.RelPath(fp) |
| 378 |
newFiles[path] = id |
| 379 |
|
| 380 |
if w.files != nil { |
| 381 |
oldID, ok := w.files[path] |
| 382 |
delete(w.files, path) |
| 383 |
switch { |
| 384 |
case !ok: |
| 385 |
evts = append(evts, protocol.FileEvent{ |
| 386 |
URI: w.URI(path), |
| 387 |
Type: protocol.Created, |
| 388 |
}) |
| 389 |
case oldID != id: |
| 390 |
changed := true |
| 391 |
|
| 392 |
|
| 393 |
|
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
if oldID.mtime.Equal(id.mtime) && oldID.hash != "" && id.hash == "" { |
| 398 |
data, err := os.ReadFile(fp) |
| 399 |
if err != nil { |
| 400 |
return err |
| 401 |
} |
| 402 |
if hashFile(data) == oldID.hash { |
| 403 |
changed = false |
| 404 |
} |
| 405 |
} |
| 406 |
if changed { |
| 407 |
evts = append(evts, protocol.FileEvent{ |
| 408 |
URI: w.URI(path), |
| 409 |
Type: protocol.Changed, |
| 410 |
}) |
| 411 |
} |
| 412 |
} |
| 413 |
} |
| 414 |
|
| 415 |
return nil |
| 416 |
}); err != nil { |
| 417 |
return nil, err |
| 418 |
} |
| 419 |
|
| 420 |
|
| 421 |
for path := range w.files { |
| 422 |
evts = append(evts, protocol.FileEvent{ |
| 423 |
URI: w.URI(path), |
| 424 |
Type: protocol.Deleted, |
| 425 |
}) |
| 426 |
} |
| 427 |
w.files = newFiles |
| 428 |
return evts, nil |
| 429 |
} |