main.go 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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. err error
  46. }
  47. // Append appends safely an inode to the INodeList.
  48. func (il *INodeList) Append(node *INode) {
  49. il.safe.Lock()
  50. il.Elements = append(il.Elements, node)
  51. il.safe.Unlock()
  52. }
  53. // INodeChan is a channel global to the sizer package that is used
  54. // to funnel the retrieved sizes and infos on the inodes.
  55. var INodeChan chan *INode
  56. // ErrNotIdenfiedType is the error returned when the a classifier does not
  57. // recognize the INode type.
  58. var ErrNotIdenfiedType = errors.New("inode not identified")
  59. // NewINode returns a new *INode with kind, size and path
  60. func NewINode(kind int, size int64, path string) *INode {
  61. var children INodeList
  62. abspath, _ := filepath.Abs(path)
  63. var newINode = INode{
  64. Kind: kind,
  65. Size: size,
  66. Path: abspath,
  67. Children: &children,
  68. }
  69. return &newINode
  70. }
  71. // Name returns the name of the file at the INode.
  72. func (i *INode) Name() string {
  73. _, name := filepath.Split(i.Path)
  74. return name
  75. }
  76. // IdentifyType classifies a file-like object in one of the FileType.
  77. func IdentifyType(path string) (int, int64, error) {
  78. f, err := os.Lstat(path)
  79. if err != nil {
  80. return -1, 0, err
  81. }
  82. switch mode := f.Mode(); {
  83. case mode.IsDir():
  84. return DirType, f.Size(), nil
  85. case mode.IsRegular():
  86. return FileType, f.Size(), nil
  87. }
  88. return -1, 0, ErrNotIdenfiedType
  89. }
  90. func ls(path string) ([]string, error) {
  91. var content []string
  92. f, err := os.Open(path)
  93. defer f.Close()
  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 runThrough(i *INode, ch chan int64, report chan statusReport, content []string) {
  107. Console.Debugln("Entering:", i.Path)
  108. for _, filepath := range content {
  109. f, err := os.Lstat(filepath)
  110. if err != nil {
  111. report <- statusReport{path: i.Path, err: err}
  112. }
  113. switch mode := f.Mode(); {
  114. case mode.IsDir():
  115. Console.Debugln(filepath, "is a directory")
  116. dirINode := NewINode(FileTypes["dir"], f.Size(), filepath)
  117. i.Children.Append(dirINode)
  118. ch <- f.Size()
  119. go dirINode.walkDir(report)
  120. case mode.IsRegular():
  121. Console.Debugln(filepath, "is a regular file")
  122. fileINode := NewINode(FileTypes["file"], f.Size(), filepath)
  123. i.Children.Append(fileINode)
  124. ch <- f.Size()
  125. default:
  126. Console.Debugln(filepath, "is NOT a regular file")
  127. var otherINode *INode
  128. inodeType, err := IdentifyType(filepath)
  129. if err != nil {
  130. if err != ErrNotIdenfiedType {
  131. report <- statusReport{path: filepath, err: err}
  132. return // Is this necessary?
  133. }
  134. otherINode = NewINode(-1, 0, filepath)
  135. } else {
  136. otherINode = NewINode(inodeType, 0, filepath)
  137. }
  138. i.Children.Append(otherINode)
  139. }
  140. }
  141. report <- statusReport{path: i.Path, err: nil}
  142. }
  143. func (i *INode) walkDir(reportUp chan statusReport) {
  144. ch := make(chan int64, 1)
  145. report := make(chan statusReport, 1)
  146. content, err := ls(i.Path)
  147. if err != nil {
  148. Console.Fatal(err)
  149. }
  150. go runThrough(i, ch, report, content)
  151. for {
  152. select {
  153. case size := <-ch:
  154. i.Size += size
  155. case status := <-report:
  156. Console.Debugf("[%s] Report from: %s\n", i.Path, status.path)
  157. if status.err != nil {
  158. Console.Debugln("Sending error. Path:", status.path, "- Err:", status.err)
  159. reportUp <- status
  160. }
  161. if status.err == nil {
  162. Console.Debugf("[%s] Received -> %s - Closing channel...\n",
  163. i.Path, status.path)
  164. reportUp <- status
  165. close(reportUp)
  166. return
  167. }
  168. }
  169. }
  170. }
  171. // Sizer is the main entrypoint for the computation of the size of a directory.
  172. func (i *INode) Sizer() {
  173. if i.Kind == FileTypes["file"] {
  174. Console.Println("This is not a directory!")
  175. return
  176. }
  177. report := make(chan statusReport)
  178. go i.walkDir(report)
  179. for r := range report {
  180. if r.err != nil {
  181. Console.Fatalf("%s: %s", r.path, r.err)
  182. }
  183. Console.Printf("%s: %d", i.Path, i.Size)
  184. }
  185. }