FIX:
* fs.FileMode for object type is 0 for regular files, so an additional
  parameter is needed.
This commit is contained in:
brent saner 2024-11-12 06:50:44 -05:00
parent 903dd00c81
commit b82f0c02ed
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
2 changed files with 37 additions and 29 deletions

View File

@ -1,20 +1,20 @@
package paths package paths


import ( import (
`io/fs` "io/fs"
) )


// Mostly just for reference. // Mostly just for reference.
const ( const (
// ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular // ModeDir | ModeSymlink | ModeNamedPipe | ModeSocket | ModeDevice | ModeCharDevice | ModeIrregular
modeDir pathMode = pathMode(fs.ModeDir) modeDir pathMode = pathMode(fs.ModeDir)
modeSymlink pathMode = pathMode(fs.ModeSymlink) modeSymlink pathMode = pathMode(fs.ModeSymlink)
modePipe pathMode = pathMode(fs.ModeNamedPipe) modePipe pathMode = pathMode(fs.ModeNamedPipe)
modeSocket pathMode = pathMode(fs.ModeSocket) modeSocket pathMode = pathMode(fs.ModeSocket)
modeDev pathMode = pathMode(fs.ModeDevice) modeDev pathMode = pathMode(fs.ModeDevice)
modeCharDev pathMode = pathMode(fs.ModeCharDevice) modeCharDev pathMode = pathMode(fs.ModeCharDevice)
modeIrregular pathMode = pathMode(fs.ModeIrregular) modeIrregular pathMode = pathMode(fs.ModeIrregular)
modeAny pathMode = modeDir | modeSymlink | modePipe | modeSocket | modeDev | modeCharDev | modeIrregular modeAnyExceptRegular pathMode = modeDir | modeSymlink | modePipe | modeSocket | modeDev | modeCharDev | modeIrregular
) )


// Times // Times

View File

@ -280,13 +280,15 @@ func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error)
targetType defines what should be included in the path list. targetType defines what should be included in the path list.
It can consist of one or more (io/)fs.FileMode types OR'd together It can consist of one or more (io/)fs.FileMode types OR'd together
(ensure they are part of (io/)fs.ModeType). (ensure they are part of (io/)fs.ModeType).
(You can use 0 as a shortcut to match anything/all paths. (You can use 0 to match regular files explicitly, and/or noFiles = true to exclude them.)
You can also use (io/)fs.ModeType itself to match anything/all paths.)
noFiles, if true, will explicitly filter out regular files from the path results.
(Normally they are *always* included regardless of targetType.)


basePtrn may be nil; if it isn't, it will be applied to *base names* basePtrn may be nil; if it isn't, it will be applied to *base names*
(that is, quux.txt rather than /foo/bar/baz/quux.txt). (that is, quux.txt rather than /foo/bar/baz/quux.txt).


pathPtrn is like ptrn except it applies to the *entire* path, pathPtrn is like basePtrn except it applies to the *entire* path,
not just the basename, if not nil (e.g. /foo/bar/baz/quux.txt, not just the basename, if not nil (e.g. /foo/bar/baz/quux.txt,
not just quux.txt). not just quux.txt).


@ -297,17 +299,22 @@ func RealPathExistsStat(path *string) (exists bool, stat os.FileInfo, err error)


ageType is one or more Time* constants OR'd together to describe which timestamp type to check. ageType is one or more Time* constants OR'd together to describe which timestamp type to check.
(Note that TimeCreated may not match if specified as it is only available on certain OSes, (Note that TimeCreated may not match if specified as it is only available on certain OSes,
kernel versions, and filesystems. This would lead to files being excluded that may have otherwise kernel versions, and filesystems. This may lead to files being excluded that may have otherwise
been included.) been included.)
(You can use TimeAny to specify any supported time.) (You can use TimeAny to specify any supported time.)
*Any* matching timestamp of all specified (and supported) timestamp types matches, *Any* matching timestamp of all specified (and supported) timestamp types matches,
so be judicious with your selection. so be judicious with your selection. They are processed in order of:

* btime (birth/creation time) (if supported)
* mtime (modification time -- contents have changed)
* ctime (OS-specific behavior; generally disk metadata has changed) (if supported)
* atime (access time)


olderThan (as mentioned above) will find paths *older* than age if true, otherwise *newer*. olderThan (as mentioned above) will find paths *older* than age if true, otherwise *newer*.
*/ */
func SearchFsPaths( func SearchFsPaths(
root string, root string,
targetType fs.FileMode, targetType fs.FileMode, noFiles bool,
basePtrn, pathPtrn *regexp.Regexp, basePtrn, pathPtrn *regexp.Regexp,
age *time.Duration, ageType pathTimeType, olderThan bool, age *time.Duration, ageType pathTimeType, olderThan bool,
) (foundPaths []string, err error) { ) (foundPaths []string, err error) {
@ -355,14 +362,15 @@ func SearchFsPaths(
} }


// fs object type (file, dir, etc.) // fs object type (file, dir, etc.)
if targetType != 0 && uint(targetType) != uint(modeAny) { if fi, outErr = d.Info(); outErr != nil {
if fi, outErr = d.Info(); outErr != nil { return
return }
} typeMode = fi.Mode().Type()
typeMode = fi.Mode().Type() if typeMode == 0 && noFiles {
if !typeFilter.HasFlag(bitmask.MaskBit(typeMode)) { return
return }
} if !typeFilter.HasFlag(bitmask.MaskBit(typeMode)) {
return
} }


// All filters passed at this point. // All filters passed at this point.
@ -411,9 +419,9 @@ func filterTimes(tspec times.Timespec, age *time.Duration, ageType *pathTimeType
*now = time.Now() *now = time.Now()
} }


// ATIME // BTIME (if supported)
if mask.HasFlag(bitmask.MaskBit(TimeAny)) || mask.HasFlag(bitmask.MaskBit(TimeAccessed)) { if tspec.HasBirthTime() && (mask.HasFlag(bitmask.MaskBit(TimeAny)) || mask.HasFlag(bitmask.MaskBit(TimeCreated))) {
curAge = now.Sub(tspec.AccessTime()) curAge = now.Sub(tspec.BirthTime())
if include = tfunc(&curAge); include { if include = tfunc(&curAge); include {
return return
} }
@ -432,9 +440,9 @@ func filterTimes(tspec times.Timespec, age *time.Duration, ageType *pathTimeType
return return
} }
} }
// BTIME (if supported) // ATIME
if tspec.HasBirthTime() && (mask.HasFlag(bitmask.MaskBit(TimeAny)) || mask.HasFlag(bitmask.MaskBit(TimeCreated))) { if mask.HasFlag(bitmask.MaskBit(TimeAny)) || mask.HasFlag(bitmask.MaskBit(TimeAccessed)) {
curAge = now.Sub(tspec.BirthTime()) curAge = now.Sub(tspec.AccessTime())
if include = tfunc(&curAge); include { if include = tfunc(&curAge); include {
return return
} }