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.

246 lines
7.4 KiB

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