From ca92770b428709f3c8f577ea81b8944802394b85 Mon Sep 17 00:00:00 2001 From: Runxi Yu Date: Sat, 4 Jan 2025 22:04:06 +0800 Subject: Clean up directory file descriptors --- config.go | 17 +++++++++++++++++ go.mod | 2 +- go.sum | 4 ++-- main.go | 1 + 4 files changed, 21 insertions(+), 3 deletions(-) diff --git a/config.go b/config.go index 1ec7207..8ba30ad 100644 --- a/config.go +++ b/config.go @@ -3,6 +3,7 @@ package main import ( "bufio" "os" + "reflect" "sync" "go.lindenii.runxiyu.org/lindenii-common/misc" @@ -50,3 +51,19 @@ func prepare_dirs() error { } return nil } + +// clean_up_dirs closes all directories. It should only be called before +// program exit. +func clean_up_dirs() { + directories_mutex.Lock() + defer directories_mutex.Unlock() + directories_rv := reflect.ValueOf(directories) + directories_rt := directories_rv.Type() + for i := range directories_rt.NumField() { + dir, ok := directories_rv.Field(i).Interface().(misc.Dir_t) + if !ok { + panic("directories contains a field that's not misc.Dir_t") + } + dir.Close() + } +} diff --git a/go.mod b/go.mod index ad6be5f..5ea39e0 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,4 @@ module go.lindenii.runxiyu.org/maild go 1.23.4 -require go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9 +require go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104140324-f57ba68adb4d diff --git a/go.sum b/go.sum index bccd500..1d1725c 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,2 @@ -go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9 h1:9H+rOwTeOYBoi11NSXn4IN4j0/3zo5nT8HA2Ktrdur0= -go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104131426-7a480ad159b9/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA= +go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104140324-f57ba68adb4d h1:ilNgswXXIKwDK/8xvV0UH6FNZ1XpZHxm3s381xz2icM= +go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250104140324-f57ba68adb4d/go.mod h1:bOxuuGXA3UpbLb1lKohr2j2MVcGGLcqfAprGx9VCkMA= diff --git a/main.go b/main.go index 2d6b706..f71391d 100644 --- a/main.go +++ b/main.go @@ -23,6 +23,7 @@ func main() { if err != nil { panic(err) } + defer clean_up_dirs() listener, err := net.Listen("tcp", ":25") if err != nil { -- cgit v1.2.3