diff --git a/pkg/building/boards.go b/pkg/building/boards.go index 9ca5c06d..7a950178 100644 --- a/pkg/building/boards.go +++ b/pkg/building/boards.go @@ -156,7 +156,7 @@ func BuildBoardPages(board *gcsql.Board) error { // Open 1.html for writing to the first page. boardPageFile, err = os.OpenFile(path.Join(criticalCfg.DocumentRoot, board.Dir, "1.html"), - os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE) + os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode) if err != nil { errEv.Err(err).Caller(). Str("page", "board.html"). @@ -198,7 +198,7 @@ func BuildBoardPages(board *gcsql.Board) error { var catalogPages boardCatalog // catalog JSON file is built with the pages because pages are recorded in the JSON file - catalogJSONFile, err := os.OpenFile(path.Join(criticalCfg.DocumentRoot, board.Dir, "catalog.json"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE) + catalogJSONFile, err := os.OpenFile(path.Join(criticalCfg.DocumentRoot, board.Dir, "catalog.json"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode) if err != nil { errEv.Err(err).Caller(). Msg("Failed opening catalog.json") @@ -215,7 +215,7 @@ func BuildBoardPages(board *gcsql.Board) error { var currentPageFilepath string pageFilename := strconv.Itoa(catalog.currentPage) + ".html" currentPageFilepath = path.Join(criticalCfg.DocumentRoot, board.Dir, pageFilename) - currentPageFile, err = os.OpenFile(currentPageFilepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE) + currentPageFile, err = os.OpenFile(currentPageFilepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode) if err != nil { errEv.Err(err).Caller(). Str("page", pageFilename). @@ -414,7 +414,7 @@ func buildBoard(board *gcsql.Board, force bool) error { Str("dirPath", dirPath).Send() return fmt.Errorf(dirIsAFileStr, dirPath) } - } else if err = os.Mkdir(dirPath, config.GC_DIR_MODE); err != nil { + } else if err = os.Mkdir(dirPath, config.DirFileMode); err != nil { errEv.Err(os.ErrExist).Caller(). Str("dirPath", dirPath).Send() return fmt.Errorf(genericErrStr, dirPath, err.Error()) @@ -438,7 +438,7 @@ func buildBoard(board *gcsql.Board, force bool) error { Str("resPath", resPath).Send() return err } - } else if err = os.Mkdir(resPath, config.GC_DIR_MODE); err != nil { + } else if err = os.Mkdir(resPath, config.DirFileMode); err != nil { err = fmt.Errorf(genericErrStr, resPath, err.Error()) errEv.Err(err).Caller(). Str("resPath", resPath).Send() @@ -463,7 +463,7 @@ func buildBoard(board *gcsql.Board, force bool) error { Str("srcPath", srcPath).Send() return err } - } else if err = os.Mkdir(srcPath, config.GC_DIR_MODE); err != nil { + } else if err = os.Mkdir(srcPath, config.DirFileMode); err != nil { err = fmt.Errorf(genericErrStr, srcPath, err.Error()) errEv.Err(err).Caller(). Str("srcPath", srcPath).Send() @@ -482,7 +482,7 @@ func buildBoard(board *gcsql.Board, force bool) error { if !thumbInfo.IsDir() { return fmt.Errorf(dirIsAFileStr, thumbPath) } - } else if err = os.Mkdir(thumbPath, config.GC_DIR_MODE); err != nil { + } else if err = os.Mkdir(thumbPath, config.DirFileMode); err != nil { errEv.Err(err).Caller(). Str("thumbPath", thumbPath).Send() return fmt.Errorf(genericErrStr, thumbPath, err.Error()) @@ -525,7 +525,7 @@ func buildBoard(board *gcsql.Board, force bool) error { // BuildBoardListJSON generates a JSON file with info about the boards func BuildBoardListJSON() error { boardsJsonPath := path.Join(config.GetSystemCriticalConfig().DocumentRoot, "boards.json") - boardListFile, err := os.OpenFile(boardsJsonPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE) + boardListFile, err := os.OpenFile(boardsJsonPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode) errEv := gcutil.LogError(nil).Str("building", "boards.json") defer errEv.Discard() if err != nil { diff --git a/pkg/building/building.go b/pkg/building/building.go index 0bd984e8..da5a6673 100644 --- a/pkg/building/building.go +++ b/pkg/building/building.go @@ -92,7 +92,7 @@ func BuildFrontPage() error { criticalCfg := config.GetSystemCriticalConfig() os.Remove(path.Join(criticalCfg.DocumentRoot, "index.html")) - frontFile, err := os.OpenFile(path.Join(criticalCfg.DocumentRoot, "index.html"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE) + frontFile, err := os.OpenFile(path.Join(criticalCfg.DocumentRoot, "index.html"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode) if err != nil { errEv.Err(err).Caller().Send() return errors.New("Failed opening front page for writing: " + err.Error()) @@ -160,7 +160,7 @@ func BuildJS() error { boardCfg := config.GetBoardConfig("") criticalCfg := config.GetSystemCriticalConfig() constsJSPath := path.Join(criticalCfg.DocumentRoot, "js", "consts.js") - constsJSFile, err := os.OpenFile(constsJSPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, config.GC_FILE_MODE) + constsJSFile, err := os.OpenFile(constsJSPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, config.NormalFileMode) if err != nil { errEv.Err(err).Caller().Send() return fmt.Errorf("error opening consts.js for writing: %s", err.Error()) diff --git a/pkg/building/catalog.go b/pkg/building/catalog.go index 70e7e0d0..dabe396f 100644 --- a/pkg/building/catalog.go +++ b/pkg/building/catalog.go @@ -92,7 +92,7 @@ func BuildCatalog(boardID int) error { errEv.Str("boardDir", board.Dir) criticalCfg := config.GetSystemCriticalConfig() catalogPath := path.Join(criticalCfg.DocumentRoot, board.Dir, "catalog.html") - catalogFile, err := os.OpenFile(catalogPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE) + catalogFile, err := os.OpenFile(catalogPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode) if err != nil { errEv.Err(err).Caller().Send() return fmt.Errorf("failed opening /%s/catalog.html: %s", board.Dir, err.Error()) diff --git a/pkg/building/threads.go b/pkg/building/threads.go index 29d6c289..fab082e5 100644 --- a/pkg/building/threads.go +++ b/pkg/building/threads.go @@ -81,7 +81,7 @@ func BuildThreadPages(op *gcsql.Post) error { os.Remove(path.Join(criticalCfg.DocumentRoot, board.Dir, "res", strconv.Itoa(op.ID)+".json")) threadPageFilepath := path.Join(criticalCfg.DocumentRoot, board.Dir, "res", strconv.Itoa(op.ID)+".html") - threadPageFile, err = os.OpenFile(threadPageFilepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE) + threadPageFile, err = os.OpenFile(threadPageFilepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode) if err != nil { errEv.Err(err).Caller().Send() return fmt.Errorf("unable to open /%s/res/%d.html: %s", board.Dir, op.ID, err.Error()) @@ -117,7 +117,7 @@ func BuildThreadPages(op *gcsql.Post) error { // Put together the thread JSON threadJSONFile, err := os.OpenFile( path.Join(criticalCfg.DocumentRoot, board.Dir, "res", strconv.Itoa(posts[0].ID)+".json"), - os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE) + os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.NormalFileMode) if err != nil { errEv.Err(err).Caller().Send() return fmt.Errorf("failed opening /%s/res/%d.json", board.Dir, posts[0].ID) diff --git a/pkg/config/config.go b/pkg/config/config.go index 564f5dab..ca6245b1 100644 --- a/pkg/config/config.go +++ b/pkg/config/config.go @@ -114,7 +114,7 @@ func (gcfg *GochanConfig) Write() error { // don't try to write anything if we're doing a test return nil } - return os.WriteFile(gcfg.jsonLocation, str, GC_FILE_MODE) + return os.WriteFile(gcfg.jsonLocation, str, NormalFileMode) } type SQLConfig struct { diff --git a/pkg/config/util.go b/pkg/config/util.go index e56a0df6..30363cea 100644 --- a/pkg/config/util.go +++ b/pkg/config/util.go @@ -2,7 +2,6 @@ package config import ( "encoding/json" - "flag" "fmt" "io/fs" "os" @@ -10,14 +9,15 @@ import ( "path" "runtime" "strconv" + "strings" "time" "github.com/gochan-org/gochan/pkg/gcutil" ) const ( - GC_DIR_MODE fs.FileMode = 0775 - GC_FILE_MODE fs.FileMode = 0664 + DirFileMode fs.FileMode = 0775 + NormalFileMode fs.FileMode = 0664 ) var ( @@ -74,7 +74,7 @@ func TakeOwnershipOfFile(f *os.File) error { // InitConfig loads and parses gochan.json on startup and verifies its contents func InitConfig(versionStr string) { cfg = defaultGochanConfig - if flag.Lookup("test.v") != nil { + if strings.HasSuffix(os.Args[0], ".test") { // create a dummy config for testing if we're using go test cfg = defaultGochanConfig cfg.ListenIP = "127.0.0.1" @@ -155,7 +155,10 @@ func InitConfig(versionStr string) { fmt.Println(err.Error()) os.Exit(1) } - if _, err = os.Stat(cfg.LogDir); err != nil { + if _, err = os.Stat(cfg.LogDir); os.IsNotExist(err) { + err = os.MkdirAll(cfg.LogDir, DirFileMode) + } + if err != nil { fmt.Println(err.Error()) os.Exit(1) } diff --git a/pkg/manage/actionsAdminPerm.go b/pkg/manage/actionsAdminPerm.go index aaabb52b..fbea8415 100644 --- a/pkg/manage/actionsAdminPerm.go +++ b/pkg/manage/actionsAdminPerm.go @@ -438,7 +438,7 @@ func templatesCallback(writer http.ResponseWriter, request *http.Request, _ *gcs if _, err = os.Stat(overrideDir); os.IsNotExist(err) { // override dir doesn't exist, create it - if err = os.Mkdir(overrideDir, config.GC_DIR_MODE); err != nil { + if err = os.Mkdir(overrideDir, config.DirFileMode); err != nil { errEv.Err(err).Caller(). Int("status", http.StatusInternalServerError). Msg("Unable to create override directory") @@ -477,7 +477,7 @@ func templatesCallback(writer http.ResponseWriter, request *http.Request, _ *gcs // back up template to override/-.bkp backupPath := path.Join(overrideDir, overriding) + time.Now().Format("-2006-01-02_15-04-05.bkp") gcutil.LogStr("backupPath", backupPath, infoEv, errEv) - if err = os.WriteFile(backupPath, ba, config.GC_FILE_MODE); err != nil { + if err = os.WriteFile(backupPath, ba, config.NormalFileMode); err != nil { errEv.Err(err).Caller(). Int("status", http.StatusInternalServerError). Msg("Unable to back up template file") @@ -486,7 +486,7 @@ func templatesCallback(writer http.ResponseWriter, request *http.Request, _ *gcs } // write changes to disk - if err = os.WriteFile(overridePath, []byte(templateStr), config.GC_FILE_MODE); err != nil { + if err = os.WriteFile(overridePath, []byte(templateStr), config.NormalFileMode); err != nil { errEv.Err(err).Caller(). Int("status", http.StatusInternalServerError). Msg("Unable to save changes") diff --git a/pkg/posting/uploads/attach.go b/pkg/posting/uploads/attach.go index 753c0c7a..ea7af041 100644 --- a/pkg/posting/uploads/attach.go +++ b/pkg/posting/uploads/attach.go @@ -156,7 +156,7 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter, } } - if err = os.WriteFile(filePath, data, config.GC_FILE_MODE); err != nil { + if err = os.WriteFile(filePath, data, config.NormalFileMode); err != nil { errEv.Err(err).Caller().Send() writer.WriteHeader(http.StatusInternalServerError) return nil, fmt.Errorf("couldn't write file %q", upload.OriginalFilename)