From 5703a22544ce96e3d7a2dc460b66a8c8fbc1b392 Mon Sep 17 00:00:00 2001 From: knoxfighter Date: Sun, 29 Oct 2017 21:05:14 +0100 Subject: [PATCH] first test of lockfile subPackage --- src/lockfile/lockfile.go | 105 +++++++++++++++++++++++++++++++++++++++ src/lockfile/main.go | 53 -------------------- src/mod_Mods.go | 3 ++ 3 files changed, 108 insertions(+), 53 deletions(-) create mode 100644 src/lockfile/lockfile.go delete mode 100644 src/lockfile/main.go diff --git a/src/lockfile/lockfile.go b/src/lockfile/lockfile.go new file mode 100644 index 0000000..dc12b12 --- /dev/null +++ b/src/lockfile/lockfile.go @@ -0,0 +1,105 @@ +package lockfile + +import ( + "sync" + "path" + "os" + "log" +) + +type FileLock struct { + m sync.Mutex + Locks map[string]Lock +} + +type Lock struct { + Read int + Write int +} + +func newLock() FileLock { + lock := FileLock{} + return lock +} + +func makeAbsolutePath(target string) string { + if path.IsAbs(target) { + return target + } + + wd, err := os.Getwd() + if err != nil { + log.Fatalf("error get working directory: %s", err) + return "" + } + + return path.Join(wd, target) +} + +func (fl *FileLock) Lock(path string) { + fl.m.Lock() + defer fl.m.Unlock() + + path = makeAbsolutePath(path) + + if fl.Locks[path].Read == 0 && fl.Locks[path].Write == 0 { + lock := fl.Locks[path] + lock.Write = 1 + fl.Locks[path] = lock + } +} + +func (fl *FileLock) Unlock(path string) { + fl.m.Lock() + defer fl.m.Unlock() + + path = makeAbsolutePath(path) + + lock := fl.Locks[path] + if lock.Read == 0 && lock.Write == 1 { + lock.Write = 0 + fl.Locks[path] = lock + } +} + +func (fl *FileLock) RLock(path string) { + fl.m.Lock() + defer fl.m.Unlock() + + path = makeAbsolutePath(path) + + lock := fl.Locks[path] + if lock.Write == 0 { + lock.Read++ + fl.Locks[path] = lock + } +} + +func (fl *FileLock) RUnlock(path string) { + fl.m.Lock() + defer fl.m.Unlock() + + path = makeAbsolutePath(path) + + lock := fl.Locks[path] + if lock.Read > 0 && lock.Write == 0 { + lock.Read-- + fl.Locks[path] = lock + } +} + +func (fl *FileLock) LockW(path string) { + return +} + +func (fl *FileLock) UnlockW(path string) { + return +} + +func (fl *FileLock) RLockW(path string) { + return +} + +func (fl *FileLock) RUnlockW(path string) { + return +} diff --git a/src/lockfile/main.go b/src/lockfile/main.go deleted file mode 100644 index d19d2f6..0000000 --- a/src/lockfile/main.go +++ /dev/null @@ -1,53 +0,0 @@ -package lockfile - -import "sync" - -type FileLock struct { - m sync.Mutex - Locks map[string]Lock -} - -type Lock struct { - Read int - Write int -} - -func newLock() FileLock { - lock := FileLock{} - return lock -} - -func (fl *FileLock) Lock(path string) { - fl.m.Lock() - if fl.Locks[ſð[æſð]ðæſ[]ðæſð[ĸ·ħæ] - defer fl.m.Unlock() - return -} - -func (fl *FileLock) Unlock(path string) { - return -} - -func (fl *FileLock) RLock(path string) { - return -} - -func (fl *FileLock) RUnlock(path string) { - return -} - -func (fl *FileLock) LockW(path string) { - return -} - -func (fl *FileLock) UnlockW(path string) { - return -} - -func (fl *FileLock) RLockW(path string) { - return -} - -func (fl *FileLock) RUnlockW(path string) { - return -} diff --git a/src/mod_Mods.go b/src/mod_Mods.go index dbe35f3..0495d1b 100644 --- a/src/mod_Mods.go +++ b/src/mod_Mods.go @@ -11,6 +11,7 @@ import ( "fmt" "io/ioutil" "path/filepath" + "lockfile" ) type Mods struct { @@ -25,6 +26,8 @@ type ModsResultList struct { ModsResult []ModsResult `json:"mods"` } +var fileLock lockfile.FileLock = lockfile.newLock() + func newMods(destination string) (Mods, error) { var err error var mods Mods