From ac5982f905fd09c21bc114f899157031344ddcb7 Mon Sep 17 00:00:00 2001 From: Pierre-Olivier Mercier Date: Thu, 1 May 2025 13:57:45 +0200 Subject: [PATCH 1/2] fileexporter: Close opened fd --- fileexporter/archive.go | 26 +++++++++++++++++++++----- fileexporter/copy.go | 6 +++--- fileexporter/main.go | 18 +++++++++++++----- 3 files changed, 37 insertions(+), 13 deletions(-) diff --git a/fileexporter/archive.go b/fileexporter/archive.go index 19544724..d4fc1ceb 100644 --- a/fileexporter/archive.go +++ b/fileexporter/archive.go @@ -4,30 +4,46 @@ import ( "archive/zip" "errors" "io" + "log" "os" "path" ) type archiveFileCreator interface { Create(name string) (io.Writer, error) + Close() error +} + +type filesCloser []io.Closer + +func (fds filesCloser) Close() error { + log.Println("Closing fd..") + for _, fd := range fds { + err := fd.Close() + if err != nil { + return err + } + } + + return nil } func init() { - OutputFormats["archive"] = func(args ...string) (func(string) (io.WriteCloser, error), error) { + OutputFormats["archive"] = func(args ...string) (func(string) (io.WriteCloser, error), io.Closer, error) { if len(args) != 1 { - return nil, errors.New("archive has 1 required argument: [destination-file]") + return nil, nil, errors.New("archive has 1 required argument: [destination-file]") } fd, err := os.Create(args[0]) if err != nil { - return nil, err + return nil, nil, err } var w archiveFileCreator if path.Ext(args[0]) == ".zip" { w = zip.NewWriter(fd) } else { - return nil, errors.New("destination file has to have .zip extension") + return nil, nil, errors.New("destination file has to have .zip extension") } return func(dest string) (io.WriteCloser, error) { @@ -37,6 +53,6 @@ func init() { } return NopCloser(fw), nil - }, nil + }, filesCloser{w, fd}, nil } } diff --git a/fileexporter/copy.go b/fileexporter/copy.go index 6610b2c1..d4c7b96a 100644 --- a/fileexporter/copy.go +++ b/fileexporter/copy.go @@ -8,15 +8,15 @@ import ( ) func init() { - OutputFormats["copy"] = func(args ...string) (func(string) (io.WriteCloser, error), error) { + OutputFormats["copy"] = func(args ...string) (func(string) (io.WriteCloser, error), io.Closer, error) { if len(args) > 1 { - return nil, errors.New("copy can only take 1 argument: [destination-folder]") + return nil, nil, errors.New("copy can only take 1 argument: [destination-folder]") } if len(args) == 1 { fic.FilesDir = args[0] } - return nil, nil + return nil, nil, nil } } diff --git a/fileexporter/main.go b/fileexporter/main.go index 2fa95ab9..5fea7f6d 100644 --- a/fileexporter/main.go +++ b/fileexporter/main.go @@ -14,7 +14,7 @@ import ( "srs.epita.fr/fic-server/libfic" ) -var OutputFormats = map[string]func(...string) (func(string) (io.WriteCloser, error), error){} +var OutputFormats = map[string]func(...string) (func(string) (io.WriteCloser, error), io.Closer, error){} func exportThemeFiles(tdir string) (errs error) { theme, exceptions, err := sync.BuildTheme(sync.GlobalImporter, tdir) @@ -129,6 +129,13 @@ func main() { log.Println("Using", sync.GlobalImporter.Kind()) + hasError := doExport() + if hasError { + os.Exit(1) + } +} + +func doExport() bool { // Configure destination if flag.NArg() < 1 { var formats []string @@ -147,7 +154,10 @@ func main() { log.Fatal("Please define wanted output format between [" + strings.Join(formats, " ") + "]") } else { - fw, err := outputFormat(flag.Args()[1:]...) + fw, closer, err := outputFormat(flag.Args()[1:]...) + if closer != nil { + defer closer.Close() + } if err != nil { log.Fatal(err) } else if fw != nil { @@ -170,7 +180,5 @@ func main() { } } - if hasError { - os.Exit(1) - } + return hasError } From 57e2b6e0bfe96f7a159fadd2a7a3c3db1b4b7053 Mon Sep 17 00:00:00 2001 From: Renovate Bot Date: Mon, 9 Jun 2025 06:05:01 +0000 Subject: [PATCH 2/2] chore(deps): update dependency go to v1.24.4 --- go.mod | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/go.mod b/go.mod index 9ace7117..5ac87409 100644 --- a/go.mod +++ b/go.mod @@ -2,7 +2,7 @@ module srs.epita.fr/fic-server go 1.23.0 -toolchain go1.24.1 +toolchain go1.24.4 require ( github.com/BurntSushi/toml v1.5.0