main.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. package sizer
  2. import (
  3. "errors"
  4. "os"
  5. "path/filepath"
  6. "sync"
  7. )
  8. const (
  9. // FileType is a const representing a file
  10. FileType = iota
  11. // DirType is a const representing a directory
  12. DirType = iota
  13. // SymLinkType is a const representing a symbolic link
  14. SymLinkType = iota
  15. // HardLinkType is a const representing a hard link
  16. HardLinkType = iota
  17. // CharDevType is a const representing a char device
  18. CharDevType = iota
  19. // NamedPipeType is a const representing a named pipe
  20. NamedPipeType = iota
  21. // SocketType is a const representing a socket, both unix and network
  22. SocketType = iota
  23. )
  24. // INode is an entry in the filesystem. It can be:
  25. // - a file
  26. // - a directory
  27. // - a symbolic link
  28. // - a hard link
  29. // - a char device/named pipe/socket
  30. // It takes a Kind (as above described), a Size and a Path
  31. type INode struct {
  32. Kind int
  33. Size int64
  34. Path string
  35. Children *INodeList
  36. }
  37. // INodeList is a simple *INode slice with a Mutex to allow safe
  38. // concurrent write.
  39. type INodeList struct {
  40. Elements []*INode
  41. safe sync.Mutex
  42. }
  43. type StatusReport struct {
  44. path string
  45. size int64
  46. err error
  47. }
  48. // Append appends safely an inode to the INodeList.
  49. func (il *INodeList) Append(node *INode) {
  50. il.safe.Lock()
  51. il.Elements = append(il.Elements, node)
  52. il.safe.Unlock()
  53. }
  54. // INodeChan is a channel global to the sizer package that is used
  55. // to funnel the retrieved sizes and infos on the inodes.
  56. var INodeChan chan *INode
  57. // ErrNotIdenfiedType is the error returned when the a classifier does not
  58. // recognize the INode type.
  59. var ErrNotIdenfiedType = errors.New("inode not identified")
  60. // NewINode returns a new *INode with kind, size and path
  61. func NewINode(kind int, size int64, path string) *INode {
  62. var children INodeList
  63. abspath, _ := filepath.Abs(path)
  64. var newINode = INode{
  65. Kind: kind,
  66. Size: size,
  67. Path: abspath,
  68. Children: &children,
  69. }
  70. return &newINode
  71. }
  72. // Name returns the name of the file at the INode.
  73. func (i *INode) Name() string {
  74. _, name := filepath.Split(i.Path)
  75. return name
  76. }
  77. // IdentifyType classifies a file-like object in one of the FileType.
  78. func IdentifyType(path string) (int, int64, error) {
  79. f, err := os.Lstat(path)
  80. if err != nil {
  81. return -1, 0, err
  82. }
  83. switch mode := f.Mode(); {
  84. case mode.IsDir():
  85. return DirType, f.Size(), nil
  86. case mode.IsRegular():
  87. return FileType, f.Size(), nil
  88. }
  89. return -1, 0, ErrNotIdenfiedType
  90. }
  91. func ls(path string) ([]string, error) {
  92. var content []string
  93. f, err := os.Open(path)
  94. if err != nil {
  95. return []string{}, err
  96. }
  97. files, err := f.Readdir(-1)
  98. if err != nil {
  99. return []string{}, err
  100. }
  101. for _, fi := range files {
  102. content = append(content, filepath.Join(path, fi.Name()))
  103. }
  104. return content, nil
  105. }
  106. func INodeFromPath(path string) (*INode, error) {
  107. kind, _, err := IdentifyType(path)
  108. if err != nil {
  109. return nil, err
  110. }
  111. i := NewINode(kind, 0, path)
  112. return i, nil
  113. }
  114. func (i *INode) size(wg *sync.WaitGroup) {
  115. //Console.Debugln(i.Path)
  116. i.Children.safe.Lock()
  117. defer i.Children.safe.Unlock()
  118. var wgIn sync.WaitGroup
  119. for _, child := range i.Children.Elements {
  120. wg.Add(1)
  121. go child.walk(&wgIn)
  122. }
  123. wgIn.Wait()
  124. for _, child := range i.Children.Elements {
  125. i.Size += child.Size
  126. wg.Done()
  127. }
  128. }
  129. func (i *INode) walk(wg *sync.WaitGroup) {
  130. children, err := ls(i.Path)
  131. if err != nil {
  132. Console.Println(err)
  133. wg.Done()
  134. return
  135. }
  136. for _, child := range children {
  137. //Console.Debugln("Parent:", i.Path, "- Child:", child)
  138. kind, size, err := IdentifyType(child)
  139. if err != nil {
  140. Console.Println(child, ":", err)
  141. }
  142. childINode := NewINode(kind, size, child)
  143. i.Children.Append(childINode)
  144. switch {
  145. case kind == DirType:
  146. //Console.Debugln("Size on dir:", child)
  147. wg.Add(1)
  148. childINode.size(wg)
  149. case kind == FileType:
  150. Console.Debugln("Filesize:", child, "-", size)
  151. childINode.Size = size
  152. default:
  153. //Console.Debugln("Unknown:", child)
  154. childINode.Size = 0
  155. }
  156. }
  157. wg.Done()
  158. }
  159. func (i *INode) Sizer() {
  160. var wg sync.WaitGroup
  161. wg.Add(1)
  162. i.walk(&wg)
  163. wg.Wait()
  164. Console.Println(i.Size)
  165. }