mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-05-20 13:18:51 -05:00
[server][core][fs] filesystem.runInTx
This commit is contained in:
@@ -54,8 +54,7 @@ func (r Resource) Move(target string, conflictResolution ResourceBindConflictRes
|
||||
|
||||
var res Resource
|
||||
var deleted = false
|
||||
return res, deleted, r.f.db.WithTx(r.f.ctx, func(dbh *db.DbHandler) error {
|
||||
f := r.f.withDb(dbh)
|
||||
return res, deleted, r.f.runInTx(func(f filesystem) error {
|
||||
if conflictResolution == ResourceBindConflictResolutionOverwrite || conflictResolution == ResourceBindConflictResolutionDelete {
|
||||
if _, err := destParent.DeleteChildRecursive(destName, true); err == nil {
|
||||
deleted = true
|
||||
@@ -117,9 +116,7 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive bool, conflictReso
|
||||
var root db.Resource
|
||||
created := false
|
||||
deleted := false
|
||||
err = r.f.db.WithTx(r.f.ctx, func(dbh *db.DbHandler) error {
|
||||
f := r.f.withDb(dbh)
|
||||
|
||||
err = r.f.runInTx(func(f filesystem) error {
|
||||
root, created, deleted, err = f.createResource(
|
||||
id,
|
||||
destParent.ID(),
|
||||
@@ -166,14 +163,14 @@ func (r Resource) Copy(target string, id uuid.UUID, recursive bool, conflictReso
|
||||
}
|
||||
}
|
||||
|
||||
if _, err := dbh.CreateResources(f.ctx, children); err != nil {
|
||||
if _, err := f.db.CreateResources(f.ctx, children); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := f.db.RecomputePermissions(r.f.ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
if created {
|
||||
return dbh.UpdateResourceModified(f.ctx, destParent.ID())
|
||||
return f.db.UpdateResourceModified(f.ctx, destParent.ID())
|
||||
}
|
||||
return nil
|
||||
})
|
||||
|
||||
@@ -51,17 +51,16 @@ func (r Resource) CreateMemberResource(name string, id uuid.UUID, dir bool, conf
|
||||
}
|
||||
var result db.Resource
|
||||
var created bool
|
||||
err := r.f.db.WithTx(r.f.ctx, func(d *db.DbHandler) error {
|
||||
err := r.f.runInTx(func(f filesystem) error {
|
||||
var err error
|
||||
parent := r.ID()
|
||||
f := r.f.withDb(d)
|
||||
if result, created, _, err = f.createResource(id, parent, name, dir, 0, emptyContentType, emptyContentSHA256, conflictResolution); err != nil {
|
||||
return err
|
||||
} else if created {
|
||||
if err := f.db.RecomputePermissions(r.f.ctx, id); err != nil {
|
||||
if err := f.db.RecomputePermissions(f.ctx, id); err != nil {
|
||||
return err
|
||||
}
|
||||
return d.UpdateResourceModified(r.f.ctx, parent)
|
||||
return f.db.UpdateResourceModified(f.ctx, parent)
|
||||
}
|
||||
return nil
|
||||
})
|
||||
@@ -97,8 +96,7 @@ func (f filesystem) createResource(
|
||||
contentSHA256 string,
|
||||
conflictResolution ResourceBindConflictResolution,
|
||||
) (res db.Resource, created, deleted bool, err error) {
|
||||
err = f.db.WithTx(f.ctx, func(d *db.DbHandler) error {
|
||||
f := f.withDb(d)
|
||||
err = f.runInTx(func(f filesystem) error {
|
||||
res, err = f.db.CreateResource(f.ctx, db.CreateResourceParams{
|
||||
ID: id,
|
||||
Parent: &parent,
|
||||
@@ -129,8 +127,7 @@ func (f filesystem) createResource(
|
||||
counter := 1
|
||||
for {
|
||||
name := fmt.Sprintf("%s (%d)%s", basename, counter, ext)
|
||||
err = f.db.WithTx(f.ctx, func(d *db.DbHandler) error {
|
||||
f := f.withDb(d)
|
||||
err = f.runInTx(func(f filesystem) error {
|
||||
res, err = f.db.CreateResource(f.ctx, db.CreateResourceParams{
|
||||
ID: id,
|
||||
Parent: &parent,
|
||||
|
||||
@@ -56,3 +56,9 @@ func (f filesystem) RunInTx(fn func(FileSystem) error) error {
|
||||
return fn(f.WithDb(db))
|
||||
})
|
||||
}
|
||||
|
||||
func (f filesystem) runInTx(fn func(filesystem) error) error {
|
||||
return f.db.WithTx(f.ctx, func(db *db.DbHandler) error {
|
||||
return fn(f.withDb(db))
|
||||
})
|
||||
}
|
||||
|
||||
@@ -106,8 +106,7 @@ func (r Resource) RestoreDeleted(parentPathOrUUID string, name string, autoRenam
|
||||
return
|
||||
}
|
||||
|
||||
err = r.f.db.WithTx(r.f.ctx, func(d *db.DbHandler) error {
|
||||
f := r.f.withDb(d)
|
||||
err = r.f.runInTx(func(f filesystem) error {
|
||||
if p.id != *r.parentID || r.name != name {
|
||||
if update, err := f.db.UpdateResourceNameParent(f.ctx, db.UpdateResourceNameParentParams{ID: r.ID(), Name: name, Parent: p.id}); err != nil {
|
||||
if strings.Contains(err.Error(), "unique_member_resource_name") {
|
||||
@@ -136,13 +135,13 @@ func (r Resource) RestoreDeleted(parentPathOrUUID string, name string, autoRenam
|
||||
|
||||
func (f filesystem) deleteRecursive(id, parent uuid.UUID, softDelete, preserveRoot bool) error {
|
||||
var ids uuid.UUIDs
|
||||
err := f.db.WithTx(f.ctx, func(d *db.DbHandler) error {
|
||||
err := f.runInTx(func(f filesystem) error {
|
||||
var err error
|
||||
if _, ids, err = f.markDeleted(id, softDelete, preserveRoot); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return d.UpdateResourceModified(f.ctx, parent)
|
||||
return f.db.UpdateResourceModified(f.ctx, parent)
|
||||
})
|
||||
|
||||
if err == nil && !softDelete {
|
||||
|
||||
Reference in New Issue
Block a user