Commits

Paul Ruane committed 47b8dfe

Changes to split storage of file paths into directory and filename.

  • Participants
  • Parent commits d98bcab

Comments (0)

Files changed (6)

File src/main/commands/dupes.go

         fmt.Printf("%v duplicate files:\n", len(fileSet))
 
         for _, file := range fileSet {
-            fmt.Printf("  %v\n", file.Path)
+            fmt.Printf("  %v\n", file.Path())
         }
     }
 
     if error != nil { return error }
 
     for _, file := range files {
-        if file.Path == absPath { continue }
+        if file.Path() == absPath { continue }
 
-        fmt.Println(file.Path)
+        fmt.Println(file.Path())
     }
 
     return nil

File src/main/commands/export.go

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

File src/main/commands/tag.go

 			fmt.Printf("Warning: file is a duplicate of previously tagged files.\n")
 
 			for _, duplicateFile := range files {
-			    fmt.Printf("  %v\n", duplicateFile.Path)
+			    fmt.Printf("  %v\n", duplicateFile.Path())
             }
 		}
 

File src/main/database.go

 import (
     "exp/sql"
     "errors"
+    "path/filepath"
 	"strconv"
 	"strings"
 	_ "github.com/mattn/go-sqlite3"
 	if error != nil { return nil, error }
 
 	database := Database{connection}
-	database.CreateSchema()
+
+	error = database.CreateSchema()
+	if error != nil { return nil, error }
 
 	return &database, nil
 }
 }
 
 func (this Database) Files() (*[]File, error) {
-	sql := `SELECT id, path, fingerprint
+	sql := `SELECT id, directory, name, fingerprint
 	        FROM file`
 
 	rows, error := this.connection.Query(sql)
         if rows.Err() != nil { return nil, error }
 
 		var fileId uint
-		var path string
+		var directory string
+		var name string
 		var fingerprint string
-		error = rows.Scan(&fileId, &path, &fingerprint)
+		error = rows.Scan(&fileId, &directory, &name, &fingerprint)
 		if error != nil { return nil, error }
 
-		files = append(files, File{fileId, path, fingerprint})
+		files = append(files, File{fileId, directory, name, fingerprint})
 	}
 
 	return &files, nil
 }
 
 func (this Database) File(id uint) (*File, error) {
-	sql := `SELECT path, fingerprint
+	sql := `SELECT directory, name, fingerprint
 	        FROM file
 	        WHERE id = ?`
 
 	if !rows.Next() { return nil, nil }
 	if rows.Err() != nil { return nil, error }
 
-	var path string
+	var directory string
+	var name string
 	var fingerprint string
-	error = rows.Scan(&path, &fingerprint)
+	error = rows.Scan(&directory, &name, &fingerprint)
 	if error != nil { return nil, error }
 
-	return &File{id, path, fingerprint}, nil
+	return &File{id, directory, name, fingerprint}, nil
 }
 
 func (this Database) FileByPath(path string) (*File, error) {
+    directory, name := filepath.Split(path)
+
 	sql := `SELECT id, fingerprint
 	        FROM file
-	        WHERE path = ?`
+	        WHERE directory = ? AND name = ?`
 
-	rows, error := this.connection.Query(sql, path)
+	rows, error := this.connection.Query(sql, directory, name)
 	if error != nil { return nil, error }
 	defer rows.Close()
 
 	error = rows.Scan(&id, &fingerprint)
 	if error != nil { return nil, error }
 
-	return &File{id, path, fingerprint}, nil
+	return &File{id, directory, name, fingerprint}, nil
 }
 
 func (this Database) FilesByFingerprint(fingerprint string) ([]File, error) {
-	sql := `SELECT id, path
+	sql := `SELECT id, directory, name
 	        FROM file
 	        WHERE fingerprint = ?`
 
         if rows.Err() != nil { return nil, error }
 
 		var fileId uint
-		var path string
-		error = rows.Scan(&fileId, &path)
+		var directory string
+		var name string
+		error = rows.Scan(&fileId, &directory, &name)
 		if error != nil { return nil, error }
 
-		files = append(files, File{fileId, path, fingerprint})
+		files = append(files, File{fileId, directory, name, fingerprint})
 	}
 
 	return files, nil
 }
 
 func (this Database) DuplicateFiles() ([][]File, error) {
-    sql := `SELECT id, path, fingerprint
+    sql := `SELECT id, directory, name, fingerprint
             FROM file
             WHERE fingerprint IN (SELECT fingerprint
                                 FROM file
         if rows.Err() != nil { return nil, error }
 
 		var fileId uint
-		var path string
+		var directory string
+		var name string
 		var fingerprint string
-		error = rows.Scan(&fileId, &path, &fingerprint)
+		error = rows.Scan(&fileId, &directory, &name, &fingerprint)
 		if error != nil { return nil, error }
 
 	    if fingerprint != previousFingerprint {
             previousFingerprint = fingerprint
         }
 
-		fileSet = append(fileSet, File{fileId, path, fingerprint})
+		fileSet = append(fileSet, File{fileId, directory, name, fingerprint})
 	}
 
     // ensure last file set is added
 }
 
 func (this Database) AddFile(path string, fingerprint string) (*File, error) {
-	sql := `INSERT INTO file (path, fingerprint)
-	        VALUES (?,?)`
+    directory, name := filepath.Split(path)
+    directory = filepath.Clean(directory)
 
-	result, error := this.connection.Exec(sql, path, fingerprint)
+	sql := `INSERT INTO file (directory, name, fingerprint)
+	        VALUES (?, ?, ?)`
+
+	result, error := this.connection.Exec(sql, directory, name, fingerprint)
 	if error != nil { return nil, error }
 
 	id, error := result.LastInsertId()
 	if error != nil { return nil, error }
 	if rowsAffected != 1 { return nil, errors.New("Expected exactly one row to be affected.") }
 
-	return &File{uint(id), path, fingerprint}, nil
+	return &File{uint(id), directory, name, fingerprint}, nil
 }
 
 func (this Database) FilesWithTags(tagNames []string) ([]File, error) {
-	sql := `SELECT id, path, fingerprint
+	sql := `SELECT id, directory, name, fingerprint
             FROM file
             WHERE id IN (
                 SELECT file_id
         if rows.Err() != nil { return nil, error }
 
 		var fileId uint
-		var path string
+		var directory string
+		var name string
 		var fingerprint string
-		error = rows.Scan(&fileId, &path, &fingerprint)
+		error = rows.Scan(&fileId, &directory, &name, &fingerprint)
 		if error != nil { return nil, error }
 
-		files = append(files, File{fileId, path, fingerprint})
+		files = append(files, File{fileId, directory, name, fingerprint})
 	}
 
 	return files, nil
 
     sql = `CREATE TABLE IF NOT EXISTS file (
                id INTEGER PRIMARY KEY,
-               path TEXT UNIQUE NOT NULL,
-               fingerprint TEXT NOT NULL
+               directory TEXT NOT NULL,
+               name TEXT NOT NULL,
+               fingerprint TEXT NOT NULL,
+               CONSTRAINT con_file_path UNIQUE (directory, name)
            )`
 
 	_, error = this.connection.Exec(sql)
 	if error != nil { return error }
 
     sql = `CREATE INDEX IF NOT EXISTS idx_file_path
-           ON file(path)`
+           ON file(directory, name)`
 
 	_, error = this.connection.Exec(sql)
 	if error != nil { return error }

File src/main/entities/file.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
 
+import (
+    "path/filepath"
+)
+
 type File struct {
-	Id          uint
-	Path        string
+	Id uint
+	Directory string
+	Name string
 	Fingerprint string
 }
+
+func (this File) Path() string {
+    return filepath.Join(this.Directory, this.Name)
+}

File src/main/fusevfs.go

     if error != nil { log.Fatalf("Could not retrieve file #%v: %v", fileId, error) }
     if file == nil { return &fuse.Attr{Mode: fuse.S_IFREG}, fuse.ENOENT }
 
-    fileInfo, error := os.Stat(file.Path)
+    fileInfo, error := os.Stat(file.Path())
     var size int64
     if error == nil {
         size = fileInfo.Size()
 	file, error := db.File(fileId)
 	if error != nil { log.Fatalf("Could not find file %v in database.", fileId) }
 
-	return file.Path, fuse.OK
+	return file.Path(), fuse.OK
 }
 
 func (this FuseVfs) getLinkName(file File) string {
-    extension := filepath.Ext(file.Path)
-    fileName := filepath.Base(file.Path)
+    extension := filepath.Ext(file.Path())
+    fileName := filepath.Base(file.Path())
     linkName := fileName[0 : len(fileName) - len(extension)]
     suffix := "." + Uitoa(file.Id) + extension