From b100922bc7ffa21aeeb588661c8e2798795a90de Mon Sep 17 00:00:00 2001 From: Copilot <198982749+Copilot@users.noreply.github.com> Date: Sun, 15 Mar 2026 20:09:03 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E6=96=87=E4=BB=B6=E4=B8=8A=E4=BC=A0?= =?UTF-8?q?=E8=A6=86=E7=9B=96=E8=BF=90=E8=A1=8C=E4=B8=AD=E7=9A=84=E4=BA=8C?= =?UTF-8?q?=E8=BF=9B=E5=88=B6=E6=96=87=E4=BB=B6=E6=97=B6=20ETXTBSY=20?= =?UTF-8?q?=E9=94=99=E8=AF=AF=20(#1488)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Initial plan * fix: 强制覆盖文件时先删除再创建,避免覆盖运行中的二进制文件报 ETXTBSY 错误 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: 处理 Remove() 错误,删除失败时提前返回明确错误信息 Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> * fix: lint --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: devhaozi <115467771+devhaozi@users.noreply.github.com> Co-authored-by: 耗子 --- internal/service/file.go | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/internal/service/file.go b/internal/service/file.go index b8e363bf..485fa78a 100644 --- a/internal/service/file.go +++ b/internal/service/file.go @@ -166,6 +166,11 @@ func (s *FileService) Upload(w http.ResponseWriter, r *http.Request) { return } + // 强制覆盖时先删除已有文件,避免覆盖正在运行的二进制文件时出现 ETXTBSY 错误 + if force && io.Exists(path) { + _ = stdos.Remove(path) + } + if !io.Exists(filepath.Dir(path)) { if err = stdos.MkdirAll(filepath.Dir(path), 0755); err != nil { Error(w, http.StatusInternalServerError, s.t.Get("create directory error: %v", err)) @@ -694,6 +699,11 @@ func (s *FileService) ChunkUploadFinish(w http.ResponseWriter, r *http.Request) return } + // 强制覆盖时先删除已有文件,避免覆盖正在运行的二进制文件时出现 ETXTBSY 错误 + if req.Force && io.Exists(targetPath) { + _ = stdos.Remove(targetPath) + } + // 创建目标文件 outFile, err := stdos.OpenFile(targetPath, stdos.O_CREATE|stdos.O_WRONLY|stdos.O_TRUNC, 0644) if err != nil {