1
0
Fork 0
mirror of https://github.com/Eggbertx/gochan.git synced 2025-08-17 10:56:24 -07:00

Use more standardized file modes

This commit is contained in:
Eggbertx 2022-12-31 17:29:57 -08:00
parent 76a4f92e18
commit f3add8bb89
10 changed files with 46 additions and 27 deletions

View file

@ -158,7 +158,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, 0777)
os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE)
if err != nil {
errEv.Err(err).Caller().
Str("page", "board.html").
@ -203,7 +203,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, 0777)
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)
if err != nil {
errEv.Err(err).Caller().
Msg("Failed opening catalog.json")
@ -221,7 +221,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, 0777)
currentPageFile, err = os.OpenFile(currentPageFilepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE)
if err != nil {
errEv.Err(err).Caller().
Str("page", pageFilename).
@ -351,14 +351,15 @@ func buildBoard(board *gcsql.Board, force bool) error {
Caller().Send()
return fmt.Errorf(dirIsAFileStr, dirPath)
}
} else if err = os.Mkdir(dirPath, 0666); err != nil {
} else if err = os.Mkdir(dirPath, config.GC_DIR_MODE); err != nil {
errEv.Err(os.ErrExist).
Str("dirPath", dirPath).
Caller().Send()
return fmt.Errorf(genericErrStr, dirPath, err.Error())
}
if err = config.TakeOwnership(dirPath); err != nil {
errEv.Err(err).Caller().
Str("dirPath", dirPath).Send()
return fmt.Errorf(genericErrStr, dirPath, err.Error())
}
@ -376,9 +377,8 @@ func buildBoard(board *gcsql.Board, force bool) error {
Str("resPath", resPath).
Caller().Send()
return err
}
} else if err = os.Mkdir(resPath, 0666); err != nil {
} else if err = os.Mkdir(resPath, config.GC_DIR_MODE); err != nil {
err = fmt.Errorf(genericErrStr, resPath, err.Error())
errEv.Err(err).
Str("resPath", resPath).
@ -406,7 +406,7 @@ func buildBoard(board *gcsql.Board, force bool) error {
Caller().Send()
return err
}
} else if err = os.Mkdir(srcPath, 0666); err != nil {
} else if err = os.Mkdir(srcPath, config.GC_DIR_MODE); err != nil {
err = fmt.Errorf(genericErrStr, srcPath, err.Error())
errEv.Err(err).
Str("srcPath", srcPath).
@ -426,7 +426,7 @@ func buildBoard(board *gcsql.Board, force bool) error {
if !thumbInfo.IsDir() {
return fmt.Errorf(dirIsAFileStr, thumbPath)
}
} else if err = os.Mkdir(thumbPath, 0666); err != nil {
} else if err = os.Mkdir(thumbPath, config.GC_DIR_MODE); err != nil {
errEv.Err(err).Caller().
Str("thumbPath", thumbPath).Send()
return fmt.Errorf(genericErrStr, thumbPath, err.Error())
@ -466,7 +466,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, 0777)
boardListFile, err := os.OpenFile(boardsJsonPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE)
errEv := gcutil.LogError(nil).Str("building", "boards.json")
defer errEv.Discard()
if err != nil {

View file

@ -105,7 +105,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, 0777)
frontFile, err := os.OpenFile(path.Join(criticalCfg.DocumentRoot, "index.html"), os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE)
if err != nil {
errEv.Err(err).Caller().Send()
return errors.New("Failed opening front page for writing: " + err.Error())
@ -179,7 +179,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, 0644)
constsJSFile, err := os.OpenFile(constsJSPath, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, config.GC_FILE_MODE)
if err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("error opening consts.js for writing: %s", err.Error())

View file

@ -81,7 +81,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, 0777)
catalogFile, err := os.OpenFile(catalogPath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE)
if err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("failed opening /%s/catalog.html: %s", board.Dir, err.Error())

View file

