func newInner()

in pkg/lockedfile/lockedfile_linux.go [17:48]


func newInner(filePath string, timeout time.Duration, metadata *Metadata) (*lockedFile, error) {
	file, err := syscall.Open(filePath, os.O_RDWR|os.O_CREATE, constants.FilePermissions_UserOnly_ReadWrite)
	if err != nil {
		// file cannot be open
		return nil, err
	}

	errChan := make(chan error)
	timeoutChan := make(chan struct{})

	go func() {
		// acquire exclusive lock on file
		err := syscall.Flock(file, syscall.LOCK_EX)
		select {
		case <-timeoutChan:
			syscall.Flock(file, syscall.LOCK_UN)
			syscall.Close(file)
		case errChan <- err:
		}
	}()

	select {
	case err := <-errChan:
		if err != nil {
			return nil, err
		}
		return &lockedFile{file, metadata}, nil
	case <-time.After(timeout):
		close(timeoutChan)
		return nil, &FileLockTimeoutError{"file lock could not be acquired in the specified time"}
	}
}