Add support for uid/gid

This commit is contained in:
Anders F Björklund
2025-03-15 17:49:27 +01:00
parent cef9090210
commit e6b0b83dcb
3 changed files with 64 additions and 11 deletions
+32 -7
View File
@@ -5,19 +5,43 @@ import (
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"strconv"
"strings" "strings"
"time" "time"
"github.com/CalebQ42/squashfs" "github.com/CalebQ42/squashfs"
) )
func printEntry(root, path string, d fs.DirEntry) { func userName(uid int, numeric bool) string {
us := strconv.Itoa(uid)
if numeric {
return us
}
if u, err := user.LookupId(us); err == nil {
return u.Username
}
return us
}
func groupName(gid int, numeric bool) string {
gs := strconv.Itoa(gid)
if numeric {
return gs
}
if g, err := user.LookupGroupId(gs); err == nil {
return g.Name
}
return gs
}
func printEntry(root, path string, d fs.DirEntry, numeric bool) {
fi, _ := d.Info() fi, _ := d.Info()
sfi := fi.(squashfs.FileInfo) sfi := fi.(squashfs.FileInfo)
owner := fmt.Sprintf("%d/%d", owner := fmt.Sprintf("%s/%s",
sfi.Uid(), userName(sfi.Uid(), numeric),
sfi.Gid()) groupName(sfi.Gid(), numeric))
link := "" link := ""
if sfi.IsSymlink() { if sfi.IsSymlink() {
link = " -> " + sfi.SymlinkPath() link = " -> " + sfi.SymlinkPath()
@@ -33,6 +57,7 @@ func main() {
verbose := flag.Bool("v", false, "Verbose") verbose := flag.Bool("v", false, "Verbose")
list := flag.Bool("l", false, "List") list := flag.Bool("l", false, "List")
long := flag.Bool("ll", false, "List with attributes") long := flag.Bool("ll", false, "List with attributes")
numeric := flag.Bool("lln", false, "List with attributes and numeric ids")
ignore := flag.Bool("ip", false, "Ignore Permissions and extract all files/folders with 0755") ignore := flag.Bool("ip", false, "Ignore Permissions and extract all files/folders with 0755")
flag.Parse() flag.Parse()
if len(flag.Args()) < 2 { if len(flag.Args()) < 2 {
@@ -47,14 +72,14 @@ func main() {
if err != nil { if err != nil {
panic(err) panic(err)
} }
if *list || *long { if *list || *long || *numeric {
root := flag.Arg(1) root := flag.Arg(1)
fs.WalkDir(r, ".", func(path string, d fs.DirEntry, err error) error { fs.WalkDir(r, ".", func(path string, d fs.DirEntry, err error) error {
if err != nil { if err != nil {
panic(err) panic(err)
} }
if *long { if *long || *numeric {
printEntry(root, path, d) printEntry(root, path, d, *numeric)
} else { } else {
fmt.Println(filepath.Join(root, path)) fmt.Println(filepath.Join(root, path))
} }
+9 -1
View File
@@ -142,7 +142,15 @@ func (f *File) ReadDir(n int) ([]fs.DirEntry, error) {
// Returns the file's fs.FileInfo // Returns the file's fs.FileInfo
func (f *File) Stat() (fs.FileInfo, error) { func (f *File) Stat() (fs.FileInfo, error) {
return newFileInfo(f.b.Name, &f.b.Inode), nil uid, err := f.b.Uid(&f.r.Low)
if err != nil {
return nil, err
}
gid, err := f.b.Gid(&f.r.Low)
if err != nil {
return nil, err
}
return newFileInfo(f.b.Name, uid, gid, &f.b.Inode), nil
} }
// SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string. // SymlinkPath returns the symlink's target path. Is the File isn't a symlink, returns an empty string.
+23 -3
View File
@@ -10,6 +10,8 @@ import (
type FileInfo struct { type FileInfo struct {
name string name string
uid uint32
gid uint32
size int64 size int64
target string target string
perm uint32 perm uint32
@@ -18,14 +20,22 @@ type FileInfo struct {
} }
func (r Reader) newFileInfo(e directory.Entry) (FileInfo, error) { func (r Reader) newFileInfo(e directory.Entry) (FileInfo, error) {
i, err := r.Low.InodeFromEntry(e) b, err := r.Low.BaseFromEntry(e)
if err != nil { if err != nil {
return FileInfo{}, err return FileInfo{}, err
} }
return newFileInfo(e.Name, &i), nil uid, err := b.Uid(&r.Low)
if err != nil {
return FileInfo{}, err
}
gid, err := b.Gid(&r.Low)
if err != nil {
return FileInfo{}, err
}
return newFileInfo(e.Name, uid, gid, &b.Inode), nil
} }
func newFileInfo(name string, i *inode.Inode) FileInfo { func newFileInfo(name string, uid, gid uint32, i *inode.Inode) FileInfo {
var size int64 var size int64
var target string var target string
switch i.Type { switch i.Type {
@@ -40,6 +50,8 @@ func newFileInfo(name string, i *inode.Inode) FileInfo {
} }
return FileInfo{ return FileInfo{
name: name, name: name,
uid: uid,
gid: gid,
size: size, size: size,
target: target, target: target,
perm: uint32(i.Perm), perm: uint32(i.Perm),
@@ -52,6 +64,14 @@ func (f FileInfo) Name() string {
return f.name return f.name
} }
func (f FileInfo) Uid() int {
return int(f.uid)
}
func (f FileInfo) Gid() int {
return int(f.gid)
}
func (f FileInfo) Size() int64 { func (f FileInfo) Size() int64 {
return f.size return f.size
} }