| 1 |
//go:build !cgo && upgrade && ignore |
| 2 |
// +build !cgo,upgrade,ignore |
| 3 |
|
| 4 |
package main |
| 5 |
|
| 6 |
import ( |
| 7 |
"archive/zip" |
| 8 |
"bufio" |
| 9 |
"bytes" |
| 10 |
"errors" |
| 11 |
"fmt" |
| 12 |
"io" |
| 13 |
"io/ioutil" |
| 14 |
"log" |
| 15 |
"net/http" |
| 16 |
"os" |
| 17 |
"path" |
| 18 |
"path/filepath" |
| 19 |
"runtime" |
| 20 |
"strings" |
| 21 |
) |
| 22 |
|
| 23 |
func download(prefix string) (url string, content []byte, err error) { |
| 24 |
resp, err := http.Get("https://www.sqlite.org/download.html") |
| 25 |
if err != nil { |
| 26 |
return "", nil, err |
| 27 |
} |
| 28 |
defer resp.Body.Close() |
| 29 |
if resp.StatusCode != http.StatusOK { |
| 30 |
return "", nil, fmt.Errorf("downloading download.html: %s", resp.Status) |
| 31 |
} |
| 32 |
|
| 33 |
b, err := ioutil.ReadAll(resp.Body) |
| 34 |
if err != nil { |
| 35 |
return "", nil, err |
| 36 |
} |
| 37 |
html := string(b) |
| 38 |
start := strings.Index(html, `<!-- Download product data for scripts to read`) |
| 39 |
if start == -1 { |
| 40 |
return "", nil, errors.New("Unable to find download section on sqlite.org") |
| 41 |
} |
| 42 |
html = html[start:] |
| 43 |
end := strings.Index(html, `-->`) |
| 44 |
if end == -1 { |
| 45 |
return "", nil, errors.New("Unable to find download section on sqlite.org") |
| 46 |
} |
| 47 |
html = html[:end] |
| 48 |
for _, line := range strings.Split(html, "\n") { |
| 49 |
if strings.Contains(line, prefix) { |
| 50 |
if tok := strings.Split(line, ","); len(tok) >= 5 { |
| 51 |
url = fmt.Sprintf("https://www.sqlite.org/%s", tok[2]) |
| 52 |
break |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
if url == "" { |
| 58 |
return "", nil, fmt.Errorf("Unable to find prefix '%s' on sqlite.org", prefix) |
| 59 |
} |
| 60 |
|
| 61 |
fmt.Printf("Downloading %v\n", url) |
| 62 |
resp, err = http.Get(url) |
| 63 |
if err != nil { |
| 64 |
return "", nil, err |
| 65 |
} |
| 66 |
defer resp.Body.Close() |
| 67 |
if resp.StatusCode != http.StatusOK { |
| 68 |
return "", nil, fmt.Errorf("downloading %s: %s", url, resp.Status) |
| 69 |
} |
| 70 |
|
| 71 |
// Ready Body Content |
| 72 |
content, err = ioutil.ReadAll(resp.Body) |
| 73 |
if err != nil { |
| 74 |
return "", nil, err |
| 75 |
} |
| 76 |
|
| 77 |
return url, content, nil |
| 78 |
} |
| 79 |
|
| 80 |
func mergeFile(src string, dst string) error { |
| 81 |
// Open destination |
| 82 |
fdst, err := os.OpenFile(dst, os.O_APPEND|os.O_WRONLY, 0666) |
| 83 |
if err != nil { |
| 84 |
return err |
| 85 |
} |
| 86 |
|
| 87 |
// Read source content |
| 88 |
content, err := ioutil.ReadFile(src) |
| 89 |
if err != nil { |
| 90 |
fdst.Close() |
| 91 |
return err |
| 92 |
} |
| 93 |
|
| 94 |
// Add Additional newline |
| 95 |
if _, err := fdst.WriteString("\n"); err != nil { |
| 96 |
fdst.Close() |
| 97 |
return err |
| 98 |
} |
| 99 |
|
| 100 |
fmt.Printf("Merging: %s into %s\n", src, dst) |
| 101 |
if _, err = fdst.Write(content); err != nil { |
| 102 |
fdst.Close() |
| 103 |
return err |
| 104 |
} |
| 105 |
|
| 106 |
// Close may surface deferred write errors; the source must survive |
| 107 |
// unless the merge fully reached the destination. |
| 108 |
if err := fdst.Close(); err != nil { |
| 109 |
return err |
| 110 |
} |
| 111 |
|
| 112 |
// Only remove the source once it has been merged successfully. |
| 113 |
fmt.Printf("Removing: %s\n", src) |
| 114 |
return os.Remove(src) |
| 115 |
} |
| 116 |
|
| 117 |
func main() { |
| 118 |
fmt.Println("Go-SQLite3 Upgrade Tool") |
| 119 |
|
| 120 |
_, file, _, ok := runtime.Caller(0) |
| 121 |
if !ok { |
| 122 |
log.Fatal("could not get current file info") |
| 123 |
} |
| 124 |
basedir := filepath.Dir(filepath.Dir(file)) |
| 125 |
|
| 126 |
// Download Amalgamation |
| 127 |
_, amalgamation, err := download("sqlite-amalgamation-") |
| 128 |
if err != nil { |
| 129 |
log.Fatalf("Failed to download: sqlite-amalgamation; %s", err) |
| 130 |
} |
| 131 |
|
| 132 |
// Download Source |
| 133 |
//_, source, err := download("sqlite-src-") |
| 134 |
//if err != nil { |
| 135 |
// log.Fatalf("Failed to download: sqlite-src; %s", err) |
| 136 |
//} |
| 137 |
|
| 138 |
// Create Amalgamation Zip Reader |
| 139 |
rAmalgamation, err := zip.NewReader(bytes.NewReader(amalgamation), int64(len(amalgamation))) |
| 140 |
if err != nil { |
| 141 |
log.Fatal(err) |
| 142 |
} |
| 143 |
|
| 144 |
// Create Source Zip Reader |
| 145 |
//rSource, err := zip.NewReader(bytes.NewReader(source), int64(len(source))) |
| 146 |
//if err != nil { |
| 147 |
// log.Fatal(err) |
| 148 |
//} |
| 149 |
|
| 150 |
// Extract Amalgamation |
| 151 |
for _, zf := range rAmalgamation.File { |
| 152 |
var f *os.File |
| 153 |
switch path.Base(zf.Name) { |
| 154 |
case "sqlite3.c": |
| 155 |
f, err = os.Create(filepath.Join(basedir, "sqlite3-binding.c")) |
| 156 |
case "sqlite3.h": |
| 157 |
f, err = os.Create(filepath.Join(basedir, "sqlite3-binding.h")) |
| 158 |
case "sqlite3ext.h": |
| 159 |
f, err = os.Create(filepath.Join(basedir, "sqlite3ext.h")) |
| 160 |
default: |
| 161 |
continue |
| 162 |
} |
| 163 |
if err != nil { |
| 164 |
log.Fatal(err) |
| 165 |
} |
| 166 |
zr, err := zf.Open() |
| 167 |
if err != nil { |
| 168 |
log.Fatal(err) |
| 169 |
} |
| 170 |
|
| 171 |
_, err = io.WriteString(f, "#ifndef USE_LIBSQLITE3\n") |
| 172 |
if err != nil { |
| 173 |
zr.Close() |
| 174 |
f.Close() |
| 175 |
log.Fatal(err) |
| 176 |
} |
| 177 |
scanner := bufio.NewScanner(zr) |
| 178 |
var werr error |
| 179 |
for scanner.Scan() { |
| 180 |
text := scanner.Text() |
| 181 |
if text == `#include "sqlite3.h"` { |
| 182 |
text = `#include "sqlite3-binding.h" |
| 183 |
#ifdef __clang__ |
| 184 |
#define assert(condition) ((void)0) |
| 185 |
#endif |
| 186 |
` |
| 187 |
} |
| 188 |
_, werr = fmt.Fprintln(f, text) |
| 189 |
if werr != nil { |
| 190 |
break |
| 191 |
} |
| 192 |
} |
| 193 |
// A write failure must not be masked by scanner.Err(), which |
| 194 |
// reports nil in that case; either way a truncated output file |
| 195 |
// must never be reported as successfully extracted. |
| 196 |
if werr != nil { |
| 197 |
zr.Close() |
| 198 |
f.Close() |
| 199 |
log.Fatal(werr) |
| 200 |
} |
| 201 |
err = scanner.Err() |
| 202 |
if err != nil { |
| 203 |
zr.Close() |
| 204 |
f.Close() |
| 205 |
log.Fatal(err) |
| 206 |
} |
| 207 |
_, err = io.WriteString(f, "#else // USE_LIBSQLITE3\n // If users really want to link against the system sqlite3 we\n// need to make this file a noop.\n #endif") |
| 208 |
if err != nil { |
| 209 |
zr.Close() |
| 210 |
f.Close() |
| 211 |
log.Fatal(err) |
| 212 |
} |
| 213 |
zr.Close() |
| 214 |
if err := f.Close(); err != nil { |
| 215 |
log.Fatal(err) |
| 216 |
} |
| 217 |
fmt.Printf("Extracted: %v\n", filepath.Base(f.Name())) |
| 218 |
} |
| 219 |
|
| 220 |
//Extract Source |
| 221 |
//for _, zf := range rSource.File { |
| 222 |
// var f *os.File |
| 223 |
// switch path.Base(zf.Name) { |
| 224 |
// case "userauth.c": |
| 225 |
// f, err = os.Create("../userauth.c") |
| 226 |
// case "sqlite3userauth.h": |
| 227 |
// f, err = os.Create("../userauth.h") |
| 228 |
// default: |
| 229 |
// continue |
| 230 |
// } |
| 231 |
// if err != nil { |
| 232 |
// log.Fatal(err) |
| 233 |
// } |
| 234 |
// zr, err := zf.Open() |
| 235 |
// if err != nil { |
| 236 |
// log.Fatal(err) |
| 237 |
// } |
| 238 |
|
| 239 |
// _, err = io.Copy(f, zr) |
| 240 |
// if err != nil { |
| 241 |
// log.Fatal(err) |
| 242 |
// } |
| 243 |
|
| 244 |
// zr.Close() |
| 245 |
// f.Close() |
| 246 |
// fmt.Printf("extracted %v\n", filepath.Base(f.Name())) |
| 247 |
//} |
| 248 |
|
| 249 |
// Merge SQLite User Authentication into amalgamation |
| 250 |
//if err := mergeFile("../userauth.c", "../sqlite3-binding.c"); err != nil { |
| 251 |
// log.Fatal(err) |
| 252 |
//} |
| 253 |
//if err := mergeFile("../userauth.h", "../sqlite3-binding.h"); err != nil { |
| 254 |
// log.Fatal(err) |
| 255 |
//} |
| 256 |
|
| 257 |
os.Exit(0) |
| 258 |
} |