]> Sergey Matveev's repositories - btrtrc.git/commitdiff
Extract file and dir perm constants
authorMatt Joiner <anacrolix@gmail.com>
Tue, 29 Apr 2025 02:01:10 +0000 (12:01 +1000)
committerMatt Joiner <anacrolix@gmail.com>
Tue, 29 Apr 2025 02:01:10 +0000 (12:01 +1000)
storage/file.go
storage/mmap.go
storage/perms.go [new file with mode: 0644]

index 2cacb1ac752328dd5114bafb30ee5ec73a0558bb..d4df5511c0ad0a398696b633d66c58f621552cc1 100644 (file)
@@ -186,9 +186,9 @@ func (fs *fileTorrentImpl) Close() error {
 }
 
 func fsync(filePath string) (err error) {
-       _ = os.MkdirAll(filepath.Dir(filePath), 0o777)
+       _ = os.MkdirAll(filepath.Dir(filePath), dirPerm)
        var f *os.File
-       f, err = os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, 0o666)
+       f, err = os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE, filePerm)
        if err != nil {
                return err
        }
@@ -212,7 +212,7 @@ func (fts *fileTorrentImpl) Flush() error {
 // writes will ever occur to them (no torrent data is associated with a zero-length file). The
 // caller should make sure the file name provided is safe/sanitized.
 func CreateNativeZeroLengthFile(name string) error {
-       os.MkdirAll(filepath.Dir(name), 0o777)
+       os.MkdirAll(filepath.Dir(name), dirPerm)
        var f io.Closer
        f, err := os.Create(name)
        if err != nil {
@@ -274,9 +274,9 @@ func (fst fileTorrentImplIO) WriteAt(p []byte, off int64) (n int, err error) {
        // log.Printf("write at %v: %v bytes", off, len(p))
        fst.fts.segmentLocater.Locate(segments.Extent{off, int64(len(p))}, func(i int, e segments.Extent) bool {
                name := fst.fts.files[i].safeOsPath
-               os.MkdirAll(filepath.Dir(name), 0o777)
+               os.MkdirAll(filepath.Dir(name), dirPerm)
                var f *os.File
-               f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE, 0o666)
+               f, err = os.OpenFile(name, os.O_WRONLY|os.O_CREATE, filePerm)
                if err != nil {
                        return false
                }
index 1640a1399cfe01ec800d01794fbf7263fa04c828..5c84f28299319467ab2d271139c881fbb3769743 100644 (file)
@@ -144,7 +144,7 @@ func mmapFile(name string, size int64) (_ FileMapping, err error) {
                return
        }
        var file *os.File
-       file, err = os.OpenFile(name, os.O_CREATE|os.O_RDWR, 0o666)
+       file, err = os.OpenFile(name, os.O_CREATE|os.O_RDWR, filePerm)
        if err != nil {
                return
        }
diff --git a/storage/perms.go b/storage/perms.go
new file mode 100644 (file)
index 0000000..f7c23ce
--- /dev/null
@@ -0,0 +1,11 @@
+package storage
+
+import (
+       "os"
+)
+
+// Default file permissions for writable OS files.
+const (
+       filePerm os.FileMode = 0o666
+       dirPerm  os.FileMode = 0o777
+)