main.go 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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 FileTypes.
  77. func IdentifyType(path string) (int, error) {
  78. return -1, ErrNotIdenfiedType
  79. }
  80. func ls(path string) ([]string, error) {
  81. var content []string
  82. f, err := os.Open(path)
  83. defer f.Close()
  84. if err != nil {
  85. return []string{}, err
  86. }
  87. files, err := f.Readdir(-1)
  88. if err != nil {
  89. return []string{}, err
  90. }
  91. for _, fi := range files {
  92. content = append(content, filepath.Join(path, fi.Name()))
  93. }
  94. return content, nil
  95. }
  96. func runThrough(i *INode, ch chan int64, report chan statusReport, content []string) {
  97. Console.Debugln("Entering:", i.Path)
  98. for _, filepath := range content {
  99. f, err := os.Lstat(filepath)
  100. if err != nil {
  101. report <- statusReport{path: i.Path, err: err}
  102. }
  103. switch mode := f.Mode(); {
  104. case mode.IsDir():
  105. Console.Debugln(filepath, "is a directory")
  106. dirINode := NewINode(FileTypes["dir"], f.Size(), filepath)
  107. i.Children.Append(dirINode)
  108. ch <- f.Size()
  109. go dirINode.walkDir(report)
  110. case mode.IsRegular():
  111. Console.Debugln(filepath, "is a regular file")
  112. fileINode := NewINode(FileTypes["file"], f.Size(), filepath)
  113. i.Children.Append(fileINode)
  114. ch <- f.Size()
  115. default:
  116. Console.Debugln(filepath, "is NOT a regular file")
  117. var otherINode *INode
  118. inodeType, err := IdentifyType(filepath)
  119. if err != nil {
  120. if err != ErrNotIdenfiedType {
  121. report <- statusReport{path: filepath, err: err}
  122. return // Is this necessary?
  123. }
  124. otherINode = NewINode(-1, 0, filepath)
  125. } else {
  126. otherINode = NewINode(inodeType, 0, filepath)
  127. }
  128. i.Children.Append(otherINode)
  129. }
  130. }
  131. report <- statusReport{path: i.Path, err: nil}
  132. }
  133. func (i *INode) walkDir(reportUp chan statusReport) {
  134. ch := make(chan int64, 1)
  135. report := make(chan statusReport, 1)
  136. content, err := ls(i.Path)
  137. if err != nil {
  138. Console.Fatal(err)
  139. }
  140. go runThrough(i, ch, report, content)
  141. for {
  142. select {
  143. case size := <-ch:
  144. i.Size += size
  145. case status := <-report:
  146. Console.Debugf("[%s] Report from: %s\n", i.Path, status.path)
  147. if status.err != nil {
  148. Console.Debugln("Sending error. Path:", status.path, "- Err:", status.err)
  149. reportUp <- status
  150. }
  151. if status.err == nil {
  152. Console.Debugf("[%s] Received -> %s - Closing channel...\n",
  153. i.Path, status.path)
  154. reportUp <- status
  155. close(reportUp)
  156. return
  157. }
  158. }
  159. }
  160. }
  161. // Sizer is the main entrypoint for the computation of the size of a directory.
  162. func (i *INode) Sizer() {
  163. if i.Kind == FileTypes["file"] {
  164. Console.Println("This is not a directory!")
  165. return
  166. }
  167. report := make(chan statusReport)
  168. go i.walkDir(report)
  169. for r := range report {
  170. if r.err != nil {
  171. Console.Fatalf("%s: %s", r.path, r.err)
  172. }
  173. Console.Printf("%s: %d", i.Path, i.Size)
  174. }
  175. }