filer_server_handlers_write.go 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. package weed_server
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "net/http"
  7. "os"
  8. "strings"
  9. "time"
  10. "github.com/seaweedfs/seaweedfs/weed/s3api/s3_constants"
  11. "github.com/seaweedfs/seaweedfs/weed/glog"
  12. "github.com/seaweedfs/seaweedfs/weed/operation"
  13. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  14. "github.com/seaweedfs/seaweedfs/weed/security"
  15. "github.com/seaweedfs/seaweedfs/weed/stats"
  16. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  17. "github.com/seaweedfs/seaweedfs/weed/util"
  18. util_http "github.com/seaweedfs/seaweedfs/weed/util/http"
  19. )
  20. var (
  21. OS_UID = uint32(os.Getuid())
  22. OS_GID = uint32(os.Getgid())
  23. ErrReadOnly = errors.New("read only")
  24. )
  25. type FilerPostResult struct {
  26. Name string `json:"name,omitempty"`
  27. Size int64 `json:"size,omitempty"`
  28. Error string `json:"error,omitempty"`
  29. }
  30. func (fs *FilerServer) assignNewFileInfo(so *operation.StorageOption) (fileId, urlLocation string, auth security.EncodedJwt, err error) {
  31. stats.FilerHandlerCounter.WithLabelValues(stats.ChunkAssign).Inc()
  32. start := time.Now()
  33. defer func() {
  34. stats.FilerRequestHistogram.WithLabelValues(stats.ChunkAssign).Observe(time.Since(start).Seconds())
  35. }()
  36. ar, altRequest := so.ToAssignRequests(1)
  37. assignResult, ae := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, ar, altRequest)
  38. if ae != nil {
  39. glog.Errorf("failing to assign a file id: %v", ae)
  40. err = ae
  41. return
  42. }
  43. fileId = assignResult.Fid
  44. assignUrl := assignResult.Url
  45. // Prefer same data center
  46. if fs.option.DataCenter != "" {
  47. for _, repl := range assignResult.Replicas {
  48. if repl.DataCenter == fs.option.DataCenter {
  49. assignUrl = repl.Url
  50. break
  51. }
  52. }
  53. }
  54. urlLocation = "http://" + assignUrl + "/" + assignResult.Fid
  55. if so.Fsync {
  56. urlLocation += "?fsync=true"
  57. }
  58. auth = assignResult.Auth
  59. return
  60. }
  61. func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request, contentLength int64) {
  62. ctx := context.Background()
  63. destination := r.RequestURI
  64. if finalDestination := r.Header.Get(s3_constants.SeaweedStorageDestinationHeader); finalDestination != "" {
  65. destination = finalDestination
  66. }
  67. query := r.URL.Query()
  68. so, err := fs.detectStorageOption0(destination,
  69. query.Get("collection"),
  70. query.Get("replication"),
  71. query.Get("ttl"),
  72. query.Get("disk"),
  73. query.Get("fsync"),
  74. query.Get("dataCenter"),
  75. query.Get("rack"),
  76. query.Get("dataNode"),
  77. query.Get("saveInside"),
  78. )
  79. if err != nil {
  80. if err == ErrReadOnly {
  81. w.WriteHeader(http.StatusInsufficientStorage)
  82. } else {
  83. glog.V(1).Infoln("post", r.RequestURI, ":", err.Error())
  84. w.WriteHeader(http.StatusInternalServerError)
  85. }
  86. return
  87. }
  88. if util.FullPath(r.URL.Path).IsLongerFileName(so.MaxFileNameLength) {
  89. glog.V(1).Infoln("post", r.RequestURI, ": ", "entry name too long")
  90. w.WriteHeader(http.StatusRequestURITooLong)
  91. return
  92. }
  93. // When DiskType is empty,use filer's -disk
  94. if so.DiskType == "" {
  95. so.DiskType = fs.option.DiskType
  96. }
  97. if strings.HasPrefix(r.URL.Path, "/etc") {
  98. so.SaveInside = true
  99. }
  100. if query.Has("mv.from") {
  101. fs.move(ctx, w, r, so)
  102. } else {
  103. fs.autoChunk(ctx, w, r, contentLength, so)
  104. }
  105. util_http.CloseRequest(r)
  106. }
  107. func (fs *FilerServer) move(ctx context.Context, w http.ResponseWriter, r *http.Request, so *operation.StorageOption) {
  108. src := r.URL.Query().Get("mv.from")
  109. dst := r.URL.Path
  110. glog.V(2).Infof("FilerServer.move %v to %v", src, dst)
  111. var err error
  112. if src, err = clearName(src); err != nil {
  113. writeJsonError(w, r, http.StatusBadRequest, err)
  114. return
  115. }
  116. if dst, err = clearName(dst); err != nil {
  117. writeJsonError(w, r, http.StatusBadRequest, err)
  118. return
  119. }
  120. src = strings.TrimRight(src, "/")
  121. if src == "" {
  122. err = fmt.Errorf("invalid source '/'")
  123. writeJsonError(w, r, http.StatusBadRequest, err)
  124. return
  125. }
  126. srcPath := util.FullPath(src)
  127. dstPath := util.FullPath(dst)
  128. if dstPath.IsLongerFileName(so.MaxFileNameLength) {
  129. err = fmt.Errorf("dst name to long")
  130. writeJsonError(w, r, http.StatusBadRequest, err)
  131. return
  132. }
  133. srcEntry, err := fs.filer.FindEntry(ctx, srcPath)
  134. if err != nil {
  135. err = fmt.Errorf("failed to get src entry '%s', err: %s", src, err)
  136. writeJsonError(w, r, http.StatusBadRequest, err)
  137. return
  138. }
  139. oldDir, oldName := srcPath.DirAndName()
  140. newDir, newName := dstPath.DirAndName()
  141. newName = util.Nvl(newName, oldName)
  142. dstEntry, err := fs.filer.FindEntry(ctx, util.FullPath(strings.TrimRight(dst, "/")))
  143. if err != nil && err != filer_pb.ErrNotFound {
  144. err = fmt.Errorf("failed to get dst entry '%s', err: %s", dst, err)
  145. writeJsonError(w, r, http.StatusInternalServerError, err)
  146. return
  147. }
  148. if err == nil && !dstEntry.IsDirectory() && srcEntry.IsDirectory() {
  149. err = fmt.Errorf("move: cannot overwrite non-directory '%s' with directory '%s'", dst, src)
  150. writeJsonError(w, r, http.StatusBadRequest, err)
  151. return
  152. }
  153. _, err = fs.AtomicRenameEntry(ctx, &filer_pb.AtomicRenameEntryRequest{
  154. OldDirectory: oldDir,
  155. OldName: oldName,
  156. NewDirectory: newDir,
  157. NewName: newName,
  158. })
  159. if err != nil {
  160. err = fmt.Errorf("failed to move entry from '%s' to '%s', err: %s", src, dst, err)
  161. writeJsonError(w, r, http.StatusBadRequest, err)
  162. return
  163. }
  164. w.WriteHeader(http.StatusNoContent)
  165. }
  166. // curl -X DELETE http://localhost:8888/path/to
  167. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  168. // curl -X DELETE http://localhost:8888/path/to?recursive=true&ignoreRecursiveError=true
  169. // curl -X DELETE http://localhost:8888/path/to?recursive=true&skipChunkDeletion=true
  170. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  171. isRecursive := r.FormValue("recursive") == "true"
  172. if !isRecursive && fs.option.recursiveDelete {
  173. if r.FormValue("recursive") != "false" {
  174. isRecursive = true
  175. }
  176. }
  177. ignoreRecursiveError := r.FormValue("ignoreRecursiveError") == "true"
  178. skipChunkDeletion := r.FormValue("skipChunkDeletion") == "true"
  179. objectPath := r.URL.Path
  180. if len(r.URL.Path) > 1 && strings.HasSuffix(objectPath, "/") {
  181. objectPath = objectPath[0 : len(objectPath)-1]
  182. }
  183. err := fs.filer.DeleteEntryMetaAndData(context.Background(), util.FullPath(objectPath), isRecursive, ignoreRecursiveError, !skipChunkDeletion, false, nil, 0)
  184. if err != nil {
  185. if err == filer_pb.ErrNotFound {
  186. writeJsonQuiet(w, r, http.StatusNoContent, nil)
  187. return
  188. }
  189. glog.V(1).Infoln("deleting", objectPath, ":", err.Error())
  190. writeJsonError(w, r, http.StatusInternalServerError, err)
  191. return
  192. }
  193. w.WriteHeader(http.StatusNoContent)
  194. }
  195. func (fs *FilerServer) detectStorageOption(requestURI, qCollection, qReplication string, ttlSeconds int32, diskType, dataCenter, rack, dataNode string) (*operation.StorageOption, error) {
  196. rule := fs.filer.FilerConf.MatchStorageRule(requestURI)
  197. if rule.ReadOnly {
  198. return nil, ErrReadOnly
  199. }
  200. if rule.MaxFileNameLength == 0 {
  201. rule.MaxFileNameLength = fs.filer.MaxFilenameLength
  202. }
  203. // required by buckets folder
  204. bucketDefaultCollection := ""
  205. if strings.HasPrefix(requestURI, fs.filer.DirBucketsPath+"/") {
  206. bucketDefaultCollection = fs.filer.DetectBucket(util.FullPath(requestURI))
  207. }
  208. if ttlSeconds == 0 {
  209. ttl, err := needle.ReadTTL(rule.GetTtl())
  210. if err != nil {
  211. glog.Errorf("fail to parse %s ttl setting %s: %v", rule.LocationPrefix, rule.Ttl, err)
  212. }
  213. ttlSeconds = int32(ttl.Minutes()) * 60
  214. }
  215. return &operation.StorageOption{
  216. Replication: util.Nvl(qReplication, rule.Replication, fs.option.DefaultReplication),
  217. Collection: util.Nvl(qCollection, rule.Collection, bucketDefaultCollection, fs.option.Collection),
  218. DataCenter: util.Nvl(dataCenter, rule.DataCenter, fs.option.DataCenter),
  219. Rack: util.Nvl(rack, rule.Rack, fs.option.Rack),
  220. DataNode: util.Nvl(dataNode, rule.DataNode, fs.option.DataNode),
  221. TtlSeconds: ttlSeconds,
  222. DiskType: util.Nvl(diskType, rule.DiskType),
  223. Fsync: rule.Fsync,
  224. VolumeGrowthCount: rule.VolumeGrowthCount,
  225. MaxFileNameLength: rule.MaxFileNameLength,
  226. }, nil
  227. }
  228. func (fs *FilerServer) detectStorageOption0(requestURI, qCollection, qReplication string, qTtl string, diskType string, fsync string, dataCenter, rack, dataNode, saveInside string) (*operation.StorageOption, error) {
  229. ttl, err := needle.ReadTTL(qTtl)
  230. if err != nil {
  231. glog.Errorf("fail to parse ttl %s: %v", qTtl, err)
  232. }
  233. so, err := fs.detectStorageOption(requestURI, qCollection, qReplication, int32(ttl.Minutes())*60, diskType, dataCenter, rack, dataNode)
  234. if so != nil {
  235. if fsync == "false" {
  236. so.Fsync = false
  237. } else if fsync == "true" {
  238. so.Fsync = true
  239. }
  240. if saveInside == "true" {
  241. so.SaveInside = true
  242. } else {
  243. so.SaveInside = false
  244. }
  245. }
  246. return so, err
  247. }