Commits

Paul Ruane committed babf826

Issue #10: Status command should report missing files.

Status command now stats database files for each iterated directory.
Minor change to remove pointer.

Comments (0)

Files changed (3)

src/main/commands/export.go

 		return error
 	}
 
-	for _, file := range *files {
+	for _, file := range files {
 		fmt.Printf("%v,%v,", file.Path(), file.Fingerprint)
 
 		tags, error := db.TagsByFileId(file.Id)

src/main/commands/status.go

         fmt.Printf("T %v\n", path)
     }
 
+    for _, path := range missing {
+        fmt.Printf("! %v\n", path)
+    }
+
     for _, path := range untagged {
-        fmt.Printf("U %v\n", path)
+        fmt.Printf("? %v\n", path)
     }
 
     return nil
             if error != nil { return nil, nil, nil, error }
 
             if file == nil {
-                untagged = append(untagged, absPath)
+                untagged = append(untagged, path)
             } else {
-                tagged = append(tagged, absPath)
+                tagged = append(tagged, path)
             }
         } else if fileInfo.IsDir() {
-            //TODO find missing files
+            files, error := db.FilesByDirectory(absPath)
+            if error != nil { return nil, nil, nil, error }
+
+            for _, file := range files {
+                _, error := os.Lstat(file.Path())
+
+                if error != nil {
+                    if error.(*os.PathError).Err == os.ENOENT {
+                        missingFilePath := filepath.Join(path, file.Name)
+                        missing = append(missing, missingFilePath)
+                    } else {
+                        return nil, nil, nil, error
+                    }
+                }
+            }
 
             childPaths, error := directoryEntries(path)
             if error != nil { return nil, nil, nil, error }

src/main/database.go

 	return count, nil
 }
 
-func (this Database) Files() (*[]File, error) {
+func (this Database) Files() ([]File, error) {
 	sql := `SELECT id, directory, name, fingerprint
 	        FROM file`
 
 		files = append(files, File{fileId, directory, name, fingerprint})
 	}
 
-	return &files, nil
+	return files, nil
 }
 
 func (this Database) File(id uint) (*File, error) {
 
 func (this Database) FileByPath(path string) (*File, error) {
     directory, name := filepath.Split(path)
+    directory = filepath.Clean(directory)
 
 	sql := `SELECT id, fingerprint
 	        FROM file
 	return &File{id, directory, name, fingerprint}, nil
 }
 
+func (this Database) FilesByDirectory(directory string) ([]File, error) {
+	sql := `SELECT id, name, fingerprint
+	        FROM file
+	        WHERE directory = ?`
+
+	rows, error := this.connection.Query(sql, directory)
+	if error != nil { return nil, error }
+	defer rows.Close()
+
+	files := make([]File, 0, 10)
+	for rows.Next() {
+        if rows.Err() != nil { return nil, error }
+
+		var fileId uint
+		var name string
+		var fingerprint string
+		error = rows.Scan(&fileId, &name, &fingerprint)
+		if error != nil { return nil, error }
+
+		files = append(files, File{fileId, directory, name, fingerprint})
+	}
+
+	return files, nil
+}
+
 func (this Database) FilesByFingerprint(fingerprint string) ([]File, error) {
 	sql := `SELECT id, directory, name
 	        FROM file