| 1 | package main |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "os" |
| 6 | "path/filepath" |
| 7 | "time" |
| 8 | |
| 9 | filelock "reasonix/internal/identitylock" |
| 10 | ) |
| 11 | |
| 12 | const desktopProjectsFileLockTimeout = 2 * time.Second |
| 13 | |
| 14 | func acquireDesktopProjectsFileLock() (func(), error) { |
| 15 | dir := desktopConfigDir() |
| 16 | if err := os.MkdirAll(dir, 0o700); err != nil { |
| 17 | return nil, err |
| 18 | } |
| 19 | ctx, cancel := context.WithTimeout(context.Background(), desktopProjectsFileLockTimeout) |
| 20 | defer cancel() |
| 21 | return filelock.Acquire(ctx, filepath.Join(dir, desktopProjectsFile)+".lock") |
| 22 | } |
| 23 | |
| 24 | // updateProjectsFileCrossProcessLocked requires desktopProjectsFileMu and the |
| 25 | // desktop-projects cross-process file lock. |
| 26 | func updateProjectsFileCrossProcessLocked(mutator func(*desktopProjectFile) (bool, error)) error { |
| 27 | f := loadProjectsFile() |
| 28 | changed, err := mutator(&f) |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | if !changed { |
| 33 | return nil |
| 34 | } |
| 35 | return saveProjectsFile(f) |
| 36 | } |
| 37 |