Commits

Paul Ruane committed d98bcab

Fixed bugs with untag command and added support for removing multiple files.
Improvements to status command in preparation for missing file identification.
Moved file functions out to os.go.

Comments (0)

Files changed (4)

src/main/Makefile

 include $(GOROOT)/src/Make.inc
 
 TARG=tmsu
-GOFILES=config.go \
-		main.go \
-		version.gen.go \
-		command.go \
+GOFILES=command.go \
 		commands/delete.go \
 		commands/dupes.go \
 		commands/export.go \
 		commands/untag.go \
 		commands/version.go \
 		commands/vfs.go \
-		entities/tag.go \
+		config.go \
+		database.go \
 		entities/file.go \
 		entities/filetag.go \
-		database.go \
+		entities/tag.go \
+		fingerprinter.go \
 		fusevfs.go \
-		fingerprinter.go
+		main.go \
+		os.go \
+		version.gen.go \
 
 include $(GOROOT)/src/Make.cmd

src/main/commands/status.go

 
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
-package main
 */
 
 package main
 func (this StatusCommand) Exec(args []string) error {
     tagged := make([]string, 0, 10)
     untagged := make([]string, 0, 10)
+    missing := make([]string, 0, 10)
     var error error
 
     if len(args) == 0 {
-        tagged, untagged, error = this.status([]string{"."}, tagged, untagged)
+        tagged, untagged, missing, error = this.status([]string { "." }, tagged, untagged, missing)
     } else {
-        tagged, untagged, error = this.status(args, tagged, untagged)
+        tagged, untagged, missing, error = this.status(args, tagged, untagged, missing)
     }
 
     if error != nil {
     return nil
 }
 
-func (this StatusCommand) status(paths []string, tagged []string, untagged []string) ([]string, []string, error) {
+func (this StatusCommand) status(paths []string, tagged []string, untagged []string, missing []string) ([]string, []string, []string, error) {
     db, error := OpenDatabase(databasePath())
-    if error != nil { return nil, nil, error }
+    if error != nil { return nil, nil, nil, error }
     defer db.Close()
 
-    return this.statusRecursive(db, paths, tagged, untagged)
+    return this.statusRecursive(db, paths, tagged, untagged, missing)
 }
 
-func (this StatusCommand) statusRecursive(db *Database, paths []string, tagged []string, untagged []string) ([]string, []string, error) {
+func (this StatusCommand) statusRecursive(db *Database, paths []string, tagged []string, untagged []string, missing []string) ([]string, []string, []string, error) {
     for _, path := range paths {
         fileInfo, error := os.Lstat(path)
-        if error != nil { return nil, nil, error }
+        if error != nil { return nil, nil, nil, error }
 
-        if fileInfo.Mode() & os.ModeType == 0 {
-            absPath, error := filepath.Abs(path)
-            if error != nil { return nil, nil, error }
+        absPath, error := filepath.Abs(path)
+        if error != nil { return nil, nil, nil, error }
 
+        if isRegular(fileInfo)  {
             file, error := db.FileByPath(absPath)
-            if error != nil { return nil, nil, error }
+            if error != nil { return nil, nil, nil, error }
 
             if file == nil {
                 untagged = append(untagged, absPath)
                 tagged = append(tagged, absPath)
             }
         } else if fileInfo.IsDir() {
-            file, error := os.Open(path)
-            if error != nil { return nil, nil, error }
+            //TODO find missing files
 
-            dirNames, error := file.Readdirnames(0)
-            if error != nil { return nil, nil, error }
+            childPaths, error := directoryEntries(path)
+            if error != nil { return nil, nil, nil, error }
 
-            error = file.Close()
-            if error != nil { return nil, nil, error }
-
-            childPaths := make([]string, len(dirNames))
-            for index, dirName := range dirNames {
-                childPaths[index] = filepath.Join(path, dirName)
-            }
-
-            tagged, untagged, error = this.statusRecursive(db, childPaths, tagged, untagged)
-            if error != nil { return nil, nil, error }
+            tagged, untagged, missing, error = this.statusRecursive(db, childPaths, tagged, untagged, missing)
+            if error != nil { return nil, nil, nil, error }
         }
     }
 
-    return tagged, untagged, nil
+    return tagged, untagged, missing, nil
 }

src/main/commands/untag.go

 
 func (this UntagCommand) Help() string {
 	return `  tmsu untag FILE TAG...
-  tmsu untag --all FILE
+  tmsu untag --all FILE...
 
 Disassociates FILE with the TAGs specified.
 
-If the --all option is specified then the file will be stripped of all tags.`
+  --all    strip each FILE of all tags`
 }
 
 func (this UntagCommand) Exec(args []string) error {
 	}
 
     if args[0] == "--all" {
-        if len(args) > 1 { return errors.New("Too many arguments.") }
+        if len(args) < 2 { return errors.New("Files to untag must be specified.") }
 
-        error := this.removeFile(args[1])
+        error := this.removeFiles(args[1:])
         if error != nil { return error }
     } else {
         if len(args) < 2 { return errors.New("Tags to remove must be specified.") }
 
 // implementation
 
-func (this UntagCommand) removeFile(path string) error {
-	absPath, error := filepath.Abs(path)
-	if error != nil {
-		return error
-	}
+func (this UntagCommand) removeFiles(paths []string) error {
+    db, error := OpenDatabase(databasePath())
+    if error != nil {
+        return error
+    }
+    defer db.Close()
 
-	db, error := OpenDatabase(databasePath())
-	if error != nil {
-		return error
-	}
-	defer db.Close()
+    for _, path := range paths {
+        absPath, error := filepath.Abs(path)
+        if error != nil {
+            return error
+        }
 
-	file, error := db.FileByPath(absPath)
-	if error != nil { return error }
-	if file == nil { return errors.New("File '" + path + "' is not tagged.") }
+        file, error := db.FileByPath(absPath)
+        if error != nil { return error }
+        if file == nil { return errors.New("File '" + path + "' is not tagged.") }
 
-    error = db.RemoveFileTagsByFileId(file.Id)
-    if error != nil { return error }
+        error = db.RemoveFileTagsByFileId(file.Id)
+        if error != nil { return error }
 
-	error = db.RemoveFile(file.Id)
-    if error != nil { return error }
+        error = db.RemoveFile(file.Id)
+        if error != nil { return error }
+    }
 
     return nil
 }
+package main
+
+import (
+    "path/filepath"
+    "os"
+)
+
+func isRegular(fileInfo os.FileInfo) bool {
+    return fileInfo.Mode() & os.ModeType == 0
+}
+
+func directoryEntries(path string) ([]string, error) {
+    file, error := os.Open(path)
+    if error != nil { return nil, error }
+    defer file.Close()
+
+    entryNames, error := file.Readdirnames(0)
+    if error != nil { return nil, error }
+
+    entryPaths := make([]string, len(entryNames))
+    for index, entryName := range entryNames {
+        entryPaths[index] = filepath.Join(path, entryName)
+    }
+
+    return entryPaths, nil
+}