filer_server_handlers_write.go 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. rule := fs.filer.FilerConf.MatchStorageRule(src)
  140. if rule.Worm {
  141. // you cannot move a worm file or directory
  142. err = fmt.Errorf("cannot move write-once entry from '%s' to '%s': operation not permitted", src, dst)
  143. writeJsonError(w, r, http.StatusForbidden, err)
  144. return
  145. }
  146. oldDir, oldName := srcPath.DirAndName()
  147. newDir, newName := dstPath.DirAndName()
  148. newName = util.Nvl(newName, oldName)
  149. dstEntry, err := fs.filer.FindEntry(ctx, util.FullPath(strings.TrimRight(dst, "/")))
  150. if err != nil && err != filer_pb.ErrNotFound {
  151. err = fmt.Errorf("failed to get dst entry '%s', err: %s", dst, err)
  152. writeJsonError(w, r, http.StatusInternalServerError, err)
  153. return
  154. }
  155. if err == nil && !dstEntry.IsDirectory() && srcEntry.IsDirectory() {
  156. err = fmt.Errorf("move: cannot overwrite non-directory '%s' with directory '%s'", dst, src)
  157. writeJsonError(w, r, http.StatusBadRequest, err)
  158. return
  159. }
  160. _, err = fs.AtomicRenameEntry(ctx, &filer_pb.AtomicRenameEntryRequest{
  161. OldDirectory: oldDir,
  162. OldName: oldName,
  163. NewDirectory: newDir,
  164. NewName: newName,
  165. })
  166. if err != nil {
  167. err = fmt.Errorf("failed to move entry from '%s' to '%s', err: %s", src, dst, err)
  168. writeJsonError(w, r, http.StatusBadRequest, err)
  169. return
  170. }
  171. w.WriteHeader(http.StatusNoContent)
  172. }
  173. // curl -X DELETE http://localhost:8888/path/to
  174. // curl -X DELETE http://localhost:8888/path/to?recursive=true
  175. // curl -X DELETE http://localhost:8888/path/to?recursive=true&ignoreRecursiveError=true
  176. // curl -X DELETE http://localhost:8888/path/to?recursive=true&skipChunkDeletion=true
  177. func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
  178. isRecursive := r.FormValue("recursive") == "true"
  179. if !isRecursive && fs.option.recursiveDelete {
  180. if r.FormValue("recursive") != "false" {
  181. isRecursive = true
  182. }
  183. }
  184. ignoreRecursiveError := r.FormValue("ignoreRecursiveError") == "true"
  185. skipChunkDeletion := r.FormValue("skipChunkDeletion") == "true"
  186. objectPath := r.URL.Path
  187. if len(r.URL.Path) > 1 && strings.HasSuffix(objectPath, "/") {
  188. objectPath = objectPath[0 : len(objectPath)-1]
  189. }
  190. rule := fs.filer.FilerConf.MatchStorageRule(objectPath)
  191. if rule.Worm {
  192. writeJsonError(w, r, http.StatusForbidden, errors.New("operation not permitted"))
  193. return
  194. }
  195. err := fs.filer.DeleteEntryMetaAndData(context.Background(), util.FullPath(objectPath), isRecursive, ignoreRecursiveError, !skipChunkDeletion, false, nil, 0)
  196. if err != nil && err != filer_pb.ErrNotFound {
  197. glog.V(1).Infoln("deleting", objectPath, ":", err.Error())
  198. writeJsonError(w, r, http.StatusInternalServerError, err)
  199. return
  200. }
  201. w.WriteHeader(http.StatusNoContent)
  202. }
  203. func (fs *FilerServer) detectStorageOption(requestURI, qCollection, qReplication string, ttlSeconds int32, diskType, dataCenter, rack, dataNode string) (*operation.StorageOption, error) {
  204. rule := fs.filer.FilerConf.MatchStorageRule(requestURI)
  205. if rule.ReadOnly {
  206. return nil, ErrReadOnly
  207. }
  208. if rule.MaxFileNameLength == 0 {
  209. rule.MaxFileNameLength = fs.filer.MaxFilenameLength
  210. }
  211. // required by buckets folder
  212. bucketDefaultCollection := ""
  213. if strings.HasPrefix(requestURI, fs.filer.DirBucketsPath+"/") {
  214. bucketDefaultCollection = fs.filer.DetectBucket(util.FullPath(requestURI))
  215. }
  216. if ttlSeconds == 0 {
  217. ttl, err := needle.ReadTTL(rule.GetTtl())
  218. if err != nil {
  219. glog.Errorf("fail to parse %s ttl setting %s: %v", rule.LocationPrefix, rule.Ttl, err)
  220. }
  221. ttlSeconds = int32(ttl.Minutes()) * 60
  222. }
  223. return &operation.StorageOption{
  224. Replication: util.Nvl(qReplication, rule.Replication, fs.option.DefaultReplication),
  225. Collection: util.Nvl(qCollection, rule.Collection, bucketDefaultCollection, fs.option.Collection),
  226. DataCenter: util.Nvl(dataCenter, rule.DataCenter, fs.option.DataCenter),
  227. Rack: util.Nvl(rack, rule.Rack, fs.option.Rack),
  228. DataNode: util.Nvl(dataNode, rule.DataNode, fs.option.DataNode),
  229. TtlSeconds: ttlSeconds,
  230. DiskType: util.Nvl(diskType, rule.DiskType),
  231. Fsync: rule.Fsync,
  232. VolumeGrowthCount: rule.VolumeGrowthCount,
  233. MaxFileNameLength: rule.MaxFileNameLength,
  234. }, nil
  235. }
  236. func (fs *FilerServer) detectStorageOption0(requestURI, qCollection, qReplication string, qTtl string, diskType string, fsync string, dataCenter, rack, dataNode, saveInside string) (*operation.StorageOption, error) {
  237. ttl, err := needle.ReadTTL(qTtl)
  238. if err != nil {
  239. glog.Errorf("fail to parse ttl %s: %v", qTtl, err)
  240. }
  241. so, err := fs.detectStorageOption(requestURI, qCollection, qReplication, int32(ttl.Minutes())*60, diskType, dataCenter, rack, dataNode)
  242. if so != nil {
  243. if fsync == "false" {
  244. so.Fsync = false
  245. } else if fsync == "true" {
  246. so.Fsync = true
  247. }
  248. if saveInside == "true" {
  249. so.SaveInside = true
  250. } else {
  251. so.SaveInside = false
  252. }
  253. }
  254. return so, err
  255. }