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.

248 lines
7.4 KiB

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