first test of lockfile subPackage

This commit is contained in:
knoxfighter 2017-10-29 21:05:14 +01:00
parent 4acb972197
commit 5703a22544
3 changed files with 108 additions and 53 deletions

105
src/lockfile/lockfile.go Normal file
View File

@ -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
}

View File

@ -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
}

View File

@ -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