Skip to content

Commit

Permalink
Linting fixes
Browse files Browse the repository at this point in the history
PRONOM identifier related linting fixes for the different source
files touched by the PRONOM types additions.
  • Loading branch information
ross-spencer committed Dec 28, 2022
1 parent 2bdc899 commit e27bb70
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 29 deletions.
6 changes: 3 additions & 3 deletions cmd/sf/longpath.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ func identify(ctxts chan *context, root, orig string, coerr, norecurse, droid bo
}
if err != nil {
if coerr {
printFile(ctxts, gf(path, "", time.Time{}, 0), WalkError{path, err})
printFile(ctxts, gf(path, "", time.Time{}, 0), walkError{path, err})
return nil
}
return WalkError{path, err}
return walkError{path, err}
}
if info.IsDir() {
if norecurse && path != root {
Expand All @@ -50,7 +50,7 @@ func identify(ctxts chan *context, root, orig string, coerr, norecurse, droid bo
}
// zero user read permissions mask, octal 400 (decimal 256)
if !info.Mode().IsRegular() || info.Mode()&256 == 0 {
printFile(ctxts, gf(path, "", info.ModTime(), info.Size()), ModeError(info.Mode()))
printFile(ctxts, gf(path, "", info.ModTime(), info.Size()), modeError(info.Mode()))
return nil
}
identifyFile(gf(path, "", info.ModTime(), info.Size()), ctxts, gf)
Expand Down
6 changes: 3 additions & 3 deletions cmd/sf/longpath_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ func identify(ctxts chan *context, root, orig string, coerr, norecurse, droid bo
info, err = retryStat(path, err) // retry stat in case is a windows long path error
if err != nil {
if coerr {
printFile(ctxts, gf(path, "", time.Time{}, 0), WalkError{path, err})
printFile(ctxts, gf(path, "", time.Time{}, 0), walkError{path, err})
return nil
}
return WalkError{path, err}
return walkError{path, err}
}
lp, sp = longpath(path), path
retry = true
Expand All @@ -107,7 +107,7 @@ func identify(ctxts chan *context, root, orig string, coerr, norecurse, droid bo
return nil
}
if !info.Mode().IsRegular() {
printFile(ctxts, gf(path, "", info.ModTime(), info.Size()), ModeError(info.Mode()))
printFile(ctxts, gf(path, "", info.ModTime(), info.Size()), modeError(info.Mode()))
return nil
}
identifyFile(gf(shortpath(path, orig), "", info.ModTime(), info.Size()), ctxts, gf)
Expand Down
2 changes: 1 addition & 1 deletion cmd/sf/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ func handleIdentify(w http.ResponseWriter, r *http.Request, s *siegfried.Siegfri
err = identify(ctxts, path, "", coerr, nrec, d, gf)
wg.Wait()
wr.Tail()
if _, ok := err.(WalkError); ok { // only dump out walk errors, other errors reported in result
if _, ok := err.(walkError); ok { // only dump out walk errors, other errors reported in result
io.WriteString(w, err.Error())
}
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/sf/sf.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ var (
ctxPool *sync.Pool
)

type ModeError os.FileMode
type modeError os.FileMode

func (me ModeError) Error() string {
func (me modeError) Error() string {
typ := "unknown"
switch {
case os.FileMode(me)&os.ModeDir == os.ModeDir:
Expand All @@ -93,12 +93,12 @@ func (me ModeError) Error() string {
return fmt.Sprintf("file is of type %s; only regular files can be scanned", typ)
}

type WalkError struct {
type walkError struct {
path string
err error
}

func (we WalkError) Error() string {
func (we walkError) Error() string {
return fmt.Sprintf("[FATAL] file access error for %s: %v", we.path, we.err)
}

Expand Down
8 changes: 5 additions & 3 deletions pkg/config/pronom.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ var pronom = struct {

// GETTERS

// DROID returns the location of the DROID signature file.
// Droid returns the location of the DROID signature file.
// If not set, infers the latest file.
func Droid() string {
if pronom.droid == "" {
Expand All @@ -79,7 +79,7 @@ func Droid() string {
return pronom.droid
}

// DROID base returns the base filename of the DROID signature file.
// DroidBase returns the base filename of the DROID signature file.
// If not set, infers the latest file.
func DroidBase() string {
if pronom.droid == "" {
Expand Down Expand Up @@ -173,11 +173,12 @@ func ExcludeDoubles(puids, cont []string) []string {
return exclude(puids, cont)
}

// Extend reports whether a set of container signature extensions has been provided.
// ExtendC reports whether a set of container signature extensions has been provided.
func ExtendC() []string {
return extensionPaths(pronom.extendc)
}

// ChangesURL returns the URL for the PRONOM release notes.
func ChangesURL() string {
return pronom.changesURL
}
Expand Down Expand Up @@ -257,6 +258,7 @@ func SetHarvestTimeout(d time.Duration) {
pronom.harvestTimeout = d
}

// SetHarvestThrottle sets a throttle value for downloading DROID reports.
func SetHarvestThrottle(d time.Duration) {
pronom.harvestThrottle = d
}
Expand Down
29 changes: 19 additions & 10 deletions pkg/pronom/identifier.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func init() {
core.RegisterIdentifier(core.Pronom, Load)
}

// Identifier stores identifier format details alongside the baseline
// siegfried identifier.
type Identifier struct {
infos map[string]formatInfo
*identifier.Base
Expand Down Expand Up @@ -67,6 +69,7 @@ func Load(ls *persist.LoadSaver) core.Identifier {
return i
}

// New creates a new instance of a PRONOM identifier.
func New(opts ...config.Option) (core.Identifier, error) {
for _, v := range opts {
v()
Expand Down Expand Up @@ -96,13 +99,15 @@ func (i *Identifier) Fields() []string {
}
}

// Recorder provides a new recorder for identification results.
func (i *Identifier) Recorder() core.Recorder {
return &Recorder{
Identifier: i,
ids: make(pids, 0, 1),
}
}

// Recorder stores information about match results.
type Recorder struct {
*Identifier
ids pids
Expand All @@ -120,6 +125,7 @@ const (
incScore
)

// Active sets the flags for the recorder's active matchers.
func (r *Recorder) Active(m core.MatcherType) {
if r.Identifier.Active(m) {
switch m {
Expand All @@ -133,6 +139,7 @@ func (r *Recorder) Active(m core.MatcherType) {
}
}

// Record builds possible results sets associated with an identification.
func (r *Recorder) Record(m core.MatcherType, res core.Result) bool {
switch m {
default:
Expand All @@ -141,16 +148,14 @@ func (r *Recorder) Record(m core.MatcherType, res core.Result) bool {
if hit, id := r.Hit(m, res.Index()); hit {
r.ids = add(r.ids, r.Name(), id, r.infos[id], res.Basis(), extScore)
return true
} else {
return false
}
return false
case core.MIMEMatcher:
if hit, id := r.Hit(m, res.Index()); hit {
r.ids = add(r.ids, r.Name(), id, r.infos[id], res.Basis(), mimeScore)
return true
} else {
return false
}
return false
case core.ContainerMatcher:
// add zip default
if res.Index() < 0 {
Expand All @@ -169,9 +174,8 @@ func (r *Recorder) Record(m core.MatcherType, res core.Result) bool {
}
r.ids = add(r.ids, r.Name(), id, r.infos[id], basis, r.cscore)
return true
} else {
return false
}
return false
case core.ByteMatcher:
if hit, id := r.Hit(m, res.Index()); hit {
if r.satisfied {
Expand All @@ -185,22 +189,22 @@ func (r *Recorder) Record(m core.MatcherType, res core.Result) bool {
}
r.ids = add(r.ids, r.Name(), id, r.infos[id], basis, r.cscore)
return true
} else {
return false
}
return false
case core.TextMatcher:
if hit, id := r.Hit(m, res.Index()); hit {
if r.satisfied {
return true
}
r.ids = add(r.ids, r.Name(), id, r.infos[id], res.Basis(), textScore)
return true
} else {
return false
}
return false
}
}

// Satisfied determines whether we should continue running identification
// with a given matcher type.
func (r *Recorder) Satisfied(mt core.MatcherType) (bool, core.Hint) {
if r.NoPriority() {
return false, core.Hint{}
Expand Down Expand Up @@ -256,6 +260,8 @@ func lowConfidence(conf int) string {
}
}

// Report organizes the results output and lists the highest priority
// results first.
func (r *Recorder) Report() []core.Identification {
// no results
if len(r.ids) == 0 {
Expand Down Expand Up @@ -406,10 +412,12 @@ func (id Identification) String() string {
return id.ID
}

// Known outputs true if the identification result isn't UNKNOWN.
func (id Identification) Known() bool {
return id.ID != "UNKNOWN"
}

// Warn returns the associated warning for a given identification.
func (id Identification) Warn() string {
return id.Warning
}
Expand All @@ -432,6 +440,7 @@ func (id Identification) Values() []string {
}
}

// Archive returns the archive value for a given identification.
func (id Identification) Archive() config.Archive {
return id.archive
}
Expand Down
20 changes: 17 additions & 3 deletions pkg/pronom/internal/mappings/droid.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// This file contains struct mappings to unmarshal three different PRONOM XML formats: the signature file format, the report format, and the container format
// Package mappings contains struct mappings to unmarshal three
// different PRONOM XML formats: the signature file format, the report
// format, and the container format.
package mappings

import (
Expand All @@ -21,23 +23,31 @@ import (

// Droid Signature File

// Droid describes the basic top-level structure of a DROID signature
// file.
type Droid struct {
XMLName xml.Name `xml:"FFSignatureFile"`
Version int `xml:",attr"`
Signatures []InternalSignature `xml:"InternalSignatureCollection>InternalSignature"`
FileFormats []FileFormat `xml:"FileFormatCollection>FileFormat"`
}

// InternalSignature describes the structure of the InternalSignature
// section of a DROID signature file.
type InternalSignature struct {
Id int `xml:"ID,attr"`
ID int `xml:"ID,attr"`
ByteSequences []ByteSeq `xml:"ByteSequence"`
}

// ByteSeq describes the structure of the ByteSequence sections of a
// DROID signature file.
type ByteSeq struct {
Reference string `xml:"Reference,attr"`
SubSequences []SubSequence `xml:"SubSequence"`
}

// SubSequence describes the structure of the SubSequence sections of a
// DROID signature file.
type SubSequence struct {
Position int `xml:",attr"`
SubSeqMinOffset string `xml:",attr"` // and empty int values are unmarshalled to 0
Expand All @@ -47,16 +57,20 @@ type SubSequence struct {
RightFragments []Fragment `xml:"RightFragment"`
}

// Fragment describes the structure of the fragment sections of a DROID
// signature file.
type Fragment struct {
Value string `xml:",chardata"`
MinOffset string `xml:",attr"`
MaxOffset string `xml:",attr"`
Position int `xml:",attr"`
}

// FileFormat describes the structure of the FileFormat section of a
// DROID signature file.
type FileFormat struct {
XMLName xml.Name `xml:"FileFormat"`
Id int `xml:"ID,attr"`
ID int `xml:"ID,attr"`
Puid string `xml:"PUID,attr"`
Name string `xml:",attr"`
Version string `xml:",attr"`
Expand Down
4 changes: 2 additions & 2 deletions pkg/pronom/parseable.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ func (d *droid) Texts() []string {
func (d *droid) idsPuids() map[int]string {
idsPuids := make(map[int]string)
for _, v := range d.FileFormats {
idsPuids[v.Id] = v.Puid
idsPuids[v.ID] = v.Puid
}
return idsPuids
}
Expand Down Expand Up @@ -317,7 +317,7 @@ func (d *droid) Signatures() ([]frames.Signature, []string, error) {
// first a map of internal sig ids to bytesequences
seqs := make(map[int][]mappings.ByteSeq)
for _, v := range d.Droid.Signatures {
seqs[v.Id] = v.ByteSequences
seqs[v.ID] = v.ByteSequences
}
m := d.puidsInternalIds()
var err error
Expand Down

0 comments on commit e27bb70

Please sign in to comment.