file.go 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. /*
  2. * Rufs - Remote Union File System
  3. *
  4. * Copyright 2017 Matthias Ladkau. All rights reserved.
  5. *
  6. * This Source Code Form is subject to the terms of the MIT
  7. * License, If a copy of the MIT License was not distributed with this
  8. * file, You can obtain one at https://opensource.org/licenses/MIT.
  9. */
  10. package term
  11. import (
  12. "fmt"
  13. "os"
  14. "path"
  15. "path/filepath"
  16. "devt.de/krotik/common/bitutil"
  17. "devt.de/krotik/rufs"
  18. )
  19. /*
  20. cmdCat reads and prints the contents of a file.
  21. */
  22. func cmdCat(tt *TreeTerm, arg ...string) (string, error) {
  23. err := fmt.Errorf("cat requires a file path")
  24. if len(arg) > 0 {
  25. err = tt.tree.ReadFileToBuffer(tt.parsePathParam(arg[0]), tt.out)
  26. }
  27. return "", err
  28. }
  29. /*
  30. cmdGet Retrieve a file and store it locally (in the current directory).
  31. */
  32. func cmdGet(tt *TreeTerm, arg ...string) (string, error) {
  33. var res string
  34. lenArg := len(arg)
  35. err := fmt.Errorf("get requires at least a source file path")
  36. if lenArg > 0 {
  37. var f *os.File
  38. src := tt.parsePathParam(arg[0])
  39. dst := src
  40. if lenArg > 1 {
  41. dst = tt.parsePathParam(arg[1])
  42. }
  43. // Make sure we only write files to the local folder
  44. _, dst = filepath.Split(dst)
  45. if f, err = os.OpenFile(dst, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0660); err == nil {
  46. defer f.Close()
  47. if err = tt.tree.ReadFileToBuffer(src, f); err == nil {
  48. res = fmt.Sprintf("Written file %s", dst)
  49. }
  50. }
  51. }
  52. return res, err
  53. }
  54. /*
  55. cmdPut Read a local file and store it.
  56. */
  57. func cmdPut(tt *TreeTerm, arg ...string) (string, error) {
  58. var res string
  59. lenArg := len(arg)
  60. err := fmt.Errorf("put requires a source and destination file path")
  61. if lenArg > 0 {
  62. var f *os.File
  63. src := arg[0]
  64. dst := tt.parsePathParam(arg[1])
  65. if f, err = os.Open(src); err == nil {
  66. defer f.Close()
  67. if err = tt.tree.WriteFileFromBuffer(dst, f); err == nil {
  68. res = fmt.Sprintf("Written file %s", dst)
  69. }
  70. }
  71. }
  72. return res, err
  73. }
  74. /*
  75. cmdRm Delete a file or directory.
  76. */
  77. func cmdRm(tt *TreeTerm, arg ...string) (string, error) {
  78. var res string
  79. lenArg := len(arg)
  80. err := fmt.Errorf("rm requires a file path")
  81. if lenArg > 0 {
  82. p := tt.parsePathParam(arg[0])
  83. dir, file := path.Split(p)
  84. if file == "" {
  85. // If a path is give just chop off the last slash and try again
  86. dir, file = path.Split(dir[:len(dir)-1])
  87. }
  88. _, err = tt.tree.ItemOp(dir, map[string]string{
  89. rufs.ItemOpAction: rufs.ItemOpActDelete,
  90. rufs.ItemOpName: file,
  91. })
  92. }
  93. return res, err
  94. }
  95. /*
  96. cmdRen Rename a file or directory.
  97. */
  98. func cmdRen(tt *TreeTerm, arg ...string) (string, error) {
  99. var res string
  100. lenArg := len(arg)
  101. err := fmt.Errorf("ren requires a filename and a new filename")
  102. if lenArg > 1 {
  103. p := tt.parsePathParam(arg[0])
  104. p2 := tt.parsePathParam(arg[1])
  105. dir1, file1 := path.Split(p)
  106. dir2, file2 := path.Split(p2)
  107. if file2 == "" || dir2 != "/" {
  108. err = fmt.Errorf("new filename must not have a path")
  109. } else {
  110. if file1 == "" {
  111. // If a path is give just chop off the last slash and try again
  112. dir1, file1 = path.Split(dir1[:len(dir1)-1])
  113. }
  114. _, err = tt.tree.ItemOp(dir1, map[string]string{
  115. rufs.ItemOpAction: rufs.ItemOpActRename,
  116. rufs.ItemOpName: file1,
  117. rufs.ItemOpNewName: file2,
  118. })
  119. }
  120. }
  121. return res, err
  122. }
  123. /*
  124. cmdMkDir Create a new direectory.
  125. */
  126. func cmdMkDir(tt *TreeTerm, arg ...string) (string, error) {
  127. var res string
  128. lenArg := len(arg)
  129. err := fmt.Errorf("mkdir requires a directory path")
  130. if lenArg > 0 {
  131. p := tt.parsePathParam(arg[0])
  132. dir, newdir := path.Split(p)
  133. if newdir == "" {
  134. // If a path is given just chop off the last slash and try again
  135. dir, newdir = path.Split(dir[:len(dir)-1])
  136. }
  137. _, err = tt.tree.ItemOp(dir, map[string]string{
  138. rufs.ItemOpAction: rufs.ItemOpActMkDir,
  139. rufs.ItemOpName: newdir,
  140. })
  141. }
  142. return res, err
  143. }
  144. /*
  145. cmdCp Copy a file.
  146. */
  147. func cmdCp(tt *TreeTerm, arg ...string) (string, error) {
  148. var res string
  149. lenArg := len(arg)
  150. err := fmt.Errorf("cp requires a source file or directory and a destination directory")
  151. if lenArg > 1 {
  152. src := tt.parsePathParam(arg[0])
  153. dst := tt.parsePathParam(arg[1])
  154. updFunc := func(file string, writtenBytes, totalBytes, currentFile, totalFiles int64) {
  155. if writtenBytes > 0 {
  156. tt.WriteStatus(fmt.Sprintf("Copy %v: %v / %v (%v of %v)", file,
  157. bitutil.ByteSizeString(writtenBytes, false),
  158. bitutil.ByteSizeString(totalBytes, false),
  159. currentFile, totalFiles))
  160. } else {
  161. tt.ClearStatus()
  162. }
  163. }
  164. if err = tt.tree.Copy([]string{src}, dst, updFunc); err == nil {
  165. res = "Done"
  166. }
  167. }
  168. return res, err
  169. }