stat

package
v0.31.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 19, 2025 License: MIT Imports: 7 Imported by: 18

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CalcCheckSum added in v0.30.0

func CalcCheckSum(b []byte) string

CalcCheckSum creates a md5 hash based on the bytes passed in. This is a common method to get a checksum of a file.

Types

type Safe added in v0.30.0

type Safe struct {
	// LineCnt returns the file line count.
	LineCnt int64 `json:"linecnt,omitempty"`

	// ByteCount returns uncompressed raw file byte count.
	ByteCnt int64 `json:"bytecnt,omitempty"`

	// Size holds the actual file size.
	Size int64 `json:"size"`

	Files int64 `json:"files,omitempty"`
	// contains filtered or unexported fields
}

Safe is thread safe without using mutex by using atomic values for all fields Do not directly modify on the values in this struct.

func (*Safe) AddBytes added in v0.30.0

func (s *Safe) AddBytes(cnt int64)

AddBytes will atomically and safely increment ByteCnt by 'cnt'.

Example
sts := Safe{}

sts.AddBytes(1)
sts.AddBytes(10)
sts.AddBytes(1100)

fmt.Println(sts.ByteCnt) // 1111
Output:

1111

func (*Safe) AddLine added in v0.30.0

func (s *Safe) AddLine()

AddLine will atomically and safely increment LineCnt by one.

Example
sts := Safe{}

sts.AddLine()
sts.AddLine()
sts.AddLine()

fmt.Println(sts.LineCnt) // 3
Output:

3

func (*Safe) Checksum added in v0.30.0

func (s *Safe) Checksum() string

func (*Safe) Created added in v0.30.0

func (s *Safe) Created() string

func (*Safe) IsDir added in v0.30.0

func (s *Safe) IsDir() bool

func (*Safe) JSONBytes added in v0.30.0

func (s *Safe) JSONBytes() []byte

func (*Safe) JSONString added in v0.30.0

func (s *Safe) JSONString() string

func (*Safe) ParseCreated added in v0.30.0

func (s *Safe) ParseCreated() time.Time

ParseCreated will attempt to parse the Created field to a time.Time object. ParseCreated expects the Created time string is in time.RFC3339. If there is a parse error then the time.Time zero value is returned.

The returned time will always be in UTC.

func (*Safe) Path added in v0.30.0

func (s *Safe) Path() string

func (*Safe) SetChecksum added in v0.30.0

func (s *Safe) SetChecksum(hsh hash.Hash)

SetChecksum will correctly calculate and set the base64 encoded checksum.

Example
sts := Safe{}

// checksum
hsh := md5.New()
hsh.Write([]byte("test message"))
sts.SetChecksum(hsh)

fmt.Println(sts.Checksum()) // c72b9698fa1927e1dd12d3cf26ed84b2
Output:

c72b9698fa1927e1dd12d3cf26ed84b2

func (*Safe) SetCreated added in v0.30.0

func (s *Safe) SetCreated(t time.Time)

SetCreated will set the Created field in the format time.RFC3339 in UTC.

Example
sts := Safe{}

created := "2017-01-02T03:04:05Z"
t, _ := time.Parse(time.RFC3339, created)

sts.SetCreated(t)
fmt.Println(sts.Created()) // 2017-01-02T03:04:05Z
Output:

2017-01-02T03:04:05Z

func (*Safe) SetDir added in v0.30.0

func (s *Safe) SetDir(isDir bool)

func (*Safe) SetPath added in v0.30.0

func (s *Safe) SetPath(pth string)
Example
sts := Safe{}

sts.SetPath("path/to/file.txt")
fmt.Println(sts.Path()) // path/to/file.txt
Output:

path/to/file.txt

func (*Safe) SetSize added in v0.30.0

func (s *Safe) SetSize(size int64)
Example
sts := Safe{}

sts.SetSize(15)
fmt.Println(sts.Size) // 15
Output:

15

func (*Safe) Stats added in v0.30.0

func (s *Safe) Stats() Stats

type Stats

type Stats struct {
	LineCnt int64 `json:"linecnt,omitempty"`

	// ByteCount returns uncompressed raw file byte count.
	ByteCnt int64 `json:"bytecnt,omitempty"`

	// Size holds the actual file size.
	Size int64 `json:"size"`

	// Checksum base64 encoded string of the file md5 hash
	Checksum string `json:"checksum,omitempty"`

	// Path returns the full absolute path of the file.
	Path string `json:"path" uri:"origin"`

	// Created date the file was created or last updated Format(time.RFC3339)
	// whichever is more recent.
	Created string `json:"created"`

	IsDir bool `json:"isDir,omitempty"`

	Files int64 `json:"files,omitempty"`
}

Stats is an immutable struct describe file details Safe should be used for any needed changes

func New deprecated

func New() Stats

Deprecated: reference Stats directly stat.Stats{}

Example
sts := New()
fmt.Println(sts.LineCnt)  // 0
fmt.Println(sts.ByteCnt)  // 0
fmt.Println(sts.Size)     // 0
fmt.Println(sts.Checksum) //
fmt.Println(sts.Path)     //
fmt.Println(sts.Created)  //
Output:

0
0
0

func NewFromBytes

func NewFromBytes(b []byte) (sts Stats)

NewFromBytes creates Stats from json bytes.

Example
sts := NewFromBytes([]byte(`{"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}`))
fmt.Println(sts.LineCnt)  // 10
fmt.Println(sts.ByteCnt)  // 100
fmt.Println(sts.Size)     // 200
fmt.Println(sts.Checksum) // test checksum
fmt.Println(sts.Path)     // test path
fmt.Println(sts.Created)  // test created
Output:

10
100
200
test checksum
test path
test created

func NewFromInfo

func NewFromInfo(info string) (sts Stats)

NewFromInfo creates Stats from a uri formatted info string.

func (Stats) JSONBytes

func (s Stats) JSONBytes() []byte
Example
sts := Stats{
	LineCnt:  10,
	ByteCnt:  100,
	Size:     200,
	Checksum: "test checksum",
	Path:     "test path",
	Created:  "test created",
}

b := sts.JSONBytes()
fmt.Println(string(b)) // {"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}
Output:

{"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}

func (Stats) JSONString

func (s Stats) JSONString() string
Example
sts := Stats{
	LineCnt:  10,
	ByteCnt:  100,
	Size:     200,
	Checksum: "test checksum",
	Path:     "test path",
	Created:  "test created",
}

s := sts.JSONString()
fmt.Println(s) // {"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}
Output:

{"linecnt":10,"bytecnt":100,"size":200,"checksum":"test checksum","path":"test path","created":"test created"}

func (Stats) ParseCreated

func (s Stats) ParseCreated() time.Time

ParseCreated converts the store string timestamp to a time.Time value

Example
sts := New()

sts.Created = "2017-01-02T03:04:05Z"

t := sts.ParseCreated()
fmt.Println(t.Format(time.RFC3339)) // 2017-01-02T03:04:05Z
Output:

2017-01-02T03:04:05Z

func (Stats) ToSafe added in v0.30.0

func (s Stats) ToSafe() *Safe
Example
sts := Stats{
	LineCnt:  1,
	ByteCnt:  2,
	Size:     3,
	Checksum: "4",
	Path:     "5",
	Created:  "6",
}

cln := sts.ToSafe()
fmt.Println(cln.LineCnt)
fmt.Println(cln.ByteCnt)
fmt.Println(cln.Size)
fmt.Println(cln.Checksum())
fmt.Println(cln.Path())
fmt.Println(cln.Created())
Output:

1
2
3
4
5
6

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL