style: add blank lines between declarations and statement blocks
Separate top-level declarations and multi-line statements with blank lines, keeping an "if err != nil" check next to the statement it belongs to. Expand semicolon one-liners, group the nativeTransport fields by purpose, and format the cgo preamble as readable C. Apart from the expanded one-liners and the C preamble, the change is whitespace only. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
ec0297fd23
commit
a6fda8f8d3
25 files changed
+929
-41
No files matched your search
@@ -27,11 +27,13 @@ func openLinkIndex(path string) (*linkIndex, error) {
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
tx, err := db.Begin(true)
|
||||
if err != nil {
|
||||
db.Close()
|
||||
return nil, err
|
||||
}
|
||||
|
||||
for _, name := range []string{"source", "target"} {
|
||||
if _, err = tx.CreateBucket([]byte(name)); err != nil {
|
||||
tx.Rollback()
|
||||
@@ -39,14 +41,18 @@ func openLinkIndex(path string) (*linkIndex, error) {
|
||||
return nil, err
|
||||
}
|
||||
}
|
||||
|
||||
return &linkIndex{db: db, tx: tx}, nil
|
||||
}
|
||||
|
||||
func (x *linkIndex) close() error {
|
||||
if x.tx != nil {
|
||||
_ = x.tx.Rollback()
|
||||
}
|
||||
|
||||
return x.db.Close()
|
||||
}
|
||||
|
||||
func (x *linkIndex) observe(bucket string, key, value [16]byte) (bool, error) {
|
||||
b := x.tx.Bucket([]byte(bucket))
|
||||
old := b.Get(key[:])
|
||||
@@ -58,6 +64,7 @@ func (x *linkIndex) observe(bucket string, key, value [16]byte) (bool, error) {
|
||||
} else {
|
||||
same = bytes.Equal(old, value[:])
|
||||
}
|
||||
|
||||
x.observations++
|
||||
// Limit dirty transaction state; the complete relation stays on disk.
|
||||
if x.observations%4096 == 0 {
|
||||
@@ -66,19 +73,23 @@ func (x *linkIndex) observe(bucket string, key, value [16]byte) (bool, error) {
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
|
||||
x.tx, err = x.db.Begin(true)
|
||||
if err != nil {
|
||||
return false, err
|
||||
}
|
||||
}
|
||||
|
||||
return same, nil
|
||||
}
|
||||
|
||||
func inodeIdentity(info os.FileInfo) ([16]byte, uint64, error) {
|
||||
var key [16]byte
|
||||
st, ok := info.Sys().(*syscall.Stat_t)
|
||||
if !ok {
|
||||
return key, 0, errors.New("missing Linux inode information")
|
||||
}
|
||||
|
||||
binary.BigEndian.PutUint64(key[:8], uint64(st.Dev))
|
||||
binary.BigEndian.PutUint64(key[8:], st.Ino)
|
||||
return key, uint64(st.Nlink), nil
|
||||
@@ -93,28 +104,34 @@ func hardlinkTopology(ctx context.Context, src, dst, scratchParent string, repai
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer os.RemoveAll(scratch)
|
||||
index, err := openLinkIndex(filepath.Join(scratch, "index.bolt"))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
defer func() {
|
||||
if err := index.close(); result == nil {
|
||||
result = err
|
||||
}
|
||||
}()
|
||||
|
||||
targetPath := func(path string) (string, error) {
|
||||
rel, err := filepath.Rel(src, path)
|
||||
if err != nil || !beneath(src, path) {
|
||||
return "", fmt.Errorf("hardlink path outside source: %s", path)
|
||||
}
|
||||
|
||||
return filepath.Join(dst, rel), nil
|
||||
}
|
||||
|
||||
visitDir := func(path string) error {
|
||||
target, err := targetPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
st, err := os.Lstat(target)
|
||||
if repair && (errors.Is(err, os.ErrNotExist) || (err == nil && !st.IsDir())) {
|
||||
// Never follow a target symlink directory, even when its referent exists.
|
||||
@@ -123,19 +140,24 @@ func hardlinkTopology(ctx context.Context, src, dst, scratchParent string, repai
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !st.IsDir() {
|
||||
return fmt.Errorf("target directory changed type: %s", target)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
visitFile := func(path string, sourceInfo os.FileInfo) error {
|
||||
if !sourceInfo.Mode().IsRegular() {
|
||||
return nil
|
||||
}
|
||||
|
||||
target, err := targetPath(path)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetInfo, err := os.Lstat(target)
|
||||
if repair && errors.Is(err, os.ErrNotExist) {
|
||||
return nil
|
||||
@@ -143,44 +165,55 @@ func hardlinkTopology(ctx context.Context, src, dst, scratchParent string, repai
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !targetInfo.Mode().IsRegular() {
|
||||
if repair {
|
||||
return nil
|
||||
}
|
||||
|
||||
return fmt.Errorf("hardlink target is not a regular file: %s", target)
|
||||
}
|
||||
|
||||
sourceKey, sourceLinks, err := inodeIdentity(sourceInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
targetKey, targetLinks, err := inodeIdentity(targetInfo)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !repair && sourceLinks > 1 {
|
||||
same, err := index.observe("source", sourceKey, targetKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !same {
|
||||
return fmt.Errorf("source hardlink group was split at %s", target)
|
||||
}
|
||||
}
|
||||
|
||||
if targetLinks > 1 {
|
||||
same, err := index.observe("target", targetKey, sourceKey)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if !same {
|
||||
if !repair {
|
||||
return fmt.Errorf("unrelated source files became hardlinked at %s", target)
|
||||
}
|
||||
|
||||
if err := os.Remove(target); err != nil {
|
||||
return fmt.Errorf("break obsolete target hardlink: %w", err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
return walkTree(ctx, src, visitDir, visitFile)
|
||||
}
|
||||
Reference in new issue
Block a user