From 22b28ba1e290fb839dfe6718ff1a8a5fd7fddf8e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=A8=E8=B5=AB=E7=84=B6?= Date: Tue, 23 Jul 2024 16:59:13 +0800 Subject: [PATCH] Add mtime for upload_blks_api --- fileserver/fileop.go | 16 ++++++++++++++-- server/repo-mgr.h | 1 + server/repo-op.c | 6 +++++- server/upload-file.c | 16 ++++++++++++++++ 4 files changed, 36 insertions(+), 3 deletions(-) diff --git a/fileserver/fileop.go b/fileserver/fileop.go index 513acec2..dfa21cb5 100644 --- a/fileserver/fileop.go +++ b/fileserver/fileop.go @@ -3109,6 +3109,15 @@ func doUploadBlks(rsp http.ResponseWriter, r *http.Request, fsm *recvData) *appE return &appError{nil, msg, http.StatusBadRequest} } + lastModifyStr := normalizeUTF8Path(r.FormValue("last_modify")) + var lastModify int64 + if lastModifyStr != "" { + t, err := time.Parse(time.RFC3339, lastModifyStr) + if err == nil { + lastModify = t.Unix() + } + } + fileName := normalizeUTF8Path(r.FormValue("file_name")) if fileName == "" { msg := "No file_name given.\n" @@ -3147,7 +3156,7 @@ func doUploadBlks(rsp http.ResponseWriter, r *http.Request, fsm *recvData) *appE return &appError{nil, msg, http.StatusBadRequest} } - fileID, appErr := commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user, fileSize, replaceExisted) + fileID, appErr := commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user, fileSize, replaceExisted, lastModify) if appErr != nil { return appErr } @@ -3173,7 +3182,7 @@ func doUploadBlks(rsp http.ResponseWriter, r *http.Request, fsm *recvData) *appE return nil } -func commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user string, fileSize int64, replace bool) (string, *appError) { +func commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user string, fileSize int64, replace bool, lastModify int64) (string, *appError) { repo := repomgr.Get(repoID) if repo == nil { msg := "Failed to get repo.\n" @@ -3218,6 +3227,9 @@ func commitFileBlocks(repoID, parentDir, fileName, blockIDsJSON, user string, fi } mtime := time.Now().Unix() + if lastModify > 0 { + mtime = lastModify + } mode := (syscall.S_IFREG | 0644) newDent := fsmgr.NewDirent(fileID, fileName, uint32(mode), mtime, user, fileSize) var names []string diff --git a/server/repo-mgr.h b/server/repo-mgr.h index c9b16bde..588501bd 100644 --- a/server/repo-mgr.h +++ b/server/repo-mgr.h @@ -364,6 +364,7 @@ seaf_repo_manager_commit_file_blocks (SeafRepoManager *mgr, const char *user, gint64 file_size, int replace_existed, + gint64 mtime, char **new_id, GError **error); diff --git a/server/repo-op.c b/server/repo-op.c index 69f207e2..c35de06c 100644 --- a/server/repo-op.c +++ b/server/repo-op.c @@ -1505,6 +1505,7 @@ seaf_repo_manager_commit_file_blocks (SeafRepoManager *mgr, const char *user, gint64 file_size, int replace_existed, + gint64 mtime, char **new_id, GError **error) { @@ -1563,9 +1564,12 @@ seaf_repo_manager_commit_file_blocks (SeafRepoManager *mgr, } rawdata_to_hex(sha1, hex, 20); + if (mtime <= 0) { + mtime = (gint64)time(NULL); + } new_dent = seaf_dirent_new (dir_version_from_repo_version(repo->version), hex, STD_FILE_MODE, file_name, - (gint64)time(NULL), user, file_size); + mtime, user, file_size); root_id = do_post_file_replace (repo, head_commit->root_id, canon_path, replace_existed, new_dent); diff --git a/server/upload-file.c b/server/upload-file.c index 88a8efa5..cc057b43 100755 --- a/server/upload-file.c +++ b/server/upload-file.c @@ -705,6 +705,8 @@ upload_blks_api_cb(evhtp_request_t *req, void *arg) { RecvFSM *fsm = arg; const char *parent_dir, *file_name, *size_str, *replace_str, *commitonly_str; + char *last_modify = NULL; + gint64 mtime = 0; GError *error = NULL; int error_code = -1; char *blockids_json; @@ -734,6 +736,11 @@ upload_blks_api_cb(evhtp_request_t *req, void *arg) file_size = atoll(size_str); commitonly_str = evhtp_kv_find (req->uri->query, "commitonly"); + last_modify = g_hash_table_lookup (fsm->form_kvs, "last_modify"); + if (last_modify) { + mtime = rfc3339_to_timestamp (last_modify); + } + if (!file_name || !parent_dir || !size_str || file_size < 0) { seaf_debug ("[upload-blks] No parent dir or file name given.\n"); send_error_reply (req, EVHTP_RES_BADREQ, "No parent dir or file name.\n"); @@ -792,6 +799,7 @@ upload_blks_api_cb(evhtp_request_t *req, void *arg) fsm->user, file_size, replace, + mtime, &new_file_id, &error); if (rc < 0) { @@ -1428,6 +1436,8 @@ update_blks_api_cb(evhtp_request_t *req, void *arg) { RecvFSM *fsm = arg; char *target_file, *parent_dir = NULL, *filename = NULL, *size_str = NULL; + char *last_modify = NULL; + gint64 mtime = 0; const char *commitonly_str; GError *error = NULL; int error_code = -1; @@ -1451,6 +1461,11 @@ update_blks_api_cb(evhtp_request_t *req, void *arg) return; } + last_modify = g_hash_table_lookup (fsm->form_kvs, "last_modify"); + if (last_modify) { + mtime = rfc3339_to_timestamp (last_modify); + } + parent_dir = g_path_get_dirname (target_file); filename = g_path_get_basename (target_file); @@ -1501,6 +1516,7 @@ update_blks_api_cb(evhtp_request_t *req, void *arg) fsm->user, file_size, 1, + mtime, &new_file_id, &error);