Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

internal/cli: check for delete permissions before starting to prune #1269

Merged
merged 1 commit into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions internal/cli/snapshot.go
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,28 @@ func (c *PruneBlockCommand) validateAgainstSnapshot(stack *node.Node, dbHandles
return nil
}

// checkDeletePermissions checks if the user has the permission to
// delete the given `path`.
func checkDeletePermissions(path string) (bool, error) {
dirInfo, err := os.Stat(path)
if err != nil {
return false, err
}
// Check if the user has write and execute permissions on the directory
if dirInfo.Mode().Perm()&(0200|0100) == (0200 | 0100) {
// Also check if the parent directory has write permissions because delete needs them
parentDir := filepath.Dir(path)
parentDirInfo, err := os.Stat(parentDir)
if err != nil {
return false, err
}
if parentDirInfo.Mode().Perm()&0200 != 0 {
return true, nil
}
}
return false, nil
}

// pruneBlock is the entry point for the ancient pruning process. Based on the user specified
// params, it will prune the ancient data. It also handles the case where the pruning process
// was interrupted earlier.
Expand All @@ -466,6 +488,17 @@ func (c *PruneBlockCommand) pruneBlock(stack *node.Node, fdHandles int) error {
}

newAncientPath := filepath.Join(path, "ancient_back")

// Check if we have delete permissions on the ancient datadir path beforehand
allow, err := checkDeletePermissions(oldAncientPath)
if err != nil {
log.Error("Failed to check delete permissions for ancient datadir", "path", oldAncientPath, "err", err)
return err
}
if !allow {
return fmt.Errorf("user doesn't have delete permissions on ancient datadir: %s", oldAncientPath)
}

blockpruner := pruner.NewBlockPruner(stack, oldAncientPath, newAncientPath, c.blockAmountReserved)

lock, exist, err := fileutil.Flock(filepath.Join(oldAncientPath, "PRUNEFLOCK"))
Expand Down
Loading