Skip to content

armgit / ARTEX

Public Imported from GitHub
0

ARTEX - AI autonomous penetration testing system (English translation; upstream: Autumn-27/ARTEX, AGPL-3.0)

fix(server): do not fsync the journal directory on Windows

BaaRai359329766470
diff --git a/server/sync_dir_other.go b/server/sync_dir_other.go
new file mode 100644
index 0000000..54245c7
--- /dev/null
+++ b/server/sync_dir_other.go
@@ -0,0 +1,8 @@
+//go:build !unix
+
+package server
+
+// syncDir does nothing here: Windows does not support fsync on a directory
+// handle (FlushFileBuffers fails on it), and the contents of the file written in
+// the directory are already synced by the caller.
+func syncDir(path string) error { return nil }
diff --git a/server/sync_dir_unix.go b/server/sync_dir_unix.go
new file mode 100644
index 0000000..8518f98
--- /dev/null
+++ b/server/sync_dir_unix.go
@@ -0,0 +1,18 @@
+//go:build unix
+
+package server
+
+import (
+ "errors"
+ "os"
+)
+
+// syncDir flushes a directory, so that an entry just created or renamed in it
+// survives a crash.
+func syncDir(path string) error {
+ d, err := os.Open(path)
+ if err != nil {
+ return err
+ }
+ return errors.Join(d.Sync(), d.Close())
+}
diff --git a/server/sync_dir_unix_test.go b/server/sync_dir_unix_test.go
new file mode 100644
index 0000000..3d22034
--- /dev/null
+++ b/server/sync_dir_unix_test.go
@@ -0,0 +1,18 @@
+//go:build unix
+
+package server
+
+import (
+ "path/filepath"
+ "testing"
+)
+
+func TestSyncDir(t *testing.T) {
+ dir := t.TempDir()
+ if err := syncDir(dir); err != nil {
+ t.Fatalf("sync of an existing directory: %v", err)
+ }
+ if err := syncDir(filepath.Join(dir, "missing")); err == nil {
+ t.Fatal("sync of a missing directory must fail")
+ }
+}
diff --git a/server/task_archive_journal_test.go b/server/task_archive_journal_test.go
new file mode 100644
index 0000000..486bdf4
--- /dev/null
+++ b/server/task_archive_journal_test.go
@@ -0,0 +1,27 @@
+package server
+
+import (
+ "encoding/json"
+ "os"
+ "testing"
+)
+
+// Writing the journal must succeed on every platform the server builds for. It
+// needs no database: on Windows the directory sync it ended with failed, so no
+// remote archive or remote deletion could start.
+func TestRemoteArchiveJournalWrite(t *testing.T) {
+ s := &Server{m: &Manager{dir: t.TempDir()}}
+ want := remoteArchiveJournal{ArchiveID: 7, URI: "s3://bucket/tasks/package.tar.zst", LocalPath: "package.tar.zst", Operation: "upload"}
+ name, err := s.writeRemoteArchiveJournal(want)
+ if err != nil {
+ t.Fatal(err)
+ }
+ raw, err := os.ReadFile(name)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var got remoteArchiveJournal
+ if err := json.Unmarshal(raw, &got); err != nil || got != want {
+ t.Fatalf("journal content %q: %v", raw, err)
+ }
+}
diff --git a/server/task_archive_remote.go b/server/task_archive_remote.go
index 87e230f..ee6b0ea 100644
--- a/server/task_archive_remote.go
+++ b/server/task_archive_remote.go
@@ -67,19 +67,11 @@ func (s *Server) writeRemoteArchiveJournal(j remoteArchiveJournal) (string, erro
return "", err
}
// Persist the directory entry before an upload or database deletion starts.
- d, err := os.Open(dir)
- if err != nil {
- return "", err
- }
- if err := errors.Join(d.Sync(), d.Close()); err != nil {
+ if err := syncDir(dir); err != nil {
return "", err
}
// The .remote directory itself may have just been created.
- parent, err := os.Open(filepath.Dir(dir))
- if err != nil {
- return "", err
- }
- return name, errors.Join(parent.Sync(), parent.Close())
+ return name, syncDir(filepath.Dir(dir))
}
func (s *Server) recoverRemoteArchiveJournals() error {