MCPcopy
hub / github.com/keploy/keploy / FixFilePermission

Function FixFilePermission

utils/permissions_unix.go:307–324  ·  view source on GitHub ↗

FixFilePermission attempts to fix permission issues on a specific file by changing ownership. This is called when a file operation (read or write) fails due to permission issues, typically because the file is owned by root from an older sudo-based keploy version.

(ctx context.Context, logger *zap.Logger, filePath string)

Source from the content-addressed store, hash-verified

305// This is called when a file operation (read or write) fails due to permission issues,
306// typically because the file is owned by root from an older sudo-based keploy version.
307func FixFilePermission(ctx context.Context, logger *zap.Logger, filePath string) error {
308 // Get current user
309 currentUser, err := user.Current()
310 if err != nil {
311 return fmt.Errorf("failed to get current user: %w", err)
312 }
313
314 logger.Debug("Cannot access file (likely owned by root from older keploy version)",
315 zap.String("path", filePath))
316 logger.Info(fmt.Sprintf("Running: sudo chown %s %s", currentUser.Username, filePath))
317
318 if err := runSudoChown(ctx, currentUser.Username, false, filePath); err != nil {
319 return fmt.Errorf("failed to change ownership of %s: %w. Please run manually: sudo chown %s %s", filePath, err, currentUser.Username, filePath)
320 }
321
322 logger.Debug("Successfully fixed file permission", zap.String("path", filePath))
323 return nil
324}
325
326// EnsureKeployFolderPermissions checks and fixes permission issues (both read and write) on the keploy folder.
327// This should be called once at startup before any file operations to ensure all files

Callers

nothing calls this directly

Calls 3

runSudoChownFunction · 0.85
DebugMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected