Skip to content

Commit

Permalink
Merge pull request #1104 from wakatime/bugfix/file-detector
Browse files Browse the repository at this point in the history
Prevent overwriting existing wakatime-project files
  • Loading branch information
alanhamlett authored Sep 26, 2024
2 parents 86d6c1d + a0c0421 commit d32ccc3
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,7 @@ func SetupLogging(v *viper.Viper) (*logfile.Params, error) {
}
}

logFile, err = os.OpenFile(logfileParams.File, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0600)
logFile, err = os.OpenFile(logfileParams.File, os.O_CREATE|os.O_APPEND|os.O_WRONLY, 0644) // nolint:gosec
if err != nil {
return nil, fmt.Errorf("error opening log file: %s", err)
}
Expand Down
2 changes: 1 addition & 1 deletion pkg/offline/offline.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ func openDB(filepath string) (db *bolt.DB, _ func(), err error) {
}
}()

db, err = bolt.Open(filepath, 0600, &bolt.Options{Timeout: 30 * time.Second})
db, err = bolt.Open(filepath, 0644, &bolt.Options{Timeout: 30 * time.Second})
if err != nil {
return nil, nil, fmt.Errorf("failed to open db file: %s", err)
}
Expand Down
15 changes: 4 additions & 11 deletions pkg/project/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ type File struct {
// a given file. First line of .wakatime-project sets the project
// name. Second line sets the current branch name.
func (f File) Detect() (Result, bool, error) {
fp, ok := FindFileOrDirectory(f.Filepath, WakaTimeProjectFile)
if !ok {
fp, found := FindFileOrDirectory(f.Filepath, WakaTimeProjectFile)
if !found {
return Result{}, false, nil
}

Expand All @@ -33,11 +33,10 @@ func (f File) Detect() (Result, bool, error) {
}

result := Result{
Folder: filepath.Dir(fp),
Folder: filepath.Dir(fp),
Project: filepath.Base(filepath.Dir(fp)),
}

result.Project = filepath.Base(filepath.Dir(fp))

if len(lines) > 0 {
result.Project = strings.TrimSpace(lines[0])
}
Expand All @@ -49,12 +48,6 @@ func (f File) Detect() (Result, bool, error) {
return result, true, nil
}

// fileOrDirExists checks if a file or directory exist.
func fileOrDirExists(fp string) bool {
_, err := os.Stat(fp)
return err == nil || os.IsExist(err)
}

// ReadFile reads a file until max number of lines and return an array of lines.
func ReadFile(fp string, max int) ([]string, error) {
if fp == "" {
Expand Down
12 changes: 6 additions & 6 deletions pkg/project/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ func (g Git) Detect() (Result, bool, error) {
}

// Find for .git file or directory
dotGit, ok := FindFileOrDirectory(fp, ".git")
if !ok {
dotGit, found := FindFileOrDirectory(fp, ".git")
if !found {
return Result{}, false, nil
}

Expand Down Expand Up @@ -132,9 +132,9 @@ func (g Git) Detect() (Result, bool, error) {
}

// Find for .git/config file
gitConfigFile, ok := FindFileOrDirectory(fp, filepath.Join(".git", "config"))
gitConfigFile, found := FindFileOrDirectory(fp, filepath.Join(".git", "config"))

if ok {
if found {
gitDir := filepath.Dir(gitConfigFile)
projectDir := filepath.Join(gitDir, "..")

Expand Down Expand Up @@ -164,8 +164,8 @@ func findSubmodule(fp string, patterns []regex.Regex) (string, bool, error) {
return "", false, nil
}

gitConfigFile, ok := FindFileOrDirectory(fp, ".git")
if !ok {
gitConfigFile, found := FindFileOrDirectory(fp, ".git")
if !found {
return "", false, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/project/mercurial.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func (m Mercurial) Detect() (Result, bool, error) {
}

// Find for .hg folder
hgDirectory, ok := FindFileOrDirectory(fp, ".hg")
if !ok {
hgDirectory, found := FindFileOrDirectory(fp, ".hg")
if !found {
return Result{}, false, nil
}

Expand Down
17 changes: 14 additions & 3 deletions pkg/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,11 @@ func DetectWithRevControl(
}

func obfuscateProjectName(folder string) string {
// prevent overwriting existing project files, use Unknown Project instead
if fileOrDirExists(filepath.Join(folder, WakaTimeProjectFile)) {
return ""
}

project := generateProjectName()

err := Write(folder, project)
Expand All @@ -321,7 +326,7 @@ func obfuscateProjectName(folder string) string {

// Write saves wakatime project file.
func Write(folder, project string) error {
err := os.WriteFile(filepath.Join(folder, WakaTimeProjectFile), []byte(project+"\n"), 0600)
err := os.WriteFile(filepath.Join(folder, WakaTimeProjectFile), []byte(project+"\n"), 0644) // nolint:gosec
if err != nil {
return fmt.Errorf("failed to save wakatime project file: %s", err)
}
Expand Down Expand Up @@ -620,8 +625,8 @@ func CountSlashesInProjectFolder(directory string) int {
return strings.Count(directory, `/`)
}

// FindFileOrDirectory searches for a file or directory named `filename`.
// Search starts in `directory` and will traverse through all parent directories.
// FindFileOrDirectory searches current and all parent folders for a file or directory named `filename`.
// Starts in `directory` and traverses through all parent directories.
// `directory` may also be a file, and in that case will start from the file's directory.
func FindFileOrDirectory(directory, filename string) (string, bool) {
i := 0
Expand Down Expand Up @@ -689,3 +694,9 @@ func FormatProjectFolder(fp string) string {

return formatted
}

// fileOrDirExists checks if a file or directory exist.
func fileOrDirExists(fp string) bool {
_, err := os.Stat(fp)
return err == nil || os.IsExist(err)
}
4 changes: 2 additions & 2 deletions pkg/project/subversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func (s Subversion) Detect() (Result, bool, error) {
}

// Find for .svn/wc.db file
svnConfigFile, ok := FindFileOrDirectory(fp, filepath.Join(".svn", "wc.db"))
if !ok {
svnConfigFile, found := FindFileOrDirectory(fp, filepath.Join(".svn", "wc.db"))
if !found {
return Result{}, false, nil
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/project/tfvc.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ func (t Tfvc) Detect() (Result, bool, error) {
}

// Find for tf/properties.tf1 file
tfDirectory, ok := FindFileOrDirectory(fp, filepath.Join(tfFolderName, "properties.tf1"))
if !ok {
tfDirectory, found := FindFileOrDirectory(fp, filepath.Join(tfFolderName, "properties.tf1"))
if !found {
return Result{}, false, nil
}

Expand Down

0 comments on commit d32ccc3

Please sign in to comment.