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 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'config.go') 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() + } +} -- cgit v1.2.3