2016-06-03 11:05:34 +08:00
|
|
|
package weed_server
|
|
|
|
|
|
|
|
import (
|
2019-03-16 06:26:09 +08:00
|
|
|
"context"
|
2021-06-04 16:03:41 +08:00
|
|
|
"errors"
|
2022-02-20 23:30:42 +08:00
|
|
|
"fmt"
|
2022-05-31 13:47:26 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/s3api/s3_constants"
|
2016-06-03 11:05:34 +08:00
|
|
|
"net/http"
|
2019-02-15 16:09:19 +08:00
|
|
|
"os"
|
2017-01-08 22:34:26 +08:00
|
|
|
"strings"
|
2018-05-28 14:59:49 +08:00
|
|
|
"time"
|
2017-01-08 09:16:29 +08:00
|
|
|
|
2016-06-03 11:05:34 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/glog"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/operation"
|
2018-05-28 02:52:26 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/filer_pb"
|
2019-02-15 16:09:19 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/security"
|
2019-06-16 03:21:44 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/stats"
|
2020-03-09 15:16:10 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
2016-06-03 11:05:34 +08:00
|
|
|
"github.com/chrislusf/seaweedfs/weed/util"
|
2018-07-22 08:47:59 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
OS_UID = uint32(os.Getuid())
|
|
|
|
OS_GID = uint32(os.Getgid())
|
2021-06-04 16:03:41 +08:00
|
|
|
|
|
|
|
ErrReadOnly = errors.New("read only")
|
2016-06-03 11:05:34 +08:00
|
|
|
)
|
|
|
|
|
2016-06-03 11:44:50 +08:00
|
|
|
type FilerPostResult struct {
|
|
|
|
Name string `json:"name,omitempty"`
|
2020-02-04 09:04:06 +08:00
|
|
|
Size int64 `json:"size,omitempty"`
|
2016-06-03 11:44:50 +08:00
|
|
|
Error string `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:58:48 +08:00
|
|
|
func (fs *FilerServer) assignNewFileInfo(so *operation.StorageOption) (fileId, urlLocation string, auth security.EncodedJwt, err error) {
|
2019-06-23 13:53:52 +08:00
|
|
|
|
|
|
|
stats.FilerRequestCounter.WithLabelValues("assign").Inc()
|
|
|
|
start := time.Now()
|
|
|
|
defer func() { stats.FilerRequestHistogram.WithLabelValues("assign").Observe(time.Since(start).Seconds()) }()
|
|
|
|
|
2020-11-16 08:58:48 +08:00
|
|
|
ar, altRequest := so.ToAssignRequests(1)
|
2018-07-22 01:39:02 +08:00
|
|
|
|
2021-02-18 12:55:55 +08:00
|
|
|
assignResult, ae := operation.Assign(fs.filer.GetMaster, fs.grpcDialOption, ar, altRequest)
|
2016-06-08 15:46:14 +08:00
|
|
|
if ae != nil {
|
2018-07-22 01:39:02 +08:00
|
|
|
glog.Errorf("failing to assign a file id: %v", ae)
|
2016-06-08 15:46:14 +08:00
|
|
|
err = ae
|
|
|
|
return
|
|
|
|
}
|
|
|
|
fileId = assignResult.Fid
|
|
|
|
urlLocation = "http://" + assignResult.Url + "/" + assignResult.Fid
|
2020-11-16 06:41:56 +08:00
|
|
|
if so.Fsync {
|
2020-04-12 14:37:10 +08:00
|
|
|
urlLocation += "?fsync=true"
|
|
|
|
}
|
2019-02-15 16:09:19 +08:00
|
|
|
auth = assignResult.Auth
|
2016-06-08 15:46:14 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-03-31 12:07:34 +08:00
|
|
|
func (fs *FilerServer) PostHandler(w http.ResponseWriter, r *http.Request, contentLength int64) {
|
2016-08-06 06:01:30 +08:00
|
|
|
|
2019-03-16 06:55:34 +08:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-05-31 13:47:26 +08:00
|
|
|
destination := r.RequestURI
|
|
|
|
if finalDestination := r.Header.Get(s3_constants.SeaweedStorageDestinationHeader); finalDestination != "" {
|
|
|
|
destination = finalDestination
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:05:34 +08:00
|
|
|
query := r.URL.Query()
|
2022-05-31 13:47:26 +08:00
|
|
|
so, err := fs.detectStorageOption0(destination,
|
2020-11-16 08:58:48 +08:00
|
|
|
query.Get("collection"),
|
|
|
|
query.Get("replication"),
|
|
|
|
query.Get("ttl"),
|
2020-12-14 04:05:31 +08:00
|
|
|
query.Get("disk"),
|
2021-12-27 09:28:47 +08:00
|
|
|
query.Get("fsync"),
|
2020-11-16 08:58:48 +08:00
|
|
|
query.Get("dataCenter"),
|
|
|
|
query.Get("rack"),
|
2021-12-22 21:57:26 +08:00
|
|
|
query.Get("dataNode"),
|
2020-11-16 08:58:48 +08:00
|
|
|
)
|
2021-06-04 16:03:41 +08:00
|
|
|
if err != nil {
|
|
|
|
if err == ErrReadOnly {
|
|
|
|
w.WriteHeader(http.StatusInsufficientStorage)
|
|
|
|
} else {
|
|
|
|
glog.V(1).Infoln("post", r.RequestURI, ":", err.Error())
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-11-16 06:41:56 +08:00
|
|
|
|
2022-02-24 07:34:42 +08:00
|
|
|
if query.Has("mv.from") {
|
2022-02-20 23:30:42 +08:00
|
|
|
fs.move(ctx, w, r, so)
|
|
|
|
} else {
|
|
|
|
fs.autoChunk(ctx, w, r, contentLength, so)
|
|
|
|
}
|
|
|
|
|
2020-11-30 18:45:00 +08:00
|
|
|
util.CloseRequest(r)
|
2019-06-23 13:53:52 +08:00
|
|
|
|
2016-06-03 11:05:34 +08:00
|
|
|
}
|
|
|
|
|
2022-02-20 23:30:42 +08:00
|
|
|
func (fs *FilerServer) move(ctx context.Context, w http.ResponseWriter, r *http.Request, so *operation.StorageOption) {
|
2022-02-24 07:34:42 +08:00
|
|
|
src := r.URL.Query().Get("mv.from")
|
2022-02-20 23:30:42 +08:00
|
|
|
dst := r.URL.Path
|
|
|
|
|
|
|
|
glog.V(2).Infof("FilerServer.move %v to %v", src, dst)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
if src, err = clearName(src); err != nil {
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if dst, err = clearName(dst); err != nil {
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
src = strings.TrimRight(src, "/")
|
|
|
|
if src == "" {
|
|
|
|
err = fmt.Errorf("invalid source '/'")
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
srcPath := util.FullPath(src)
|
|
|
|
dstPath := util.FullPath(dst)
|
|
|
|
srcEntry, err := fs.filer.FindEntry(ctx, srcPath)
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("failed to get src entry '%s', err: %s", src, err)
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
oldDir, oldName := srcPath.DirAndName()
|
|
|
|
newDir, newName := dstPath.DirAndName()
|
|
|
|
newName = util.Nvl(newName, oldName)
|
|
|
|
|
|
|
|
dstEntry, err := fs.filer.FindEntry(ctx, util.FullPath(strings.TrimRight(dst, "/")))
|
|
|
|
if err != nil && err != filer_pb.ErrNotFound {
|
|
|
|
err = fmt.Errorf("failed to get dst entry '%s', err: %s", dst, err)
|
|
|
|
writeJsonError(w, r, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err == nil && !dstEntry.IsDirectory() && srcEntry.IsDirectory() {
|
|
|
|
err = fmt.Errorf("move: cannot overwrite non-directory '%s' with directory '%s'", dst, src)
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = fs.AtomicRenameEntry(ctx, &filer_pb.AtomicRenameEntryRequest{
|
|
|
|
OldDirectory: oldDir,
|
|
|
|
OldName: oldName,
|
|
|
|
NewDirectory: newDir,
|
|
|
|
NewName: newName,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
err = fmt.Errorf("failed to move entry from '%s' to '%s', err: %s", src, dst, err)
|
|
|
|
writeJsonError(w, r, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusNoContent)
|
|
|
|
}
|
|
|
|
|
2016-06-03 11:05:34 +08:00
|
|
|
// curl -X DELETE http://localhost:8888/path/to
|
2018-07-25 13:33:26 +08:00
|
|
|
// curl -X DELETE http://localhost:8888/path/to?recursive=true
|
2019-09-12 11:26:20 +08:00
|
|
|
// curl -X DELETE http://localhost:8888/path/to?recursive=true&ignoreRecursiveError=true
|
2019-12-12 06:58:22 +08:00
|
|
|
// curl -X DELETE http://localhost:8888/path/to?recursive=true&skipChunkDeletion=true
|
2016-06-03 11:05:34 +08:00
|
|
|
func (fs *FilerServer) DeleteHandler(w http.ResponseWriter, r *http.Request) {
|
2018-05-14 14:56:16 +08:00
|
|
|
|
2018-07-25 13:33:26 +08:00
|
|
|
isRecursive := r.FormValue("recursive") == "true"
|
2020-01-01 03:52:54 +08:00
|
|
|
if !isRecursive && fs.option.recursiveDelete {
|
|
|
|
if r.FormValue("recursive") != "false" {
|
|
|
|
isRecursive = true
|
|
|
|
}
|
|
|
|
}
|
2019-09-12 11:26:20 +08:00
|
|
|
ignoreRecursiveError := r.FormValue("ignoreRecursiveError") == "true"
|
2019-12-12 06:58:22 +08:00
|
|
|
skipChunkDeletion := r.FormValue("skipChunkDeletion") == "true"
|
2018-07-25 13:33:26 +08:00
|
|
|
|
2020-06-23 01:01:00 +08:00
|
|
|
objectPath := r.URL.Path
|
|
|
|
if len(r.URL.Path) > 1 && strings.HasSuffix(objectPath, "/") {
|
|
|
|
objectPath = objectPath[0 : len(objectPath)-1]
|
|
|
|
}
|
|
|
|
|
2020-08-29 14:48:48 +08:00
|
|
|
err := fs.filer.DeleteEntryMetaAndData(context.Background(), util.FullPath(objectPath), isRecursive, ignoreRecursiveError, !skipChunkDeletion, false, nil)
|
2018-05-14 14:56:16 +08:00
|
|
|
if err != nil {
|
2020-06-23 01:01:00 +08:00
|
|
|
glog.V(1).Infoln("deleting", objectPath, ":", err.Error())
|
2019-12-19 13:04:40 +08:00
|
|
|
httpStatus := http.StatusInternalServerError
|
2020-03-08 09:01:39 +08:00
|
|
|
if err == filer_pb.ErrNotFound {
|
2021-02-03 16:40:31 +08:00
|
|
|
httpStatus = http.StatusNoContent
|
2019-12-19 13:04:40 +08:00
|
|
|
}
|
|
|
|
writeJsonError(w, r, httpStatus, err)
|
2018-05-14 14:56:16 +08:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-07-22 09:49:47 +08:00
|
|
|
w.WriteHeader(http.StatusNoContent)
|
2016-06-03 11:05:34 +08:00
|
|
|
}
|
2020-02-25 14:28:45 +08:00
|
|
|
|
2021-12-22 21:57:26 +08:00
|
|
|
func (fs *FilerServer) detectStorageOption(requestURI, qCollection, qReplication string, ttlSeconds int32, diskType, dataCenter, rack, dataNode string) (*operation.StorageOption, error) {
|
2020-02-25 14:28:45 +08:00
|
|
|
|
2020-11-16 08:58:48 +08:00
|
|
|
rule := fs.filer.FilerConf.MatchStorageRule(requestURI)
|
|
|
|
|
2021-06-04 16:03:41 +08:00
|
|
|
if rule.ReadOnly {
|
|
|
|
return nil, ErrReadOnly
|
|
|
|
}
|
|
|
|
|
2021-08-14 17:54:13 +08:00
|
|
|
// required by buckets folder
|
2022-06-06 15:39:35 +08:00
|
|
|
bucketDefaultCollection := ""
|
2021-08-14 17:54:13 +08:00
|
|
|
if strings.HasPrefix(requestURI, fs.filer.DirBucketsPath+"/") {
|
|
|
|
bucketDefaultCollection = fs.filer.DetectBucket(util.FullPath(requestURI))
|
|
|
|
}
|
|
|
|
|
2020-11-16 08:58:48 +08:00
|
|
|
if ttlSeconds == 0 {
|
|
|
|
ttl, err := needle.ReadTTL(rule.GetTtl())
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("fail to parse %s ttl setting %s: %v", rule.LocationPrefix, rule.Ttl, err)
|
|
|
|
}
|
2020-11-16 08:59:28 +08:00
|
|
|
ttlSeconds = int32(ttl.Minutes()) * 60
|
2020-11-16 08:58:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return &operation.StorageOption{
|
2022-06-06 15:39:35 +08:00
|
|
|
Replication: util.Nvl(qReplication, rule.Replication, fs.option.DefaultReplication),
|
2021-08-14 17:54:13 +08:00
|
|
|
Collection: util.Nvl(qCollection, rule.Collection, bucketDefaultCollection, fs.option.Collection),
|
2021-12-23 23:24:10 +08:00
|
|
|
DataCenter: util.Nvl(dataCenter, rule.DataCenter, fs.option.DataCenter),
|
|
|
|
Rack: util.Nvl(rack, rule.Rack, fs.option.Rack),
|
|
|
|
DataNode: util.Nvl(dataNode, rule.DataNode, fs.option.DataNode),
|
2020-11-17 17:00:02 +08:00
|
|
|
TtlSeconds: ttlSeconds,
|
2020-12-17 01:10:14 +08:00
|
|
|
DiskType: util.Nvl(diskType, rule.DiskType),
|
2022-06-06 15:39:35 +08:00
|
|
|
Fsync: rule.Fsync,
|
2020-11-17 17:00:02 +08:00
|
|
|
VolumeGrowthCount: rule.VolumeGrowthCount,
|
2021-06-04 16:03:41 +08:00
|
|
|
}, nil
|
2020-11-16 08:58:48 +08:00
|
|
|
}
|
|
|
|
|
2021-12-27 09:28:47 +08:00
|
|
|
func (fs *FilerServer) detectStorageOption0(requestURI, qCollection, qReplication string, qTtl string, diskType string, fsync string, dataCenter, rack, dataNode string) (*operation.StorageOption, error) {
|
2020-11-16 08:58:48 +08:00
|
|
|
|
|
|
|
ttl, err := needle.ReadTTL(qTtl)
|
|
|
|
if err != nil {
|
|
|
|
glog.Errorf("fail to parse ttl %s: %v", qTtl, err)
|
|
|
|
}
|
|
|
|
|
2021-12-27 09:28:47 +08:00
|
|
|
so, err := fs.detectStorageOption(requestURI, qCollection, qReplication, int32(ttl.Minutes())*60, diskType, dataCenter, rack, dataNode)
|
|
|
|
if so != nil {
|
2022-05-19 23:17:17 +08:00
|
|
|
if fsync == "false" {
|
|
|
|
so.Fsync = false
|
|
|
|
} else if fsync == "true" {
|
|
|
|
so.Fsync = true
|
|
|
|
}
|
2021-12-27 09:28:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return so, err
|
2020-02-25 14:28:45 +08:00
|
|
|
}
|