From f7a44bee3c0ba1112b36850c05f1b0667f6339bc Mon Sep 17 00:00:00 2001 From: Andre Detsch Date: Thu, 5 Oct 2023 19:01:00 -0300 Subject: [PATCH] targets: offline-update: Check if dest dir already has data This commit prevents the execution of "targets offlile-update" using a destination directory that already contains update data (i.e., "ostree_repo" or "apps" subdirectories). There is no check for the tuf subdirectory, since the metadata in that subdir is updated by default, and there is no conflict. It is possible to override the default behavior by using a new --allow-multiple-targets option. Signed-off-by: Andre Detsch --- subcommands/targets/offline-update.go | 32 ++++++++++++++++++++++----- 1 file changed, 27 insertions(+), 5 deletions(-) diff --git a/subcommands/targets/offline-update.go b/subcommands/targets/offline-update.go index a1043ad9..21f6b14b 100644 --- a/subcommands/targets/offline-update.go +++ b/subcommands/targets/offline-update.go @@ -29,11 +29,12 @@ type ( ) var ( - ouTag string - ouProd bool - ouExpiresIn int - ouTufOnly bool - ouNoApps bool + ouTag string + ouProd bool + ouExpiresIn int + ouTufOnly bool + ouNoApps bool + ouAllowMultipleTargets bool ) func init() { @@ -62,6 +63,8 @@ func init() { "Fetch only TUF metadata") offlineUpdateCmd.Flags().BoolVarP(&ouNoApps, "no-apps", "", false, "Skip fetching Target Apps") + offlineUpdateCmd.Flags().BoolVarP(&ouAllowMultipleTargets, "allow-multiple-targets", "", false, + "Allow multiple targets to be stored in the same directory") } func doOfflineUpdate(cmd *cobra.Command, args []string) { @@ -84,6 +87,14 @@ func doOfflineUpdate(cmd *cobra.Command, args []string) { fmt.Println("Successfully refreshed and downloaded TUF metadata") if !ouTufOnly { + if !isDstDirClean(dstDir) { + if !ouAllowMultipleTargets { + subcommands.DieNotNil(errors.New(`Destination directory already has update data. +Provide a clean destination directory or re-run with --allow-multiple-targets to add a new target to a directory which already has update data. +Notice that multiple targets in the same directory is only supported in LmP >= v92.`)) + } + } + fmt.Printf("Downloading an ostree repo from the Target's OE build %d...\n", ti.ostreeVersion) subcommands.DieNotNil(downloadOstree(factory, ti.ostreeVersion, ti.hardwareID, dstDir), "Failed to download Target's ostree repo:") if !ouNoApps { @@ -152,6 +163,17 @@ func getTargetInfo(factory string, targetName string) (*ouTargetInfo, error) { return &info, nil } +func isDstDirClean(dstDir string) bool { + for _, subDir := range []string{"ostree_repo", "apps"} { + fullPath := path.Join(dstDir, subDir) + if _, err := os.Stat(fullPath); !os.IsNotExist(err) { + fmt.Println(fullPath + " already exists") + return false + } + } + return true +} + func downloadTufRepo(factory string, target string, tag string, prod bool, expiresIn int, dstDir string) error { // v1 - auto-generated by tuf_keyserver (default, on Factory creation); // v2 - auto-generated by ota-lite to take keys online (default, on Factory creation);