// Adds a file to the database. func InsertFile(tx *Tx, path string, fingerprint fingerprint.Fingerprint, modTime time.Time, size int64, isDir bool) (*entities.File, error) { directory := filepath.Dir(path) name := filepath.Base(path) sql := ` INSERT INTO file (directory, name, fingerprint, mod_time, size, is_dir) VALUES (?, ?, ?, ?, ?, ?)` result, err := tx.Exec(sql, directory, name, string(fingerprint), modTime, size, isDir) if err != nil { return nil, err } id, err := result.LastInsertId() if err != nil { return nil, err } rowsAffected, err := result.RowsAffected() if err != nil { return nil, err } if rowsAffected != 1 { panic("expected exactly one row to be affected.") } return &entities.File{entities.FileId(id), directory, name, fingerprint, modTime, size, isDir}, nil }
func readFileTags(rows *sql.Rows, fileTags entities.FileTags) (entities.FileTags, error) { for rows.Next() { if rows.Err() != nil { return nil, rows.Err() } var fileId entities.FileId var tagId entities.TagId var valueId entities.ValueId err := rows.Scan(&fileId, &tagId, &valueId) if err != nil { return nil, err } fileTags = append(fileTags, &entities.FileTag{entities.FileId(fileId), tagId, valueId, true, false}) } return fileTags, nil }
func (vfs FuseVfs) parseFileId(name string) entities.FileId { parts := strings.Split(name, ".") var index int switch len(parts) { case 1: return 0 case 2: index = 1 default: index = len(parts) - 2 } id, err := asciiToFileId(parts[index]) if err != nil { return 0 } return entities.FileId(id) }
// Updates a file in the database. func UpdateFile(tx *Tx, fileId entities.FileId, path string, fingerprint fingerprint.Fingerprint, modTime time.Time, size int64, isDir bool) (*entities.File, error) { directory := filepath.Dir(path) name := filepath.Base(path) sql := ` UPDATE file SET directory = ?, name = ?, fingerprint = ?, mod_time = ?, size = ?, is_dir = ? WHERE id = ?` result, err := tx.Exec(sql, directory, name, string(fingerprint), modTime, size, isDir, int(fileId)) if err != nil { return nil, err } rowsAffected, err := result.RowsAffected() if err != nil { return nil, err } if rowsAffected != 1 { panic("expected exactly one row to be affected.") } return &entities.File{entities.FileId(fileId), directory, name, fingerprint, modTime, size, isDir}, nil }
func asciiToFileId(str string) (entities.FileId, error) { ui64, err := strconv.ParseUint(str, 10, 0) return entities.FileId(ui64), err }