Skip to content

Commit

Permalink
fix cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
davies committed May 26, 2023
1 parent 3e8379a commit 289affb
Show file tree
Hide file tree
Showing 7 changed files with 24 additions and 14 deletions.
2 changes: 1 addition & 1 deletion cmd/gc.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func gc(ctx *cli.Context) error {
edge := time.Now().Add(-time.Duration(format.TrashDays) * 24 * time.Hour)
if delete {
cleanTrashSpin := progress.AddCountSpinner("Cleaned trash")
m.CleanupTrashBefore(c, edge, cleanTrashSpin.Increment)
m.CleanupTrashBefore(c, edge, cleanTrashSpin.IncrBy)
cleanTrashSpin.Done()

cleanDetachedNodeSpin := progress.AddCountSpinner("Cleaned detached nodes")
Expand Down
2 changes: 1 addition & 1 deletion cmd/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func restore(ctx *cli.Context) error {
return fmt.Errorf("only root can restore files from trash")
}
removePassword(ctx.Args().Get(0))
m := meta.NewClient(ctx.Args().Get(0), &meta.Config{Retries: 10, Strict: true})
m := meta.NewClient(ctx.Args().Get(0), nil)
_, err := m.Load(true)
if err != nil {
return err
Expand Down
13 changes: 5 additions & 8 deletions pkg/meta/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -2175,7 +2175,7 @@ func (m *baseMeta) CleanupDetachedNodesBefore(ctx Context, edge time.Time, incre
}
}

func (m *baseMeta) CleanupTrashBefore(ctx Context, edge time.Time, increProgress func()) {
func (m *baseMeta) CleanupTrashBefore(ctx Context, edge time.Time, increProgress func(int)) {
logger.Debugf("cleanup trash: started")
now := time.Now()
var st syscall.Errno
Expand Down Expand Up @@ -2214,15 +2214,12 @@ func (m *baseMeta) CleanupTrashBefore(ctx Context, edge time.Time, increProgress
entries = entries[1:]
}
for _, se := range subEntries {
if se.Attr.Typ == TypeDirectory {
st = m.en.doRmdir(ctx, e.Inode, string(se.Name), nil)
} else {
st = m.en.doUnlink(ctx, e.Inode, string(se.Name), nil)
}
var c uint64
st = m.Remove(ctx, e.Inode, string(se.Name), &c)
if st == 0 {
count++
count += int(c)
if increProgress != nil {
increProgress()
increProgress(int(c))
}
} else {
logger.Warnf("delete from trash %s/%s: %s", e.Name, se.Name, st)
Expand Down
2 changes: 1 addition & 1 deletion pkg/meta/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,7 @@ type Meta interface {
// CleanStaleSessions cleans up sessions not active for more than 5 minutes
CleanStaleSessions()
// CleanupTrashBefore deletes all files in trash before the given time.
CleanupTrashBefore(ctx Context, edge time.Time, increProgress func())
CleanupTrashBefore(ctx Context, edge time.Time, increProgress func(int))
// CleanupDetachedNodesBefore deletes all detached nodes before the given time.
CleanupDetachedNodesBefore(ctx Context, edge time.Time, increProgress func())

Expand Down
7 changes: 6 additions & 1 deletion pkg/meta/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -1583,6 +1583,11 @@ func (m *redisMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentD
var dtyp uint8
var tattr Attr
var newSpace, newInode int64
keys := []string{m.inodeKey(parentSrc), m.entryKey(parentSrc), m.inodeKey(parentDst), m.entryKey(parentDst)}
if isTrash(parentSrc) {
// lock the parentDst
keys[0], keys[2] = keys[2], keys[0]
}
err := m.txn(ctx, func(tx *redis.Tx) error {
opened = false
dino, dtyp = 0, 0
Expand Down Expand Up @@ -1844,7 +1849,7 @@ func (m *redisMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentD
return nil
})
return err
}, m.inodeKey(parentSrc), m.entryKey(parentSrc), m.inodeKey(parentDst), m.entryKey(parentDst))
}, keys...)
if err == nil && !exchange && trash == 0 {
if dino > 0 && dtyp == TypeFile && tattr.Nlink == 0 {
m.fileDeleted(opened, false, dino, tattr.Length)
Expand Down
6 changes: 5 additions & 1 deletion pkg/meta/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -1604,6 +1604,10 @@ func (m *dbMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentDst
var dino Ino
var dn node
var newSpace, newInode int64
lockParent := parentSrc
if isTrash(lockParent) {
lockParent = parentDst
}
err := m.txn(func(s *xorm.Session) error {
opened = false
dino = 0
Expand Down Expand Up @@ -1879,7 +1883,7 @@ func (m *dbMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentDst
}
}
return err
}, parentSrc)
}, lockParent)
if err == nil && !exchange && trash == 0 {
if dino > 0 && dn.Type == TypeFile && dn.Nlink == 0 {
m.fileDeleted(opened, false, dino, dn.Length)
Expand Down
6 changes: 5 additions & 1 deletion pkg/meta/tkv.go
Original file line number Diff line number Diff line change
Expand Up @@ -1438,6 +1438,10 @@ func (m *kvMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentDst
var dtyp uint8
var tattr Attr
var newSpace, newInode int64
lockParent := parentSrc
if isTrash(lockParent) {
lockParent = parentDst
}
err := m.txn(func(tx *kvTxn) error {
opened = false
dino, dtyp = 0, 0
Expand Down Expand Up @@ -1667,7 +1671,7 @@ func (m *kvMeta) doRename(ctx Context, parentSrc Ino, nameSrc string, parentDst
tx.set(m.inodeKey(parentDst), m.marshal(&dattr))
}
return nil
}, parentSrc)
}, lockParent)
if err == nil && !exchange && trash == 0 {
if dino > 0 && dtyp == TypeFile && tattr.Nlink == 0 {
m.fileDeleted(opened, false, dino, tattr.Length)
Expand Down

0 comments on commit 289affb

Please sign in to comment.