fork/gdu
git clone https://git.julia.blue/fork/gdu
d062e88
master
1package analyze 2 3import ( 4"encoding/json" 5"io" 6"strconv" 7 8"github.com/dundee/gdu/v5/pkg/fs" 9) 10 11// EncodeJSON writes JSON representation of dir 12func (f * Dir )EncodeJSON (writer io.Writer ,topLevel bool ,attributes fs.JSONAttributes )error { 13buff := make ([]byte ,0 ,20 ) 14 15buff = append (buff , []byte (`[{"name":` )... ) 16 17if topLevel { 18if err := addString (& buff ,f .GetPath ());err != nil { 19return err 20 } 21 }else { 22if err := addString (& buff ,f .GetName ());err != nil { 23return err 24 } 25 } 26 27// Directory summary stats (asize, dsize, items) are written by default so 28// they can be preserved on import. When --output-attrs is supplied, each is 29// emitted only if explicitly selected. 30if attributes .Includes ("asize" ) { 31buff = append (buff , []byte (`,"asize":` )... ) 32buff = append (buff , []byte (strconv .FormatInt (f .GetSize (),10 ))... ) 33 } 34if attributes .Includes ("dsize" ) { 35buff = append (buff , []byte (`,"dsize":` )... ) 36buff = append (buff , []byte (strconv .FormatInt (f .GetUsage (),10 ))... ) 37 } 38if attributes .Includes ("items" ) { 39buff = append (buff , []byte (`,"items":` )... ) 40buff = append (buff , []byte (strconv .FormatInt (f .GetItemCount (),10 ))... ) 41 } 42if attributes .Includes ("mtime" )&& ! f .GetMtime ().IsZero () { 43buff = append (buff , []byte (`,"mtime":` )... ) 44buff = append (buff , []byte (strconv .FormatInt (f .GetMtime ().Unix (),10 ))... ) 45 } 46 47buff = append (buff ,'}' ) 48if f .Files .Len ()> 0 { 49buff = append (buff ,',' ) 50 } 51buff = append (buff ,'\n' ) 52 53if _ ,err := writer .Write (buff );err != nil { 54return err 55 } 56 57for i ,item := range f .Files { 58if i > 0 { 59if _ ,err := writer .Write ([]byte (",\n" ));err != nil { 60return err 61 } 62 } 63err := item .EncodeJSON (writer ,false ,attributes ) 64if err != nil { 65return err 66 } 67 } 68 69if _ ,err := writer .Write ([]byte ("]" ));err != nil { 70return err 71 } 72return nil 73} 74 75// EncodeJSON writes JSON representation of file 76func (f * File )EncodeJSON (writer io.Writer ,_ bool ,attributes fs.JSONAttributes )error { 77buff := make ([]byte ,0 ,20 ) 78 79buff = append (buff , []byte (`{"name":` )... ) 80if err := addString (& buff ,f .GetName ());err != nil { 81return err 82 } 83if attributes .Includes ("asize" )&& f .GetSize ()> 0 { 84buff = append (buff , []byte (`,"asize":` )... ) 85buff = append (buff , []byte (strconv .FormatInt (f .GetSize (),10 ))... ) 86 } 87if attributes .Includes ("dsize" )&& f .GetUsage ()> 0 { 88buff = append (buff , []byte (`,"dsize":` )... ) 89buff = append (buff , []byte (strconv .FormatInt (f .GetUsage (),10 ))... ) 90 } 91if attributes .Includes ("mtime" )&& ! f .GetMtime ().IsZero () { 92buff = append (buff , []byte (`,"mtime":` )... ) 93buff = append (buff , []byte (strconv .FormatInt (f .GetMtime ().Unix (),10 ))... ) 94 } 95 96if attributes .Includes ("notreg" )&& f .Flag == '@' { 97buff = append (buff , []byte (`,"notreg":true` )... ) 98 } 99if attributes == nil && f .Flag == 'H' { 100buff = append (buff , []byte (`,"ino":` + strconv .FormatUint (f .Mli ,10 )+ `,"hlnkc":true` )... ) 101 } 102 103buff = append (buff ,'}' ) 104 105if _ ,err := writer .Write (buff );err != nil { 106return err 107 } 108return nil 109} 110 111func addString (buff * []byte ,val string )error { 112b ,err := json .Marshal (val ) 113if err != nil { 114return err 115 } 116* buff = append (* buff ,b ... ) 117return err 118}