|
1 | 1 | package remoteproc |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "archive/tar" |
4 | 5 | "bufio" |
| 6 | + "compress/gzip" |
| 7 | + "context" |
5 | 8 | "fmt" |
6 | 9 | "io" |
| 10 | + "net/http" |
| 11 | + "os" |
7 | 12 | "path/filepath" |
| 13 | + "runtime" |
8 | 14 | "strings" |
| 15 | + "sync" |
9 | 16 | "time" |
10 | 17 |
|
11 | 18 | "github.com/arm/remoteproc-runtime/e2e/limavm" |
| 19 | + "github.com/arm/remoteproc-runtime/e2e/repo" |
12 | 20 | "github.com/arm/remoteproc-runtime/e2e/runner" |
13 | 21 | ) |
14 | 22 |
|
@@ -123,3 +131,143 @@ func (r *Simulator) DeviceDir() string { |
123 | 131 | r.rootDir, "sys", "class", "remoteproc", fmt.Sprintf("remoteproc%d", r.index), |
124 | 132 | ) |
125 | 133 | } |
| 134 | + |
| 135 | +var downloadLocks sync.Map |
| 136 | + |
| 137 | +func DownloadSimulator(ctx context.Context) (string, error) { |
| 138 | + const version = "v0.0.8" |
| 139 | + arch := runtime.GOARCH |
| 140 | + |
| 141 | + cacheDir := filepath.Join(repo.MustFindRootDir(), ".downloads") |
| 142 | + extractDir := filepath.Join(cacheDir, arch, version) |
| 143 | + executablePath := filepath.Join(extractDir, "remoteproc-simulator") |
| 144 | + |
| 145 | + if fileExists(executablePath) { |
| 146 | + return executablePath, nil |
| 147 | + } |
| 148 | + |
| 149 | + lockKey := executablePath |
| 150 | + mu, _ := downloadLocks.LoadOrStore(lockKey, &sync.Mutex{}) |
| 151 | + lock := mu.(*sync.Mutex) |
| 152 | + |
| 153 | + lock.Lock() |
| 154 | + defer lock.Unlock() |
| 155 | + |
| 156 | + fmt.Printf("Downloading remoteproc-simulator %s for %s...\n", version, arch) |
| 157 | + |
| 158 | + if err := os.MkdirAll(extractDir, 0o755); err != nil { |
| 159 | + return "", fmt.Errorf("failed to create cache directory: %w", err) |
| 160 | + } |
| 161 | + |
| 162 | + versionWithoutV := strings.TrimPrefix(version, "v") |
| 163 | + assetName := fmt.Sprintf("remoteproc-simulator_%s_linux_%s.tar.gz", versionWithoutV, arch) |
| 164 | + assetURL := fmt.Sprintf("https://github.com/arm/remoteproc-simulator/releases/download/%s/%s", version, assetName) |
| 165 | + archivePath := filepath.Join(extractDir, assetName) |
| 166 | + |
| 167 | + if err := downloadFile(ctx, assetURL, archivePath); err != nil { |
| 168 | + return "", fmt.Errorf("failed to download file: %w", err) |
| 169 | + } |
| 170 | + |
| 171 | + if err := extractTarGz(archivePath, extractDir); err != nil { |
| 172 | + return "", fmt.Errorf("failed to extract archive: %w", err) |
| 173 | + } |
| 174 | + |
| 175 | + _ = os.Remove(archivePath) |
| 176 | + |
| 177 | + if err := os.Chmod(executablePath, 0o755); err != nil { |
| 178 | + return "", fmt.Errorf("failed to make file executable: %w", err) |
| 179 | + } |
| 180 | + |
| 181 | + return executablePath, nil |
| 182 | +} |
| 183 | + |
| 184 | +func downloadFile(ctx context.Context, url, targetPath string) error { |
| 185 | + req, err := http.NewRequestWithContext(ctx, "GET", url, nil) |
| 186 | + if err != nil { |
| 187 | + return err |
| 188 | + } |
| 189 | + |
| 190 | + client := &http.Client{Timeout: 5 * time.Minute} |
| 191 | + resp, err := client.Do(req) |
| 192 | + if err != nil { |
| 193 | + return err |
| 194 | + } |
| 195 | + defer func() { _ = resp.Body.Close() }() |
| 196 | + |
| 197 | + if resp.StatusCode != http.StatusOK { |
| 198 | + body, _ := io.ReadAll(resp.Body) |
| 199 | + return fmt.Errorf("download returned status %d: %s", resp.StatusCode, string(body)) |
| 200 | + } |
| 201 | + |
| 202 | + tmpPath := targetPath + ".tmp" |
| 203 | + out, err := os.Create(tmpPath) |
| 204 | + if err != nil { |
| 205 | + return err |
| 206 | + } |
| 207 | + defer func() { _ = out.Close() }() |
| 208 | + |
| 209 | + if _, err := io.Copy(out, resp.Body); err != nil { |
| 210 | + _ = os.Remove(tmpPath) |
| 211 | + return err |
| 212 | + } |
| 213 | + |
| 214 | + if err := out.Close(); err != nil { |
| 215 | + _ = os.Remove(tmpPath) |
| 216 | + return err |
| 217 | + } |
| 218 | + |
| 219 | + return os.Rename(tmpPath, targetPath) |
| 220 | +} |
| 221 | + |
| 222 | +func extractTarGz(archivePath, extractDir string) error { |
| 223 | + f, err := os.Open(archivePath) |
| 224 | + if err != nil { |
| 225 | + return err |
| 226 | + } |
| 227 | + defer func() { _ = f.Close() }() |
| 228 | + |
| 229 | + gzr, err := gzip.NewReader(f) |
| 230 | + if err != nil { |
| 231 | + return err |
| 232 | + } |
| 233 | + defer func() { _ = gzr.Close() }() |
| 234 | + |
| 235 | + tr := tar.NewReader(gzr) |
| 236 | + |
| 237 | + for { |
| 238 | + header, err := tr.Next() |
| 239 | + if err == io.EOF { |
| 240 | + break |
| 241 | + } |
| 242 | + if err != nil { |
| 243 | + return err |
| 244 | + } |
| 245 | + |
| 246 | + if header.Typeflag == tar.TypeDir { |
| 247 | + continue |
| 248 | + } |
| 249 | + |
| 250 | + target := filepath.Join(extractDir, header.Name) |
| 251 | + if err := os.MkdirAll(filepath.Dir(target), 0o755); err != nil { |
| 252 | + return err |
| 253 | + } |
| 254 | + |
| 255 | + out, err := os.OpenFile(target, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.FileMode(header.Mode)) |
| 256 | + if err != nil { |
| 257 | + return err |
| 258 | + } |
| 259 | + |
| 260 | + if _, err := io.Copy(out, tr); err != nil { |
| 261 | + _ = out.Close() |
| 262 | + return err |
| 263 | + } |
| 264 | + _ = out.Close() |
| 265 | + } |
| 266 | + |
| 267 | + return nil |
| 268 | +} |
| 269 | + |
| 270 | +func fileExists(path string) bool { |
| 271 | + _, err := os.Stat(path) |
| 272 | + return err == nil |
| 273 | +} |
0 commit comments