1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-02 15:06:23 -07:00

Use proper "Go-like" names for file modes

Try to create log dir if it doesn't exist
This commit is contained in:
Eggbertx 2024-05-17 10:57:23 -07:00
parent caea5ee77e
commit 3547b3117e
8 changed files with 26 additions and 23 deletions

View file

@ -156,7 +156,7 @@ func BuildBoardPages(board *gcsql.Board) error {
// Open 1.html for writing to the first page. // Open 1.html for writing to the first page.
boardPageFile, err = os.OpenFile(path.Join(criticalCfg.DocumentRoot, board.Dir, "1.html"), 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 { if err != nil {
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("page", "board.html"). Str("page", "board.html").
@ -198,7 +198,7 @@ func BuildBoardPages(board *gcsql.Board) error {
var catalogPages boardCatalog var catalogPages boardCatalog
// catalog JSON file is built with the pages because pages are recorded in the JSON file // 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 { if err != nil {
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Msg("Failed opening catalog.json") Msg("Failed opening catalog.json")
@ -215,7 +215,7 @@ func BuildBoardPages(board *gcsql.Board) error {
var currentPageFilepath string var currentPageFilepath string
pageFilename := strconv.Itoa(catalog.currentPage) + ".html" pageFilename := strconv.Itoa(catalog.currentPage) + ".html"
currentPageFilepath = path.Join(criticalCfg.DocumentRoot, board.Dir, pageFilename) 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 { if err != nil {
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("page", pageFilename). Str("page", pageFilename).
@ -414,7 +414,7 @@ func buildBoard(board *gcsql.Board, force bool) error {
Str("dirPath", dirPath).Send() Str("dirPath", dirPath).Send()
return fmt.Errorf(dirIsAFileStr, dirPath) 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(). errEv.Err(os.ErrExist).Caller().
Str("dirPath", dirPath).Send() Str("dirPath", dirPath).Send()
return fmt.Errorf(genericErrStr, dirPath, err.Error()) return fmt.Errorf(genericErrStr, dirPath, err.Error())
@ -438,7 +438,7 @@ func buildBoard(board *gcsql.Board, force bool) error {
Str("resPath", resPath).Send() Str("resPath", resPath).Send()
return err 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()) err = fmt.Errorf(genericErrStr, resPath, err.Error())
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("resPath", resPath).Send() Str("resPath", resPath).Send()
@ -463,7 +463,7 @@ func buildBoard(board *gcsql.Board, force bool) error {
Str("srcPath", srcPath).Send() Str("srcPath", srcPath).Send()
return err 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()) err = fmt.Errorf(genericErrStr, srcPath, err.Error())
errEv.Err(err).Caller(). errEv.Err(err).Caller().
Str("srcPath", srcPath).Send() Str("srcPath", srcPath).Send()
@ -482,7 +482,7 @@ func buildBoard(board *gcsql.Board, force bool) error {
if !thumbInfo.IsDir() { if !thumbInfo.IsDir() {
return fmt.Errorf(dirIsAFileStr, thumbPath) 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(). errEv.Err(err).Caller().
Str("thumbPath", thumbPath).Send() Str("thumbPath", thumbPath).Send()
return fmt.Errorf(genericErrStr, thumbPath, err.Error()) 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 // BuildBoardListJSON generates a JSON file with info about the boards
func BuildBoardListJSON() error { func BuildBoardListJSON() error {
boardsJsonPath := path.Join(config.GetSystemCriticalConfig().DocumentRoot, "boards.json") 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") errEv := gcutil.LogError(nil).Str("building", "boards.json")
defer errEv.Discard() defer errEv.Discard()
if err != nil { if err != nil {

View file

@ -92,7 +92,7 @@ func BuildFrontPage() error {
criticalCfg := config.GetSystemCriticalConfig() criticalCfg := config.GetSystemCriticalConfig()
os.Remove(path.Join(criticalCfg.DocumentRoot, "index.html")) 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 { if err != nil {
errEv.Err(err).Caller().Send() errEv.Err(err).Caller().Send()
return errors.New("Failed opening front page for writing: " + err.Error()) return errors.New("Failed opening front page for writing: " + err.Error())
@ -160,7 +160,7 @@ func BuildJS() error {
boardCfg := config.GetBoardConfig("") boardCfg := config.GetBoardConfig("")
criticalCfg := config.GetSystemCriticalConfig() criticalCfg := config.GetSystemCriticalConfig()
constsJSPath := path.Join(criticalCfg.DocumentRoot, "js", "consts.js") 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 { if err != nil {
errEv.Err(err).Caller().Send() errEv.Err(err).Caller().Send()
return fmt.Errorf("error opening consts.js for writing: %s", err.Error()) return fmt.Errorf("error opening consts.js for writing: %s", err.Error())

View file

@ -92,7 +92,7 @@ func BuildCatalog(boardID int) error {
errEv.Str("boardDir", board.Dir) errEv.Str("boardDir", board.Dir)
criticalCfg := config.GetSystemCriticalConfig() criticalCfg := config.GetSystemCriticalConfig()
catalogPath := path.Join(criticalCfg.DocumentRoot, board.Dir, "catalog.html") 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 { if err != nil {
errEv.Err(err).Caller().Send() errEv.Err(err).Caller().Send()
return fmt.Errorf("failed opening /%s/catalog.html: %s", board.Dir, err.Error()) return fmt.Errorf("failed opening /%s/catalog.html: %s", board.Dir, err.Error())

View file

@ -81,7 +81,7 @@ func BuildThreadPages(op *gcsql.Post) error {
os.Remove(path.Join(criticalCfg.DocumentRoot, board.Dir, "res", strconv.Itoa(op.ID)+".json")) 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") 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 { if err != nil {
errEv.Err(err).Caller().Send() errEv.Err(err).Caller().Send()
return fmt.Errorf("unable to open /%s/res/%d.html: %s", board.Dir, op.ID, err.Error()) 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 // Put together the thread JSON
threadJSONFile, err := os.OpenFile( threadJSONFile, err := os.OpenFile(
path.Join(criticalCfg.DocumentRoot, board.Dir, "res", strconv.Itoa(posts[0].ID)+".json"), 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 { if err != nil {
errEv.Err(err).Caller().Send() errEv.Err(err).Caller().Send()
return fmt.Errorf("failed opening /%s/res/%d.json", board.Dir, posts[0].ID) return fmt.Errorf("failed opening /%s/res/%d.json", board.Dir, posts[0].ID)

View file

@ -114,7 +114,7 @@ func (gcfg *GochanConfig) Write() error {
// don't try to write anything if we're doing a test // don't try to write anything if we're doing a test
return nil return nil
} }
return os.WriteFile(gcfg.jsonLocation, str, GC_FILE_MODE) return os.WriteFile(gcfg.jsonLocation, str, NormalFileMode)
} }
type SQLConfig struct { type SQLConfig struct {

View file

@ -2,7 +2,6 @@ package config
import ( import (
"encoding/json" "encoding/json"
"flag"
"fmt" "fmt"
"io/fs" "io/fs"
"os" "os"
@ -10,14 +9,15 @@ import (
"path" "path"
"runtime" "runtime"
"strconv" "strconv"
"strings"
"time" "time"
"github.com/gochan-org/gochan/pkg/gcutil" "github.com/gochan-org/gochan/pkg/gcutil"
) )
const ( const (
GC_DIR_MODE fs.FileMode = 0775 DirFileMode fs.FileMode = 0775
GC_FILE_MODE fs.FileMode = 0664 NormalFileMode fs.FileMode = 0664
) )
var ( var (
@ -74,7 +74,7 @@ func TakeOwnershipOfFile(f *os.File) error {
// InitConfig loads and parses gochan.json on startup and verifies its contents // InitConfig loads and parses gochan.json on startup and verifies its contents
func InitConfig(versionStr string) { func InitConfig(versionStr string) {
cfg = defaultGochanConfig 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 // create a dummy config for testing if we're using go test
cfg = defaultGochanConfig cfg = defaultGochanConfig
cfg.ListenIP = "127.0.0.1" cfg.ListenIP = "127.0.0.1"
@ -155,7 +155,10 @@ func InitConfig(versionStr string) {
fmt.Println(err.Error()) fmt.Println(err.Error())
os.Exit(1) 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()) fmt.Println(err.Error())
os.Exit(1) os.Exit(1)
} }

View file

@ -438,7 +438,7 @@ func templatesCallback(writer http.ResponseWriter, request *http.Request, _ *gcs
if _, err = os.Stat(overrideDir); os.IsNotExist(err) { if _, err = os.Stat(overrideDir); os.IsNotExist(err) {
// override dir doesn't exist, create it // 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(). errEv.Err(err).Caller().
Int("status", http.StatusInternalServerError). Int("status", http.StatusInternalServerError).
Msg("Unable to create override directory") Msg("Unable to create override directory")
@ -477,7 +477,7 @@ func templatesCallback(writer http.ResponseWriter, request *http.Request, _ *gcs
// back up template to override/<overriding>-<timestamp>.bkp // back up template to override/<overriding>-<timestamp>.bkp
backupPath := path.Join(overrideDir, overriding) + time.Now().Format("-2006-01-02_15-04-05.bkp") backupPath := path.Join(overrideDir, overriding) + time.Now().Format("-2006-01-02_15-04-05.bkp")
gcutil.LogStr("backupPath", backupPath, infoEv, errEv) 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(). errEv.Err(err).Caller().
Int("status", http.StatusInternalServerError). Int("status", http.StatusInternalServerError).
Msg("Unable to back up template file") Msg("Unable to back up template file")
@ -486,7 +486,7 @@ func templatesCallback(writer http.ResponseWriter, request *http.Request, _ *gcs
} }
// write changes to disk // 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(). errEv.Err(err).Caller().
Int("status", http.StatusInternalServerError). Int("status", http.StatusInternalServerError).
Msg("Unable to save changes") Msg("Unable to save changes")

View file

@ -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() errEv.Err(err).Caller().Send()
writer.WriteHeader(http.StatusInternalServerError) writer.WriteHeader(http.StatusInternalServerError)
return nil, fmt.Errorf("couldn't write file %q", upload.OriginalFilename) return nil, fmt.Errorf("couldn't write file %q", upload.OriginalFilename)