/*
goredo -- djb's redo implementation on pure Go
-Copyright (C) 2020-2021 Sergey Matveev <stargrave@stargrave.org>
+Copyright (C) 2020-2022 Sergey Matveev <stargrave@stargrave.org>
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
OODTgts map[string]struct{}
FdOODTgts *os.File
FdOODTgtsLock *os.File
+
+ ErrMissingTarget = errors.New("invalid format of .rec: missing Target")
)
type TgtError struct {
for _, m := range depInfo.ifchanges {
dep := m["Target"]
if dep == "" {
- return ood, TgtError{tgtOrig, errors.New("invalid format of .rec: missing Target")}
+ return ood, TgtError{tgtOrig, ErrMissingTarget}
}
theirInode, err := inodeFromRec(m)
if err != nil {
theirHsh := m["Hash"]
tracef(CDebug, "ood: %s%s -> %s: checking", indent, tgtOrig, dep)
- fd, err := os.Open(path.Join(cwd, dep))
+ inode, err := inodeFromFileByPath(path.Join(cwd, dep))
if err != nil {
if os.IsNotExist(err) {
tracef(CDebug, "ood: %s%s -> %s: not exists", indent, tgtOrig, dep)
}
return ood, TgtError{tgtOrig, err}
}
- defer fd.Close()
- inode, err := inodeFromFile(fd)
- if err != nil {
- return ood, TgtError{tgtOrig, err}
- }
if inode.Size != theirInode.Size {
tracef(CDebug, "ood: %s%s -> %s: size differs", indent, tgtOrig, dep)
ood = true
tracef(CDebug, "ood: %s%s -> %s: same inode", indent, tgtOrig, dep)
} else {
tracef(CDebug, "ood: %s%s -> %s: inode differs", indent, tgtOrig, dep)
+ fd, err := os.Open(path.Join(cwd, dep))
+ if err != nil {
+ return ood, TgtError{tgtOrig, err}
+ }
hsh, err := fileHash(fd)
+ fd.Close()
if err != nil {
return ood, TgtError{tgtOrig, err}
}
}
tracef(CDebug, "ood: %s%s -> %s: same hash", indent, tgtOrig, dep)
}
- fd.Close() // optimization not to hold it for long
if dep == tgt {
tracef(CDebug, "ood: %s%s -> %s: same target", indent, tgtOrig, dep)
return ood, err
}
RecordOODTgt:
- if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
+ flock := unix.Flock_t{
+ Type: unix.F_WRLCK,
+ Whence: io.SeekStart,
+ }
+ if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
log.Fatalln(err)
}
if _, err = FdOODTgts.Seek(0, io.SeekEnd); err != nil {
if _, err := FdOODTgts.WriteString(p + "\x00"); err != nil {
log.Fatalln(err)
}
- if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN); err != nil {
+ flock.Type = unix.F_UNLCK
+ if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
log.Fatalln(err)
}
return true, nil
func oodTgtsClear() {
var err error
- if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_EX); err != nil {
+ flock := unix.Flock_t{
+ Type: unix.F_WRLCK,
+ Whence: io.SeekStart,
+ }
+ if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLKW, &flock); err != nil {
log.Fatalln(err)
}
if err = FdOODTgts.Truncate(0); err != nil {
log.Fatalln(err)
}
- if err = unix.Flock(int(FdOODTgtsLock.Fd()), unix.LOCK_UN); err != nil {
+ flock.Type = unix.F_UNLCK
+ if err = unix.FcntlFlock(FdOODTgtsLock.Fd(), unix.F_SETLK, &flock); err != nil {
log.Fatalln(err)
}
}