7171 fileToUpdate * mpi.File ,
7272 ) error
7373 SetIsConnected (isConnected bool )
74- MoveFileFromTempDirectory (ctx context.Context , fileAction * model. FileCache , tempDir string ) error
74+ renameFile (ctx context.Context , hash , fileName , tempDir string ) error
7575 UpdateClient (ctx context.Context , fileServiceClient mpi.FileServiceClient )
7676 }
7777
@@ -191,7 +191,7 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
191191 return model .Error , rollbackTempErr
192192 }
193193
194- rollbackTempFilesErr := fms .RollbackTempFiles (ctx )
194+ rollbackTempFilesErr := fms .backupFiles (ctx )
195195 if rollbackTempFilesErr != nil {
196196 return model .Error , rollbackTempFilesErr
197197 }
@@ -211,47 +211,19 @@ func (fms *FileManagerService) ConfigApply(ctx context.Context,
211211 return model .OK , nil
212212}
213213
214- func (fms * FileManagerService ) RollbackTempFiles (ctx context.Context ) error {
215- for _ , file := range fms .fileActions {
216- if file .Action == model .Add || file .Action == model .Unchanged {
217- continue
218- }
219-
220- filePath := file .File .GetFileMeta ().GetName ()
221-
222- if _ , err := os .Stat (filePath ); os .IsNotExist (err ) {
223- slog .DebugContext (ctx , "Unable to backup file content since file does not exist" ,
224- "file" , filePath )
225-
226- continue
227- }
228-
229- tempFilePath := filepath .Join (fms .tempRollbackDir , filePath )
230- slog .DebugContext (ctx , "Attempting to backup file content since file exists" , "temp_path" , tempFilePath )
231-
232- moveErr := fms .fileOperator .MoveFile (ctx , filePath , tempFilePath )
233-
234- if moveErr != nil {
235- return moveErr
236- }
237- }
238-
239- return nil
240- }
241-
242214func (fms * FileManagerService ) ClearCache () {
243215 slog .Debug ("Clearing cache and temp files after config apply" )
244216 clear (fms .fileActions )
245217 clear (fms .previousManifestFiles )
246218
247219 configErr := os .RemoveAll (fms .tempConfigDir )
248220 if configErr != nil {
249- slog .Error ("error removing temp config directory" , "path" , fms .tempConfigDir , "err" , configErr )
221+ slog .Error ("Error removing temp config directory" , "path" , fms .tempConfigDir , "err" , configErr )
250222 }
251223
252224 rollbackErr := os .RemoveAll (fms .tempRollbackDir )
253225 if rollbackErr != nil {
254- slog .Error ("error removing temp rollback directory" , "path" , fms .tempRollbackDir , "err" , rollbackErr )
226+ slog .Error ("Error removing temp rollback directory" , "path" , fms .tempRollbackDir , "err" , rollbackErr )
255227 }
256228}
257229
@@ -273,30 +245,14 @@ func (fms *FileManagerService) Rollback(ctx context.Context, instanceID string)
273245
274246 continue
275247 case model .Delete , model .Update :
276- fileMeta := fileAction .File .GetFileMeta ()
277- fileName := fileMeta .GetName ()
278-
279- tempFilePath := filepath .Join (fms .tempRollbackDir , fileName )
280-
281- // Create parent directories for the target file if they don't exist
282- if err := os .MkdirAll (filepath .Dir (fileName ), dirPerm ); err != nil {
283- return fmt .Errorf ("failed to create directories for %s: %w" , fileName , err )
284- }
285-
286- moveErr := os .Rename (tempFilePath , fileName )
287- if moveErr != nil {
288- return fmt .Errorf ("failed to rename file, %s to %s: %w" , tempFilePath , fileName , moveErr )
289- }
290-
291- content , readErr := os .ReadFile (fileMeta .GetName ())
292- if readErr != nil {
293- return fmt .Errorf ("error reading file, unable to generate hash: %s error: %w" ,
294- fileMeta .GetName (), readErr )
248+ content , err := fms .restoreFiles (fileAction )
249+ if err != nil {
250+ return err
295251 }
296252
297253 // currentFilesOnDisk needs to be updated after rollback action is performed
298- fileMeta .Hash = files .GenerateHash (content )
299- fms .currentFilesOnDisk [fileMeta .GetName ()] = fileAction .File
254+ fileAction . File . FileMeta .Hash = files .GenerateHash (content )
255+ fms .currentFilesOnDisk [fileAction . File . GetFileMeta () .GetName ()] = fileAction .File
300256 case model .Unchanged :
301257 fallthrough
302258 default :
@@ -513,6 +469,59 @@ func (fms *FileManagerService) UpdateManifestFile(ctx context.Context,
513469 return fms .fileOperator .WriteManifestFile (ctx , updatedFiles , fms .agentConfig .LibDir , fms .manifestFilePath )
514470}
515471
472+ func (fms * FileManagerService ) backupFiles (ctx context.Context ) error {
473+ for _ , file := range fms .fileActions {
474+ if file .Action == model .Add || file .Action == model .Unchanged {
475+ continue
476+ }
477+
478+ filePath := file .File .GetFileMeta ().GetName ()
479+
480+ if _ , err := os .Stat (filePath ); os .IsNotExist (err ) {
481+ slog .DebugContext (ctx , "Unable to backup file content since file does not exist" ,
482+ "file" , filePath )
483+
484+ continue
485+ }
486+
487+ tempFilePath := filepath .Join (fms .tempRollbackDir , filePath )
488+ slog .DebugContext (ctx , "Attempting to backup file content since file exists" , "temp_path" , tempFilePath )
489+
490+ moveErr := fms .fileOperator .MoveFile (ctx , filePath , tempFilePath )
491+
492+ if moveErr != nil {
493+ return moveErr
494+ }
495+ }
496+
497+ return nil
498+ }
499+
500+ func (fms * FileManagerService ) restoreFiles (fileAction * model.FileCache ) ([]byte , error ) {
501+ fileMeta := fileAction .File .GetFileMeta ()
502+ fileName := fileMeta .GetName ()
503+
504+ tempFilePath := filepath .Join (fms .tempRollbackDir , fileName )
505+
506+ // Create parent directories for the target file if they don't exist
507+ if err := os .MkdirAll (filepath .Dir (fileName ), dirPerm ); err != nil {
508+ return nil , fmt .Errorf ("failed to create directories for %s: %w" , fileName , err )
509+ }
510+
511+ moveErr := os .Rename (tempFilePath , fileName )
512+ if moveErr != nil {
513+ return nil , fmt .Errorf ("failed to rename file, %s to %s: %w" , tempFilePath , fileName , moveErr )
514+ }
515+
516+ content , readErr := os .ReadFile (fileMeta .GetName ())
517+ if readErr != nil {
518+ return nil , fmt .Errorf ("error reading file, unable to generate hash: %s error: %w" ,
519+ fileMeta .GetName (), readErr )
520+ }
521+
522+ return content , nil
523+ }
524+
516525func (fms * FileManagerService ) manifestFile () (map [string ]* model.ManifestFile , map [string ]* mpi.File , error ) {
517526 if _ , err := os .Stat (fms .manifestFilePath ); err != nil {
518527 return nil , nil , err
@@ -596,7 +605,8 @@ actionsLoop:
596605
597606 continue
598607 case model .Add , model .Update :
599- err := fms .fileServiceOperator .MoveFileFromTempDirectory (ctx , fileAction , tempDir )
608+ fileMeta := fileAction .File .GetFileMeta ()
609+ err := fms .fileServiceOperator .renameFile (ctx , fileMeta .GetHash (), fileMeta .GetName (), tempDir )
600610 if err != nil {
601611 actionError = err
602612
0 commit comments