goduf-byinode_unix.go
author Mikael Berthe <mikael@lilotux.net>
Wed, 23 Feb 2022 22:56:53 +0100
changeset 45 ea6a9ba7a3c8
parent 18 5219596f5c71
permissions -rw-r--r--
Display existing hard links in result sets This is a breaking change in the plain text output, but somehow the list displayed in case of existing hard links was arbitrary, since the all the hardlinked filenames were not displayed. Here's a sample JSON result with this patch: { "file_size": 9216, "paths": [ "test_tree/f09-1_5.raw", "test_tree/f09-4_5.raw" ], "links": { "test_tree/f09-1_5.raw": [ "test_tree/f09-2_5.raw", "test_tree/f09-3_5.raw" ], "test_tree/f09-4_5.raw": [ "test_tree/f09-5_5.raw" ] } } Here the 5 files have the same contents, but there are two hardlink groups: "test_tree/f09-1_5.raw" "test_tree/f09-2_5.raw" "test_tree/f09-3_5.raw" are hard-linked, and "test_tree/f09-4_5.raw" "test_tree/f09-5_5.raw" are hard-linked. Here's the same set displayed With the regular text output: Group #5 (2 files * 9216 bytes): test_tree/f09-1_5.raw test_tree/f09-2_5.raw test_tree/f09-3_5.raw test_tree/f09-4_5.raw test_tree/f09-5_5.raw (The link file names are indented using 1 space character.)
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
18
5219596f5c71 Change comments so that build constraints still work
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     1
//
5219596f5c71 Change comments so that build constraints still work
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     2
// Copyright (C) 2014 Mikael Berthe <mikael@lilotux.net>
5219596f5c71 Change comments so that build constraints still work
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     3
//
5219596f5c71 Change comments so that build constraints still work
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     4
// This program is free software; you can redistribute it and/or modify
5219596f5c71 Change comments so that build constraints still work
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     5
// it under the terms of the GNU General Public License as published by
5219596f5c71 Change comments so that build constraints still work
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     6
// the Free Software Foundation; either version 2 of the License, or (at
5219596f5c71 Change comments so that build constraints still work
Mikael Berthe <mikael@lilotux.net>
parents: 17
diff changeset
     7
// your option) any later version.
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     8
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
     9
// +build darwin dragonfly freebsd linux nacl netbsd openbsd solaris
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    10
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    11
package main
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    12
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    13
import "os"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    14
import "syscall"
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    15
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    16
// ByInode is a FileObjList type with a sort interface
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    17
type ByInode FileObjList
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    18
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    19
func (a ByInode) Len() int      { return len(a) }
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    20
func (a ByInode) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    21
func (a ByInode) Less(i, j int) bool {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    22
	// Sort by device id first
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    23
	iDevice := a[i].Sys().(*syscall.Stat_t).Dev
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    24
	jDevice := a[j].Sys().(*syscall.Stat_t).Dev
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    25
	switch {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    26
	case iDevice < jDevice:
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    27
		return true
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    28
	case iDevice > jDevice:
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    29
		return false
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    30
	}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    31
	iInode := a[i].Sys().(*syscall.Stat_t).Ino
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    32
	jInode := a[j].Sys().(*syscall.Stat_t).Ino
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    33
	return iInode < jInode
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    34
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    35
3
d08d45871171 Add descriptions to exported functions
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    36
// OSHasInodes returns true iff the O.S. uses inodes for its filesystems.
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    37
func OSHasInodes() bool {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    38
	return true
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    39
}
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    40
3
d08d45871171 Add descriptions to exported functions
Mikael Berthe <mikael@lilotux.net>
parents: 0
diff changeset
    41
// GetDevIno returns the device and inode IDs of a given file.
0
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    42
func GetDevIno(fi os.FileInfo) (uint64, uint64) {
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    43
	dev := fi.Sys().(*syscall.Stat_t).Dev
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    44
	ino := fi.Sys().(*syscall.Stat_t).Ino
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    45
	return uint64(dev), uint64(ino)
a5642cd03cef Goduf - initial version-controlled revision
Mikael Berthe <mikael@lilotux.net>
parents:
diff changeset
    46
}