Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to Remove Dirs from untagged output #275

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 27 additions & 21 deletions cli/untagged.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@ package cli

import (
"fmt"
"os"
"path/filepath"

"github.com/oniony/TMSU/common/log"
_path "github.com/oniony/TMSU/common/path"
"github.com/oniony/TMSU/storage"
"os"
"path/filepath"
)

var UntaggedCommand = Command{
Expand All @@ -35,7 +36,8 @@ Where PATHs are not specified, untagged items under the current working director
"$ tmsu untagged /home/fred/drawings"},
Options: Options{Option{"--directory", "-d", "do not examine directory contents (non-recursive)", false, ""},
Option{"--count", "-c", "list the number of files rather than their names", false, ""},
Option{"--no-dereference", "-P", "do not dereference symbolic links", false, ""}},
Option{"--no-dereference", "-P", "do not dereference symbolic links", false, ""},
Option{"--no-directories", "", "do not show directories in output", false, ""}},
Exec: untaggedExec,
}

Expand All @@ -45,6 +47,7 @@ func untaggedExec(options Options, args []string, databasePath string) (error, w
recursive := !options.HasOption("--directory")
count := options.HasOption("--count")
followSymlinks := !options.HasOption("--no-dereference")
considerDirectories := !options.HasOption("--no-directories")

paths := args
if len(paths) == 0 {
Expand All @@ -68,49 +71,52 @@ func untaggedExec(options Options, args []string, databasePath string) (error, w
defer tx.Commit()

if count {
count, err := findUntaggedCount(store, tx, paths, recursive, followSymlinks)
count, err := findUntaggedCount(store, tx, paths, recursive, followSymlinks, considerDirectories)
if err != nil {
return err, nil
}

fmt.Println(count)
} else {
if err := findUntagged(store, tx, paths, recursive, followSymlinks); err != nil {
if err := findUntagged(store, tx, paths, recursive, followSymlinks, considerDirectories); err != nil {
return err, nil
}
}

return nil, nil
}

func findUntagged(store *storage.Storage, tx *storage.Tx, paths []string, recursive, followSymlinks bool) error {
func findUntagged(store *storage.Storage, tx *storage.Tx, paths []string, recursive, followSymlinks, considerDirectories bool) error {
var action = func(absPath string) {
relPath := _path.Rel(absPath)
fmt.Println(relPath)
}

return findUntaggedFunc(store, tx, paths, recursive, followSymlinks, action)
return findUntaggedFunc(store, tx, paths, recursive, followSymlinks, considerDirectories, action)
}

func findUntaggedCount(store *storage.Storage, tx *storage.Tx, paths []string, recursive, followSymlinks bool) (uint, error) {
func findUntaggedCount(store *storage.Storage, tx *storage.Tx, paths []string, recursive, followSymlinks, considerDirectories bool) (uint, error) {
var count uint

var action = func(absPath string) {
count++
}

err := findUntaggedFunc(store, tx, paths, recursive, followSymlinks, action)
err := findUntaggedFunc(store, tx, paths, recursive, followSymlinks, considerDirectories, action)

return count, err
}

func findUntaggedFunc(store *storage.Storage, tx *storage.Tx, paths []string, recursive, followSymlinks bool, action func(absPath string)) error {
func findUntaggedFunc(store *storage.Storage, tx *storage.Tx, paths []string, recursive, followSymlinks, considerDirectories bool, action func(absPath string)) error {
for _, path := range paths {
absPath, err := filepath.Abs(path)
if err != nil {
return fmt.Errorf("%v: could not get absolute path: %v", path, err)
}

stat, err := os.Stat(absPath)
if err != nil {
return fmt.Errorf("%v: could not stat path: %v", absPath, err)
}
if followSymlinks {
log.Infof(2, "%v: resolving path", path)

Expand All @@ -119,23 +125,23 @@ func findUntaggedFunc(store *storage.Storage, tx *storage.Tx, paths []string, re
return fmt.Errorf("%v: could not dereference path: %v", path, err)
}
}

//TODO PERF no need to retrieve file: we merely need to know it exists
file, err := store.FileByPath(tx, absPath)
if err != nil {
return fmt.Errorf("%v: could not retrieve file: %v", path, err)
}
if file == nil {
action(absPath)
if !stat.IsDir() || considerDirectories {
//TODO PERF no need to retrieve file: we merely need to know it exists
file, err := store.FileByPath(tx, absPath)
if err != nil {
return fmt.Errorf("%v: could not retrieve file: %v", path, err)
}
if file == nil {
action(absPath)
}
}

if recursive {
entries, err := directoryEntries(path)
if err != nil {
return err
}

findUntaggedFunc(store, tx, entries, true, followSymlinks, action)
findUntaggedFunc(store, tx, entries, true, followSymlinks, considerDirectories, action)
}
}

Expand Down
2 changes: 1 addition & 1 deletion misc/bash/tmsu
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ subcmd_lt_untag() {
subcmd_lt_tag
}

opts_untagged='-d --directory -c --count -P --no-dereference'
opts_untagged='-d --directory -c --count -P --no-dereference --no-directories'
args_untagged='0 0 0 0 0 0'
subcmd_gt_untagged() {
:
Expand Down
1 change: 1 addition & 0 deletions misc/zsh/_tmsu
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,7 @@ _tmsu_cmd_untagged() {
_arguments -s -w ''{--directory,-d}'[do not examine directory contents (non-recursive)]' \
''{--count,-c}'[lists the number of files rather than their names]' \
''{--no-dereference,-P}'[never follow symlinks (list untagged links)]' \
'--no-directories[do not show directories in output]' \
'*:file:_files' \
&& ret=0
}
Expand Down