Debian Codegraph @

arm64/instgen/xmlspec/util.go

raw symbols SCIP
1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package xmlspec
6
7 // This file contains utility functions.
8
9 import (
10 "archive/tar"
11 "compress/gzip"
12 "fmt"
13 "io"
14 "net/http"
15 "os"
16 "path/filepath"
17 "strings"
18 )
19
20 // GetArm64XMLSpec downloads the ARM64 XML spec from the given URL to a temporary directory.
21 // It returns the path to directory containing all instruction XML files.
22 // If anything goes wrong, it will return an error.
23 func GetArm64XMLSpec(tmpDir string, url string, version string) (string, error) {
24 if err := downloadArm64XMLSpec(tmpDir, url); err != nil {
25 return "", fmt.Errorf("downloadArm64XMLSpec failed: %v", err)
26 }
27
28 // The tarball extracts to a directory like "ISA_A64_xml_A_profile-2025-12".
29 // We need to find it.
30 entries, err := os.ReadDir(tmpDir)
31 if err != nil {
32 return "", fmt.Errorf("os.ReadDir failed: %v", err)
33 }
34
35 var xmlDir string
36 for _, e := range entries {
37 if e.IsDir() && strings.HasPrefix(e.Name(), version) {
38 xmlDir = filepath.Join(tmpDir, e.Name())
39 break
40 }
41 }
42
43 if xmlDir == "" {
44 return "", fmt.Errorf("could not find extracted XML directory in %s", tmpDir)
45 }
46 return xmlDir, nil
47 }
48
49 // downloadArm64XMLSpec downloads the ARM64 XML spec from the given URL to the given directory.
50 func downloadArm64XMLSpec(dir string, url string) error {
51 resp, err := http.Get(url)
52 if err != nil {
53 return fmt.Errorf("fetching ARM64 XML spec from %s failed: %v", url, err)
54 }
55 defer resp.Body.Close()
56
57 if resp.StatusCode != http.StatusOK {
58 return fmt.Errorf("fetching ARM64 XML spec from %s returned status: %s", url, resp.Status)
59 }
60
61 if err := extractTarGz(resp.Body, dir); err != nil {
62 return err
63 }
64 return nil
65 }
66
67 // extractTarGz extracts the tar.gz file to the given directory.
68 func extractTarGz(r io.Reader, dir string) error {
69 gzr, err := gzip.NewReader(r)
70 if err != nil {
71 return err
72 }
73 defer gzr.Close()
74
75 tr := tar.NewReader(gzr)
76
77 // Iterate over the entries in the tarball.
78 for {
79 header, err := tr.Next()
80 if err == io.EOF {
81 break
82 }
83 if err != nil {
84 return err
85 }
86
87 target := filepath.Join(dir, header.Name)
88
89 switch header.Typeflag {
90 // directories
91 case tar.TypeDir:
92 if err := os.MkdirAll(target, 0755); err != nil {
93 return err
94 }
95 // regular files
96 case tar.TypeReg:
97 if err := os.MkdirAll(filepath.Dir(target), 0755); err != nil {
98 return err
99 }
100 f, err := os.Create(target)
101 if err != nil {
102 return err
103 }
104 if _, err := io.Copy(f, tr); err != nil {
105 f.Close()
106 return err
107 }
108 f.Close()
109 }
110 }
111 return nil
112 }
113
114 const ExpectedURL = "https://developer.arm.com/-/cdn-downloads/permalink/Exploration-Tools-A64-ISA/ISA_A64/ISA_A64_xml_A_profile-2025-12.tar.gz"
115 const ExpectedVersion = "ISA_A64_xml_A_profile-2025-12"