lockFile attempts to create the window geometry lock file
()
| 76 | |
| 77 | // lockFile attempts to create the window geometry lock file |
| 78 | func (ws *windowGeometrySaver) lockFile() error { |
| 79 | pdir := TheApp.CogentCoreDataDir() |
| 80 | pnm := filepath.Join(pdir, ws.filename+".lck") |
| 81 | for rep := 0; rep < 10; rep++ { |
| 82 | if _, err := os.Stat(pnm); os.IsNotExist(err) { |
| 83 | b, _ := time.Now().MarshalJSON() |
| 84 | err = os.WriteFile(pnm, b, 0644) |
| 85 | if err == nil { |
| 86 | return nil |
| 87 | } |
| 88 | } |
| 89 | b, err := os.ReadFile(pnm) |
| 90 | if err != nil { |
| 91 | time.Sleep(ws.lockSleep) |
| 92 | continue |
| 93 | } |
| 94 | var lts time.Time |
| 95 | err = lts.UnmarshalJSON(b) |
| 96 | if err != nil { |
| 97 | time.Sleep(ws.lockSleep) |
| 98 | continue |
| 99 | } |
| 100 | if time.Since(lts) > 1*time.Second { |
| 101 | // log.Printf("WindowGeometry: lock file stale: %v\n", lts.String()) |
| 102 | os.Remove(pnm) |
| 103 | continue |
| 104 | } |
| 105 | // log.Printf("WindowGeometry: waiting for lock file: %v\n", lts.String()) |
| 106 | time.Sleep(ws.lockSleep) |
| 107 | } |
| 108 | return errors.New("WinGeom could not lock lock file") |
| 109 | } |
| 110 | |
| 111 | // UnLockFile unlocks the window geometry lock file (just removes it) |
| 112 | func (ws *windowGeometrySaver) unlockFile() { |
no test coverage detected