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)
| 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. |
| 307 | func 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 |
nothing calls this directly
no test coverage detected