func deleteScriptsAndSettingsExceptMostRecent()

in internal/cleanup/cleanup.go [43:68]


func deleteScriptsAndSettingsExceptMostRecent(ctx *log.Context, metadata types.RCMetadata, h types.HandlerEnvironment, runAsUser string) {
	runtimeSettingsRegexFormat := metadata.ExtName + ".\\d+.settings"
	runtimeSettingsLastSeqNumFormat := metadata.ExtName + ".%d.settings"

	// check if directory exists
	_, err := os.Open(metadata.DownloadPath)
	if err == nil {
		err := utils.TryClearExtensionScriptsDirectoriesAndSettingsFilesExceptMostRecent(metadata.DownloadPath, h.HandlerEnvironment.ConfigFolder, "",
			uint64(metadata.SeqNum), runtimeSettingsRegexFormat, runtimeSettingsLastSeqNumFormat)
		if err != nil {
			ctx.Log("event", "could not clear settings and script files", "error", err)
		}
	} else {
		ctx.Log("message", "directory does not exist. Skipping cleanup")
	}

	if runAsUser != "" {
		runAsDownloadParent := filepath.Join(fmt.Sprintf(constants.RunAsDir, runAsUser), metadata.DownloadDir)
		seqNumString := strconv.Itoa(metadata.SeqNum)
		ctx.Log("message", "removing all files from the download 'runas' directory "+runAsDownloadParent)
		err = utils.TryDeleteDirectoriesExcept(runAsDownloadParent, seqNumString)
		if err != nil {
			ctx.Log("event", "could not clear runas script")
		}
	}
}