mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
filer: support appending to a file
This commit is contained in:
parent
1b8e3da295
commit
f98817cfe6
|
@ -121,14 +121,11 @@ func (fs *FilerServer) doPutAutoChunk(ctx context.Context, w http.ResponseWriter
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fs *FilerServer) saveMetaData(ctx context.Context, r *http.Request, fileName string, contentType string, so *operation.StorageOption, md5bytes []byte, fileChunks []*filer_pb.FileChunk, chunkOffset int64, content []byte) (filerResult *FilerPostResult, replyerr error) {
|
func isAppend(r *http.Request) bool {
|
||||||
|
return r.URL.Query().Get("op") == "append"
|
||||||
|
}
|
||||||
|
|
||||||
// maybe compact chunks
|
func (fs *FilerServer) saveMetaData(ctx context.Context, r *http.Request, fileName string, contentType string, so *operation.StorageOption, md5bytes []byte, fileChunks []*filer_pb.FileChunk, chunkOffset int64, content []byte) (filerResult *FilerPostResult, replyerr error) {
|
||||||
fileChunks, replyerr = filer.MaybeManifestize(fs.saveAsChunk(so), fileChunks)
|
|
||||||
if replyerr != nil {
|
|
||||||
glog.V(0).Infof("manifestize %s: %v", r.RequestURI, replyerr)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// detect file mode
|
// detect file mode
|
||||||
modeStr := r.URL.Query().Get("mode")
|
modeStr := r.URL.Query().Get("mode")
|
||||||
|
@ -149,25 +146,61 @@ func (fs *FilerServer) saveMetaData(ctx context.Context, r *http.Request, fileNa
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
glog.V(4).Infoln("saving", path)
|
var entry *filer.Entry
|
||||||
entry := &filer.Entry{
|
var mergedChunks []*filer_pb.FileChunk
|
||||||
FullPath: util.FullPath(path),
|
// when it is an append
|
||||||
Attr: filer.Attr{
|
if isAppend(r) {
|
||||||
Mtime: time.Now(),
|
existingEntry, findErr := fs.filer.FindEntry(ctx, util.FullPath(path))
|
||||||
Crtime: time.Now(),
|
if findErr != nil && findErr != filer_pb.ErrNotFound {
|
||||||
Mode: os.FileMode(mode),
|
glog.V(0).Infof("failing to find %s: %v", path, findErr)
|
||||||
Uid: OS_UID,
|
}
|
||||||
Gid: OS_GID,
|
entry = existingEntry
|
||||||
Replication: so.Replication,
|
|
||||||
Collection: so.Collection,
|
|
||||||
TtlSec: so.TtlSeconds,
|
|
||||||
Mime: contentType,
|
|
||||||
Md5: md5bytes,
|
|
||||||
FileSize: uint64(chunkOffset),
|
|
||||||
},
|
|
||||||
Chunks: fileChunks,
|
|
||||||
Content: content,
|
|
||||||
}
|
}
|
||||||
|
if entry != nil {
|
||||||
|
entry.Mtime = time.Now()
|
||||||
|
entry.Md5 = nil
|
||||||
|
// adjust chunk offsets
|
||||||
|
for _, chunk := range fileChunks {
|
||||||
|
chunk.Offset += int64(entry.FileSize)
|
||||||
|
}
|
||||||
|
mergedChunks = append(entry.Chunks, fileChunks...)
|
||||||
|
entry.FileSize += uint64(chunkOffset)
|
||||||
|
|
||||||
|
// TODO
|
||||||
|
if len(entry.Content) > 0 {
|
||||||
|
replyerr = fmt.Errorf("append to small file is not supported yet")
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
glog.V(4).Infoln("saving", path)
|
||||||
|
mergedChunks = fileChunks
|
||||||
|
entry = &filer.Entry{
|
||||||
|
FullPath: util.FullPath(path),
|
||||||
|
Attr: filer.Attr{
|
||||||
|
Mtime: time.Now(),
|
||||||
|
Crtime: time.Now(),
|
||||||
|
Mode: os.FileMode(mode),
|
||||||
|
Uid: OS_UID,
|
||||||
|
Gid: OS_GID,
|
||||||
|
Replication: so.Replication,
|
||||||
|
Collection: so.Collection,
|
||||||
|
TtlSec: so.TtlSeconds,
|
||||||
|
Mime: contentType,
|
||||||
|
Md5: md5bytes,
|
||||||
|
FileSize: uint64(chunkOffset),
|
||||||
|
},
|
||||||
|
Content: content,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// maybe compact entry chunks
|
||||||
|
mergedChunks, replyerr = filer.MaybeManifestize(fs.saveAsChunk(so), mergedChunks)
|
||||||
|
if replyerr != nil {
|
||||||
|
glog.V(0).Infof("manifestize %s: %v", r.RequestURI, replyerr)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
entry.Chunks = mergedChunks
|
||||||
|
|
||||||
filerResult = &FilerPostResult{
|
filerResult = &FilerPostResult{
|
||||||
Name: fileName,
|
Name: fileName,
|
||||||
|
@ -187,7 +220,7 @@ func (fs *FilerServer) saveMetaData(ctx context.Context, r *http.Request, fileNa
|
||||||
}
|
}
|
||||||
|
|
||||||
if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
|
if dbErr := fs.filer.CreateEntry(ctx, entry, false, false, nil); dbErr != nil {
|
||||||
fs.filer.DeleteChunks(entry.Chunks)
|
fs.filer.DeleteChunks(fileChunks)
|
||||||
replyerr = dbErr
|
replyerr = dbErr
|
||||||
filerResult.Error = dbErr.Error()
|
filerResult.Error = dbErr.Error()
|
||||||
glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
|
glog.V(0).Infof("failing to write %s to filer server : %v", path, dbErr)
|
||||||
|
@ -211,7 +244,7 @@ func (fs *FilerServer) uploadReaderToChunks(w http.ResponseWriter, r *http.Reque
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, nil, 0, err, nil
|
return nil, nil, 0, err, nil
|
||||||
}
|
}
|
||||||
if chunkOffset == 0 {
|
if chunkOffset == 0 && !isAppend(r) {
|
||||||
if len(data) < fs.option.SaveToFilerLimit || strings.HasPrefix(r.URL.Path, filer.DirectoryEtcRoot) && len(data) < 4*1024 {
|
if len(data) < fs.option.SaveToFilerLimit || strings.HasPrefix(r.URL.Path, filer.DirectoryEtcRoot) && len(data) < 4*1024 {
|
||||||
smallContent = data
|
smallContent = data
|
||||||
chunkOffset += int64(len(data))
|
chunkOffset += int64(len(data))
|
||||||
|
|
Loading…
Reference in a new issue