Skip to content

Commit f02382c

Browse files
author
Qingping Hou
committed
feat: override default file mode from command line
1 parent a8c87be commit f02382c

File tree

2 files changed

+29
-12
lines changed

2 files changed

+29
-12
lines changed

main.go

Lines changed: 16 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"net/http"
77
"os"
88
"runtime/debug"
9+
"strconv"
910
"time"
1011

1112
"github.com/getsentry/sentry-go"
@@ -19,11 +20,12 @@ import (
1920
)
2021

2122
var (
22-
InitialRunFinished atomic.Bool
23-
FlagRunOnce bool
24-
FlagStatusAddr = ":8087"
25-
FlagExclude []string
26-
FlagScratch bool
23+
InitialRunFinished atomic.Bool
24+
FlagRunOnce bool
25+
FlagStatusAddr = ":8087"
26+
FlagExclude []string
27+
FlagScratch bool
28+
FlagDefaultFileMode = "0666"
2729

2830
metricsSyncTime = prometheus.NewGauge(prometheus.GaugeOpts{
2931
Namespace: "objinsync",
@@ -106,6 +108,13 @@ func main() {
106108
if !FlagScratch {
107109
puller.PopulateChecksum()
108110
}
111+
if FlagDefaultFileMode != "" {
112+
mode, err := strconv.ParseInt(FlagDefaultFileMode, 8, 64)
113+
if err != nil {
114+
log.Fatal("invalid default file mode", err)
115+
}
116+
puller.SetDefaultFileMode(os.FileMode(mode))
117+
}
109118

110119
pull := func() {
111120
start := time.Now()
@@ -158,6 +167,8 @@ func main() {
158167
false,
159168
"skip checksums calculation and override all files during the initial sync",
160169
)
170+
pullCmd.PersistentFlags().StringVarP(
171+
&FlagDefaultFileMode, "default-file-mode", "m", "0666", "default mode to use for creating local file")
161172

162173
rootCmd.AddCommand(pullCmd)
163174
rootCmd.Execute()

pkg/sync/pull.go

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ type Puller struct {
103103
LocalDir string
104104

105105
workingDir string
106+
defaultMode os.FileMode
106107
exclude []string
107108
workerCnt int
108109
uidCache map[string]string
@@ -152,7 +153,7 @@ func (self *Puller) downloadHandler(task DownloadTask, downloader GenericDownloa
152153
// create file
153154
tmpfileName := fmt.Sprintf("%x", md5.Sum([]byte(task.LocalPath)))
154155
tmpfilePath := filepath.Join(self.workingDir, tmpfileName)
155-
tmpfile, err := os.OpenFile(tmpfilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0666)
156+
tmpfile, err := os.OpenFile(tmpfilePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, self.defaultMode)
156157
if err != nil {
157158
self.errMsgQueue <- fmt.Sprintf("Failed to create temp file for download: %v", err)
158159
return
@@ -447,17 +448,22 @@ func (self *Puller) PopulateChecksum() {
447448
}
448449
}
449450

451+
func (self *Puller) SetDefaultFileMode(mode os.FileMode) {
452+
self.defaultMode = mode
453+
}
454+
450455
func NewPuller(remoteUri string, localDir string) (*Puller, error) {
451456
if _, err := os.Stat(localDir); os.IsNotExist(err) {
452457
return nil, fmt.Errorf("local directory `%s` does not exist: %v", localDir, err)
453458
}
454459

455460
return &Puller{
456-
RemoteUri: remoteUri,
457-
LocalDir: localDir,
458-
workingDir: filepath.Join(localDir, ".objinsync"),
459-
workerCnt: 5,
460-
uidCache: map[string]string{},
461-
uidLock: &sync.Mutex{},
461+
RemoteUri: remoteUri,
462+
LocalDir: localDir,
463+
workingDir: filepath.Join(localDir, ".objinsync"),
464+
defaultMode: 0666,
465+
workerCnt: 5,
466+
uidCache: map[string]string{},
467+
uidLock: &sync.Mutex{},
462468
}, nil
463469
}

0 commit comments

Comments
 (0)