Beispiel #1
0
func main() {
	if len(os.Args) == 1 {
		log.Fatal("Argument required: <lock file>")
	}
	lockFilePath := os.Args[1]

	lockFile, err := os.OpenFile(lockFilePath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, 0600)
	if err != nil {
		log.Fatal(err)
	}
	defer lockFile.Close()

	err = lockapi.TryLockFile(lockFile)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Press ENTER to unlock...")
	fmt.Scanln()

	err = lockapi.UnlockFile(lockFile)
	if err != nil {
		log.Fatal(err)
	}

	fmt.Println("Press ENTER to end...")
	fmt.Scanln()
}
Beispiel #2
0
func (app *App) UnlockDirectory() (err error) {
	if app.lockFile == nil {
		return nil
	}

	if !caravel.FileExists(app.lockFile.Name()) {
		return nil
	}

	log.Info("Releasing the API lock...")
	err = lockapi.UnlockFile(app.lockFile)
	if err != nil {
		return err
	}
	log.Notice("Lock released")

	log.Info("Closing lock file...")
	err = app.lockFile.Close()
	if err != nil {
		return err
	}
	log.Notice("Lock file closed")

	log.Info("Deleting lock file...")
	err = os.Remove(app.lockFile.Name())
	if err != nil {
		return err
	}
	log.Notice("Lock file deleted")

	app.lockFile = nil

	return nil
}
func TestBlockingFileLockingCalls(t *testing.T) {
	lockFile, err := ioutil.TempFile(os.TempDir(), "LockAPI_TestFile")
	if err != nil {
		t.Fatal(err)
	}

	defer func() {
		lockFile.Close()
		os.Remove(lockFile.Name())
	}()

	err = lockapi.LockFile(lockFile)
	if err != nil {
		t.Fatal(err)
	}

	err = lockapi.UnlockFile(lockFile)
	if err != nil {
		t.Fatal(err)
	}
}