diff --git a/pkg/storage/fs/owncloudsql/filecache/filecache.go b/pkg/storage/fs/owncloudsql/filecache/filecache.go index 935f95665fc..d7590ee2d03 100644 --- a/pkg/storage/fs/owncloudsql/filecache/filecache.go +++ b/pkg/storage/fs/owncloudsql/filecache/filecache.go @@ -41,6 +41,7 @@ type Cache struct { db *sql.DB } +// NewMysql returns a new Cache instance connecting to a MySQL database func NewMysql(dsn string) (*Cache, error) { sqldb, err := sql.Open("mysql", dsn) if err != nil { @@ -58,6 +59,7 @@ func NewMysql(dsn string) (*Cache, error) { return New("mysql", sqldb) } +// New returns a new Cache instance connecting to the given sql.DB func New(driver string, sqldb *sql.DB) (*Cache, error) { return &Cache{ driver: driver, @@ -65,7 +67,8 @@ func New(driver string, sqldb *sql.DB) (*Cache, error) { }, nil } -func (c *Cache) GetNumericStorageId(id string) (int, error) { +// GetNumericStorageID returns the database id for the given storage +func (c *Cache) GetNumericStorageID(id string) (int, error) { row := c.db.QueryRow("Select numeric_id from oc_storages where id = ?", id) var nid int switch err := row.Scan(&nid); err { @@ -133,8 +136,9 @@ func (c *Cache) rowToFile(row Scannable) (*File, error) { }, nil } +// Get returns the cache entry for the specified storage/path func (c *Cache) Get(s interface{}, p string) (*File, error) { - storageId, err := toIntId(s) + storageID, err := toIntID(s) if err != nil { return nil, err } @@ -142,12 +146,13 @@ func (c *Cache) Get(s interface{}, p string) (*File, error) { phashBytes := md5.Sum([]byte(p)) phash := hex.EncodeToString(phashBytes[:]) - row := c.db.QueryRow("Select fileid, storage, path, parent, permissions, mimetype, mimepart, size, mtime, storage_mtime, encrypted, unencrypted_size, name, etag, checksum from oc_filecache where path_hash = ? and storage = ?", phash, storageId) + row := c.db.QueryRow("Select fileid, storage, path, parent, permissions, mimetype, mimepart, size, mtime, storage_mtime, encrypted, unencrypted_size, name, etag, checksum from oc_filecache where path_hash = ? and storage = ?", phash, storageID) return c.rowToFile(row) } +// Path returns the path for the specified entry func (c *Cache) Path(id interface{}) (string, error) { - id, err := toIntId(id) + id, err := toIntID(id) if err != nil { return "", err } @@ -161,6 +166,7 @@ func (c *Cache) Path(id interface{}) (string, error) { return path, nil } +// Permissions returns the permissions for the specified storage/path func (c *Cache) Permissions(storage interface{}, p string) (*provider.ResourcePermissions, error) { entry, err := c.Get(storage, p) if err != nil { @@ -175,8 +181,9 @@ func (c *Cache) Permissions(storage interface{}, p string) (*provider.ResourcePe return conversions.RoleFromOCSPermissions(perms).CS3ResourcePermissions(), nil } +// InsertOrUpdate creates or updates a cache entry func (c *Cache) InsertOrUpdate(storage interface{}, data map[string]interface{}) (int, error) { - storageId, err := toIntId(storage) + storageID, err := toIntID(storage) if err != nil { return -1, err } @@ -196,7 +203,7 @@ func (c *Cache) InsertOrUpdate(storage interface{}, data map[string]interface{}) if parentPath == "." { parentPath = "" } - parent, err := c.Get(storageId, parentPath) + parent, err := c.Get(storageID, parentPath) if err != nil { return -1, fmt.Errorf("could not find parent %s, %s, %v, %w", parentPath, path, parent, err) } @@ -274,12 +281,13 @@ func (c *Cache) InsertOrUpdate(storage interface{}, data map[string]interface{}) return int(id), nil } +// Copy creates a copy of the specified entry at the target path func (c *Cache) Copy(storage interface{}, sourcePath, targetPath string) (int, error) { - storageId, err := toIntId(storage) + storageID, err := toIntID(storage) if err != nil { return -1, err } - source, err := c.Get(storageId, sourcePath) + source, err := c.Get(storageID, sourcePath) if err != nil { return -1, errors.Wrap(err, "could not find source") } @@ -306,17 +314,18 @@ func (c *Cache) Copy(storage interface{}, sourcePath, targetPath string) (int, e return c.InsertOrUpdate(storage, data) } +// Move moves the specified entry to the target path func (c *Cache) Move(storage interface{}, sourcePath, targetPath string) error { - storageId, err := toIntId(storage) + storageID, err := toIntID(storage) if err != nil { return err } - source, err := c.Get(storageId, sourcePath) + source, err := c.Get(storageID, sourcePath) if err != nil { return errors.Wrap(err, "could not find source") } newParentPath := strings.TrimRight(filepath.Dir(targetPath), "/") - newParent, err := c.Get(storageId, newParentPath) + newParent, err := c.Get(storageID, newParentPath) if err != nil { return errors.Wrap(err, "could not find new parent") } @@ -332,7 +341,7 @@ func (c *Cache) Move(storage interface{}, sourcePath, targetPath string) error { } defer stmt.Close() phashBytes := md5.Sum([]byte(targetPath)) - _, err = stmt.Exec(newParent.Id, targetPath, filepath.Base(targetPath), hex.EncodeToString(phashBytes[:]), storageId, source.Id) + _, err = stmt.Exec(newParent.Id, targetPath, filepath.Base(targetPath), hex.EncodeToString(phashBytes[:]), storageID, source.Id) if err != nil { return err } @@ -351,7 +360,7 @@ func (c *Cache) Move(storage interface{}, sourcePath, targetPath string) error { for id, path := range children { path = strings.Replace(path, sourcePath, targetPath, -1) phashBytes = md5.Sum([]byte(path)) - _, err = stmt.Exec(source.Id, path, filepath.Base(path), hex.EncodeToString(phashBytes[:]), storageId, id) + _, err = stmt.Exec(source.Id, path, filepath.Base(path), hex.EncodeToString(phashBytes[:]), storageID, id) if err != nil { tx.Rollback() return err @@ -361,6 +370,7 @@ func (c *Cache) Move(storage interface{}, sourcePath, targetPath string) error { return tx.Commit() } +// Delete removes the specified storage/path from the cache func (c *Cache) Delete(storage interface{}, user, path, trashPath string) error { err := c.Move(storage, path, trashPath) if err != nil { @@ -389,17 +399,18 @@ func (c *Cache) Delete(storage interface{}, user, path, trashPath string) error return nil } +// GetRecycleItem returns the specified recycle item func (c *Cache) GetRecycleItem(user, path string, timestamp int) (*TrashItem, error) { row := c.db.QueryRow("SELECT auto_id, id, location FROM oc_files_trash WHERE id = ? and user = ? and timestamp = ?", path, user, timestamp) - var autoid int + var autoID int var id, location string - err := row.Scan(&autoid, &id, &location) + err := row.Scan(&autoID, &id, &location) if err != nil { return nil, err } return &TrashItem{ - Id: autoid, + Id: autoID, Name: id, User: user, Path: location, @@ -407,21 +418,22 @@ func (c *Cache) GetRecycleItem(user, path string, timestamp int) (*TrashItem, er }, nil } +// PurgeRecycleItem deletes the specified item from the cache func (c *Cache) PurgeRecycleItem(user, path string, timestamp int) error { row := c.db.QueryRow("Select auto_id, location from oc_files_trash where id = ? and user = ? and timestamp = ?", path, user, timestamp) - var autoId int + var autoID int var location string - err := row.Scan(&autoId, &location) + err := row.Scan(&autoID, &location) if err != nil { return err } - _, err = c.db.Exec("DELETE FROM oc_files_trash WHERE auto_id=?", autoId) + _, err = c.db.Exec("DELETE FROM oc_files_trash WHERE auto_id=?", autoID) if err != nil { return err } - storage, err := c.GetNumericStorageId("home::" + user) + storage, err := c.GetNumericStorageID("home::" + user) if err != nil { return err } @@ -434,12 +446,13 @@ func (c *Cache) PurgeRecycleItem(user, path string, timestamp int) error { return err } +// SetEtag set a new etag for the specified item func (c *Cache) SetEtag(storage interface{}, path, etag string) error { - storageId, err := toIntId(storage) + storageID, err := toIntID(storage) if err != nil { return err } - source, err := c.Get(storageId, path) + source, err := c.Get(storageID, path) if err != nil { return errors.Wrap(err, "could not find source") } @@ -447,10 +460,11 @@ func (c *Cache) SetEtag(storage interface{}, path, etag string) error { if err != nil { return err } - _, err = stmt.Exec(etag, storageId, source.Id) + _, err = stmt.Exec(etag, storageID, source.Id) return err } +// InsertMimetype adds a new mimetype to the database func (c *Cache) InsertMimetype(mimetype string) error { stmt, err := c.db.Prepare("INSERT INTO oc_mimetypes(mimetype) VALUES(?)") if err != nil { @@ -466,7 +480,7 @@ func (c *Cache) InsertMimetype(mimetype string) error { return nil } -func toIntId(rid interface{}) (int, error) { +func toIntID(rid interface{}) (int, error) { switch t := rid.(type) { case int: return t, nil diff --git a/pkg/storage/fs/owncloudsql/filecache/filecache_test.go b/pkg/storage/fs/owncloudsql/filecache/filecache_test.go index 3a73952da98..3107705b721 100644 --- a/pkg/storage/fs/owncloudsql/filecache/filecache_test.go +++ b/pkg/storage/fs/owncloudsql/filecache/filecache_test.go @@ -61,9 +61,9 @@ var _ = Describe("Filecache", func() { os.Remove(testDbFile.Name()) }) - Describe("GetNumericStorageId", func() { + Describe("GetNumericStorageID", func() { It("returns the proper storage id", func() { - nid, err := cache.GetNumericStorageId("home::admin") + nid, err := cache.GetNumericStorageID("home::admin") Expect(err).ToNot(HaveOccurred()) Expect(nid).To(Equal(1)) }) diff --git a/pkg/storage/fs/owncloudsql/owncloudsql.go b/pkg/storage/fs/owncloudsql/owncloudsql.go index 89a1d0048c2..faddbc1f7bc 100644 --- a/pkg/storage/fs/owncloudsql/owncloudsql.go +++ b/pkg/storage/fs/owncloudsql/owncloudsql.go @@ -563,7 +563,7 @@ func (fc *ocfs) getUserStorage(ctx context.Context) (int, error) { if !ok { return -1, fmt.Errorf("Could not get user for context") } - return fc.filecache.GetNumericStorageId("home::" + user.Username) + return fc.filecache.GetNumericStorageID("home::" + user.Username) } func (fs *ocfs) convertToResourceInfo(ctx context.Context, fi os.FileInfo, ip string, sp string, mdKeys []string) (*provider.ResourceInfo, error) {