Skip to content

Commit

Permalink
feat: override default file mode from command line
Browse files Browse the repository at this point in the history
  • Loading branch information
Qingping Hou committed Mar 25, 2020
1 parent a8c87be commit f02382c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 12 deletions.
21 changes: 16 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"net/http"
"os"
"runtime/debug"
"strconv"
"time"

"github.com/getsentry/sentry-go"
Expand All @@ -19,11 +20,12 @@ import (
)

var (
InitialRunFinished atomic.Bool
FlagRunOnce bool
FlagStatusAddr = ":8087"
FlagExclude []string
FlagScratch bool
InitialRunFinished atomic.Bool
FlagRunOnce bool
FlagStatusAddr = ":8087"
FlagExclude []string
FlagScratch bool
FlagDefaultFileMode = "0666"

metricsSyncTime = prometheus.NewGauge(prometheus.GaugeOpts{
Namespace: "objinsync",
Expand Down Expand Up @@ -106,6 +108,13 @@ func main() {
if !FlagScratch {
puller.PopulateChecksum()
}
if FlagDefaultFileMode != "" {
mode, err := strconv.ParseInt(FlagDefaultFileMode, 8, 64)
if err != nil {
log.Fatal("invalid default file mode", err)
}
puller.SetDefaultFileMode(os.FileMode(mode))
}

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

rootCmd.AddCommand(pullCmd)
rootCmd.Execute()
Expand Down
20 changes: 13 additions & 7 deletions pkg/sync/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ type Puller struct {
LocalDir string

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

func (self *Puller) SetDefaultFileMode(mode os.FileMode) {
self.defaultMode = mode
}

func NewPuller(remoteUri string, localDir string) (*Puller, error) {
if _, err := os.Stat(localDir); os.IsNotExist(err) {
return nil, fmt.Errorf("local directory `%s` does not exist: %v", localDir, err)
}

return &Puller{
RemoteUri: remoteUri,
LocalDir: localDir,
workingDir: filepath.Join(localDir, ".objinsync"),
workerCnt: 5,
uidCache: map[string]string{},
uidLock: &sync.Mutex{},
RemoteUri: remoteUri,
LocalDir: localDir,
workingDir: filepath.Join(localDir, ".objinsync"),
defaultMode: 0666,
workerCnt: 5,
uidCache: map[string]string{},
uidLock: &sync.Mutex{},
}, nil
}

0 comments on commit f02382c

Please sign in to comment.