You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

295 lines
7.1 KiB

  1. package embedded_filer
  2. import (
  3. "bufio"
  4. "fmt"
  5. "io"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "sync"
  11. "github.com/chrislusf/seaweedfs/go/filer"
  12. "github.com/chrislusf/seaweedfs/go/util"
  13. )
  14. var writeLock sync.Mutex //serialize changes to dir.log
  15. type DirectoryEntryInMap struct {
  16. sync.Mutex
  17. Name string
  18. Parent *DirectoryEntryInMap
  19. subDirectories map[string]*DirectoryEntryInMap
  20. Id filer.DirectoryId
  21. }
  22. func (de *DirectoryEntryInMap) getChild(dirName string) (*DirectoryEntryInMap, bool) {
  23. de.Lock()
  24. defer de.Unlock()
  25. child, ok := de.subDirectories[dirName]
  26. return child, ok
  27. }
  28. func (de *DirectoryEntryInMap) addChild(dirName string, child *DirectoryEntryInMap) {
  29. de.Lock()
  30. defer de.Unlock()
  31. de.subDirectories[dirName] = child
  32. }
  33. func (de *DirectoryEntryInMap) removeChild(dirName string) {
  34. de.Lock()
  35. defer de.Unlock()
  36. delete(de.subDirectories, dirName)
  37. }
  38. func (de *DirectoryEntryInMap) hasChildren() bool {
  39. de.Lock()
  40. defer de.Unlock()
  41. return len(de.subDirectories) > 0
  42. }
  43. func (de *DirectoryEntryInMap) children() (dirNames []filer.DirectoryEntry) {
  44. de.Lock()
  45. defer de.Unlock()
  46. for k, v := range de.subDirectories {
  47. dirNames = append(dirNames, filer.DirectoryEntry{Name: k, Id: v.Id})
  48. }
  49. return dirNames
  50. }
  51. type DirectoryManagerInMap struct {
  52. Root *DirectoryEntryInMap
  53. max filer.DirectoryId
  54. logFile *os.File
  55. isLoading bool
  56. }
  57. func (dm *DirectoryManagerInMap) NewDirectoryEntryInMap(parent *DirectoryEntryInMap, name string) (d *DirectoryEntryInMap, err error) {
  58. writeLock.Lock()
  59. defer writeLock.Unlock()
  60. d = &DirectoryEntryInMap{Name: name, Parent: parent, subDirectories: make(map[string]*DirectoryEntryInMap)}
  61. var parts []string
  62. for p := d; p != nil && p.Name != ""; p = p.Parent {
  63. parts = append(parts, p.Name)
  64. }
  65. n := len(parts)
  66. if n <= 0 {
  67. return nil, fmt.Errorf("Failed to create folder %s/%s", parent.Name, name)
  68. }
  69. for i := 0; i < n/2; i++ {
  70. parts[i], parts[n-1-i] = parts[n-1-i], parts[i]
  71. }
  72. dm.max++
  73. d.Id = dm.max
  74. dm.log("add", "/"+strings.Join(parts, "/"), strconv.Itoa(int(d.Id)))
  75. return d, nil
  76. }
  77. func (dm *DirectoryManagerInMap) log(words ...string) {
  78. if !dm.isLoading {
  79. dm.logFile.WriteString(strings.Join(words, "\t") + "\n")
  80. }
  81. }
  82. func NewDirectoryManagerInMap(dirLogFile string) (dm *DirectoryManagerInMap, err error) {
  83. dm = &DirectoryManagerInMap{}
  84. //dm.Root do not use NewDirectoryEntryInMap, since dm.max will be changed
  85. dm.Root = &DirectoryEntryInMap{subDirectories: make(map[string]*DirectoryEntryInMap)}
  86. if dm.logFile, err = os.OpenFile(dirLogFile, os.O_RDWR|os.O_CREATE, 0644); err != nil {
  87. return nil, fmt.Errorf("cannot write directory log file %s: %v", dirLogFile, err)
  88. }
  89. return dm, dm.load()
  90. }
  91. func (dm *DirectoryManagerInMap) processEachLine(line string) error {
  92. if strings.HasPrefix(line, "#") {
  93. return nil
  94. }
  95. if line == "" {
  96. return nil
  97. }
  98. parts := strings.Split(line, "\t")
  99. if len(parts) == 0 {
  100. return nil
  101. }
  102. switch parts[0] {
  103. case "add":
  104. v, pe := strconv.Atoi(parts[2])
  105. if pe != nil {
  106. return pe
  107. }
  108. if e := dm.loadDirectory(parts[1], filer.DirectoryId(v)); e != nil {
  109. return e
  110. }
  111. case "mov":
  112. newName := ""
  113. if len(parts) >= 4 {
  114. newName = parts[3]
  115. }
  116. if e := dm.MoveUnderDirectory(parts[1], parts[2], newName); e != nil {
  117. return e
  118. }
  119. case "del":
  120. if e := dm.DeleteDirectory(parts[1]); e != nil {
  121. return e
  122. }
  123. default:
  124. fmt.Printf("line %s has %s!\n", line, parts[0])
  125. return nil
  126. }
  127. return nil
  128. }
  129. func (dm *DirectoryManagerInMap) load() error {
  130. dm.max = 0
  131. lines := bufio.NewReader(dm.logFile)
  132. dm.isLoading = true
  133. defer func() { dm.isLoading = false }()
  134. for {
  135. line, err := util.Readln(lines)
  136. if err != nil && err != io.EOF {
  137. return err
  138. }
  139. if pe := dm.processEachLine(string(line)); pe != nil {
  140. return pe
  141. }
  142. if err == io.EOF {
  143. return nil
  144. }
  145. }
  146. }
  147. func (dm *DirectoryManagerInMap) findDirectory(dirPath string) (*DirectoryEntryInMap, error) {
  148. if dirPath == "" {
  149. return dm.Root, nil
  150. }
  151. dirPath = CleanFilePath(dirPath)
  152. if dirPath == "/" {
  153. return dm.Root, nil
  154. }
  155. parts := strings.Split(dirPath, "/")
  156. dir := dm.Root
  157. for i := 1; i < len(parts); i++ {
  158. if sub, ok := dir.getChild(parts[i]); ok {
  159. dir = sub
  160. } else {
  161. return dm.Root, fmt.Errorf("Directory %s Not Found", dirPath)
  162. }
  163. }
  164. return dir, nil
  165. }
  166. func (dm *DirectoryManagerInMap) FindDirectory(dirPath string) (filer.DirectoryId, error) {
  167. d, e := dm.findDirectory(dirPath)
  168. if e == nil {
  169. return d.Id, nil
  170. }
  171. return dm.Root.Id, e
  172. }
  173. func (dm *DirectoryManagerInMap) loadDirectory(dirPath string, dirId filer.DirectoryId) error {
  174. dirPath = CleanFilePath(dirPath)
  175. if dirPath == "/" {
  176. return nil
  177. }
  178. parts := strings.Split(dirPath, "/")
  179. dir := dm.Root
  180. for i := 1; i < len(parts); i++ {
  181. sub, ok := dir.getChild(parts[i])
  182. if !ok {
  183. if i != len(parts)-1 {
  184. return fmt.Errorf("%s should be created after parent %s", dirPath, parts[i])
  185. }
  186. var err error
  187. sub, err = dm.NewDirectoryEntryInMap(dir, parts[i])
  188. if err != nil {
  189. return err
  190. }
  191. if sub.Id != dirId {
  192. return fmt.Errorf("%s should be have id %v instead of %v", dirPath, sub.Id, dirId)
  193. }
  194. dir.addChild(parts[i], sub)
  195. }
  196. dir = sub
  197. }
  198. return nil
  199. }
  200. func (dm *DirectoryManagerInMap) makeDirectory(dirPath string) (dir *DirectoryEntryInMap, created bool) {
  201. dirPath = CleanFilePath(dirPath)
  202. if dirPath == "/" {
  203. return dm.Root, false
  204. }
  205. parts := strings.Split(dirPath, "/")
  206. dir = dm.Root
  207. for i := 1; i < len(parts); i++ {
  208. sub, ok := dir.getChild(parts[i])
  209. if !ok {
  210. var err error
  211. sub, err = dm.NewDirectoryEntryInMap(dir, parts[i])
  212. if err != nil {
  213. return nil, false
  214. }
  215. dir.addChild(parts[i], sub)
  216. created = true
  217. }
  218. dir = sub
  219. }
  220. return dir, created
  221. }
  222. func (dm *DirectoryManagerInMap) MakeDirectory(dirPath string) (filer.DirectoryId, error) {
  223. dir, _ := dm.makeDirectory(dirPath)
  224. return dir.Id, nil
  225. }
  226. func (dm *DirectoryManagerInMap) MoveUnderDirectory(oldDirPath string, newParentDirPath string, newName string) error {
  227. writeLock.Lock()
  228. defer writeLock.Unlock()
  229. oldDir, oe := dm.findDirectory(oldDirPath)
  230. if oe != nil {
  231. return oe
  232. }
  233. parentDir, pe := dm.findDirectory(newParentDirPath)
  234. if pe != nil {
  235. return pe
  236. }
  237. dm.log("mov", oldDirPath, newParentDirPath, newName)
  238. oldDir.Parent.removeChild(oldDir.Name)
  239. if newName == "" {
  240. newName = oldDir.Name
  241. }
  242. parentDir.addChild(newName, oldDir)
  243. oldDir.Name = newName
  244. oldDir.Parent = parentDir
  245. return nil
  246. }
  247. func (dm *DirectoryManagerInMap) ListDirectories(dirPath string) (dirNames []filer.DirectoryEntry, err error) {
  248. d, e := dm.findDirectory(dirPath)
  249. if e != nil {
  250. return dirNames, e
  251. }
  252. return d.children(), nil
  253. }
  254. func (dm *DirectoryManagerInMap) DeleteDirectory(dirPath string) error {
  255. writeLock.Lock()
  256. defer writeLock.Unlock()
  257. if dirPath == "/" {
  258. return fmt.Errorf("Can not delete %s", dirPath)
  259. }
  260. d, e := dm.findDirectory(dirPath)
  261. if e != nil {
  262. return e
  263. }
  264. if d.hasChildren() {
  265. return fmt.Errorf("dir %s still has sub directories", dirPath)
  266. }
  267. d.Parent.removeChild(d.Name)
  268. d.Parent = nil
  269. dm.log("del", dirPath)
  270. return nil
  271. }
  272. func CleanFilePath(fp string) string {
  273. ret := filepath.Clean(fp)
  274. if os.PathSeparator == '\\' {
  275. return strings.Replace(ret, "\\", "/", -1)
  276. }
  277. return ret
  278. }