1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.build.gcs;
17 
18 import com.android.tradefed.build.BuildRetrievalError;
19 import com.android.tradefed.build.FileDownloadCache;
20 import com.android.tradefed.build.FileDownloadCacheFactory;
21 import com.android.tradefed.build.FileDownloadCacheWrapper;
22 import com.android.tradefed.build.IFileDownloader;
23 import com.android.tradefed.config.GlobalConfiguration;
24 import com.android.tradefed.host.IHostOptions;
25 import com.android.tradefed.util.GCSFileDownloader;
26 
27 import java.io.File;
28 
29 /** Downloader for GCS bucket that takes care of caching and resolving the global config. */
30 public class GCSDownloaderHelper {
31 
32     private IFileDownloader mFileDownloader = null;
33 
34     /**
35      * Fetch the resource from the GS path.
36      *
37      * @param destFile The {@link File} pointing to the retrieved resource.
38      * @param gsPath the path where the resource is located. For example: gs://bucket/path/file
39      * @throws BuildRetrievalError
40      */
fetchTestResource(File destFile, String gsPath)41     public void fetchTestResource(File destFile, String gsPath) throws BuildRetrievalError {
42         try {
43             getGCSFileDownloader().downloadFile(gsPath, destFile);
44         } catch (BuildRetrievalError e) {
45             deleteCacheEntry(gsPath);
46             throw e;
47         }
48     }
49 
50     /**
51      * Fetch the resource from the GS path.
52      *
53      * @param gsPath the path where the resource is located. For example: gs://bucket/path/file
54      * @return The {@link File} pointing to the retrieved resource.
55      * @throws BuildRetrievalError
56      */
fetchTestResource(String gsPath)57     public File fetchTestResource(String gsPath) throws BuildRetrievalError {
58         try {
59             return getGCSFileDownloader().downloadFile(gsPath);
60         } catch (BuildRetrievalError e) {
61             deleteCacheEntry(gsPath);
62             throw e;
63         }
64     }
65 
getGCSFileDownloader()66     private IFileDownloader getGCSFileDownloader() {
67         if (mFileDownloader == null) {
68             mFileDownloader =
69                     new FileDownloadCacheWrapper(
70                             getHostOptions().getDownloadCacheDir(), new GCSFileDownloader());
71         }
72         return mFileDownloader;
73     }
74 
75     /** Gets the {@link IHostOptions} instance to use. */
getHostOptions()76     private IHostOptions getHostOptions() {
77         return GlobalConfiguration.getInstance().getHostOptions();
78     }
79 
deleteCacheEntry(String remoteFilePath)80     private void deleteCacheEntry(String remoteFilePath) {
81         if (remoteFilePath != null) {
82             FileDownloadCache cache =
83                     FileDownloadCacheFactory.getInstance()
84                             .getCache(getHostOptions().getDownloadCacheDir());
85             cache.deleteCacheEntry(remoteFilePath);
86         }
87     }
88 }
89