@ -83,7 +83,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, 0777)
threadPageFile, err = os.OpenFile(threadPageFilepath, os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE)
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())
@ -115,7 +115,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, 0777)
os.O_CREATE|os.O_RDWR|os.O_TRUNC, config.GC_FILE_MODE)
if err != nil {
errEv.Err(err).Caller().Send()
return fmt.Errorf("failed opening /%s/res/%d.json: %s", board.Dir, posts[0].ID, err.Error())

View file

@ -247,7 +247,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, 0777)
return os.WriteFile(gcfg.jsonLocation, str, GC_FILE_MODE)
}
/*

View file

@ -4,6 +4,7 @@ import (
"encoding/json"
"flag"
"fmt"
"io/fs"
"os"
"os/user"
"path"
@ -15,6 +16,11 @@ import (
"github.com/gochan-org/gochan/pkg/gcutil"
)
const (
GC_DIR_MODE fs.FileMode = 0775
GC_FILE_MODE fs.FileMode = 0664
)
var (
criticalFields = []string{
"ListenIP", "Port", "Username", "UseFastCGI", "DocumentRoot", "TemplateDir", "LogDir",
@ -79,7 +85,7 @@ func GetDefaultString(key string) string {
return str
}
func TakeOwnership(fp string) error {
func TakeOwnership(fp string) (err error) {
if runtime.GOOS == "windows" || fp == "" {
// Chown returns an error in Windows
return nil
@ -305,13 +311,8 @@ func InitConfig(versionStr string) {
}
cfg.LogDir = gcutil.FindResource(cfg.LogDir, "log", "/var/log/gochan/")
if err = gcutil.InitLog(path.Join(cfg.LogDir, "gochan.log"), cfg.DebugMode); err != nil {
fmt.Println(err.Error())
os.Exit(1)
}
if err = gcutil.InitAccessLog(path.Join(cfg.LogDir, "gochan_access.log")); err != nil {
fmt.Println(err.Error())
os.Exit(1)
if err = gcutil.InitLogs(cfg.LogDir, cfg.DebugMode, uid, gid); err != nil {
fmt.Println("Error opening logs:", err.Error())
}
if cfg.Port == 0 {

View file

@ -4,6 +4,7 @@ import (
"io"
"net/http"
"os"
"path"
"github.com/rs/zerolog"
)
@ -82,6 +83,23 @@ func InitAccessLog(logPath string) (err error) {
return nil
}
func InitLogs(logDir string, debug bool, uid int, gid int) (err error) {
if err = InitLog(path.Join(logDir, "gochan.log"), debug); err != nil {
return err
}
if err = logFile.Chown(uid, gid); err != nil {
return err
}
if err = InitAccessLog(path.Join(logDir, "gochan_access.log")); err != nil {
return err
}
if err = accessFile.Chown(uid, gid); err != nil {
return err
}
return nil
}
func Logger() *zerolog.Logger {
return &logger
}

View file

@ -81,7 +81,7 @@ func AttachUploadFromRequest(request *http.Request, writer http.ResponseWriter,
thumbPath := path.Join(documentRoot, postBoard.Dir, "thumb", upload.ThumbnailPath("thumb"))
catalogThumbPath := path.Join(documentRoot, postBoard.Dir, "thumb", upload.ThumbnailPath("catalog"))
if err = os.WriteFile(filePath, data, 0644); err != nil {
if err = os.WriteFile(filePath, data, config.GC_FILE_MODE); err != nil {
errEv.Err(err).Caller().
Str("filename", upload.Filename).
Str("originalFilename", upload.OriginalFilename).

View file

@ -2,7 +2,7 @@
"ListenIP": "127.0.0.1",
"Port": 8080,
"FirstPage": ["index.html","firstrun.html","1.html"],
"Username": "gochan",
"Username": "www-data",
"UseFastCGI": false,
"DebugMode": false,

View file

@ -132,7 +132,7 @@ su - vagrant <<EOF
mkdir -p /home/vagrant/go
source /home/vagrant/.bashrc
cd /vagrant/devtools
python build_initdb.py
./build_initdb.py
cd ..
mkdir -p $GOPATH/src/github.com/gochan-org/gochan
cp -r pkg $GOPATH/src/github.com/gochan-org/gochan