main.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. }
  107. }
  108. func (i *INode) size(wg *sync.WaitGroup) {
  109. //Console.Debugln(i.Path)
  110. i.Children.safe.Lock()
  111. defer i.Children.safe.Unlock()
  112. var wgIn sync.WaitGroup
  113. for _, child := range i.Children.Elements {
  114. wg.Add(1)
  115. go child.walk(&wgIn)
  116. }
  117. wgIn.Wait()
  118. for _, child := range i.Children.Elements {
  119. i.Size += child.Size
  120. wg.Done()
  121. }
  122. }
  123. func (i *INode) walk(wg *sync.WaitGroup) {
  124. children, err := ls(i.Path)
  125. if err != nil {
  126. Console.Println(err)
  127. wg.Done()
  128. return
  129. }
  130. for _, child := range children {
  131. //Console.Debugln("Parent:", i.Path, "- Child:", child)
  132. kind, size, err := IdentifyType(child)
  133. if err != nil {
  134. Console.Println(child, ":", err)
  135. }
  136. childINode := NewINode(kind, size, child)
  137. i.Children.Append(childINode)
  138. switch {
  139. case kind == DirType:
  140. //Console.Debugln("Size on dir:", child)
  141. wg.Add(1)
  142. childINode.size(wg)
  143. case kind == FileType:
  144. Console.Debugln("Filesize:", child, "-", size)
  145. childINode.Size = size
  146. default:
  147. //Console.Debugln("Unknown:", child)
  148. childINode.Size = 0
  149. }
  150. }
  151. wg.Done()
  152. }
  153. func (i *INode) Sizer() {
  154. var wg sync.WaitGroup
  155. wg.Add(1)
  156. i.walk(&wg)
  157. wg.Wait()
  158. Console.Println(i.Size)
  159. }