Fix for segfault

main
Jef Roosens 2022-02-19 21:41:26 +01:00
parent 6c435c2a1e
commit 57c4af0aaf
Signed by untrusted user: Jef Roosens
GPG Key ID: B580B976584B5F30
4 changed files with 39 additions and 38 deletions

View File

@ -3,5 +3,5 @@ module main
import docker
fn build() {
println(docker.containers() or { panic("yeet") })
println(docker.containers() or { panic('yeet') })
}

View File

@ -4,12 +4,12 @@ import json
import net.urllib
struct Container {
id string
names []string
id string
names []string
}
pub fn containers() ?[]Container {
res := docker.get(urllib.parse('/containers/json') ?) ?
res := get(urllib.parse('/containers/json') ?) ?
return json.decode([]Container, res.text)
return json.decode([]Container, res.text) or {}
}

View File

@ -5,44 +5,45 @@ import net.urllib
import net.http
const socket = '/var/run/docker.sock'
const buf_len = 1024
fn request(method string, url urllib.URL) ?http.Response {
req := "$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n"
req := '$method $url.request_uri() HTTP/1.1\nHost: localhost\n\n'
// Open a connection to the socket
mut s := unix.connect_stream(socket) ?
// Open a connection to the socket
mut s := unix.connect_stream(docker.socket) ?
defer {
s.close() ?
}
defer {
// This or is required because otherwise, the V compiler segfaults for
// some reason
s.close() or {}
}
// Write the request to the socket
s.write_string(req) ?
// Write the request to the socket
s.write_string(req) ?
// Wait for the server to respond
s.wait_for_write() ?
// Wait for the server to respond
s.wait_for_write() ?
mut buf := []byte{len: buf_len}
mut res := []byte{}
mut buf := []byte{len: docker.buf_len}
mut res := []byte{}
mut c := 0
mut c := 0
for {
c = s.read(mut buf) or {
return error('Failed to read data from socket.')
}
res << buf[..c]
for {
c = s.read(mut buf) or { return error('Failed to read data from socket.') }
res << buf[..c]
if c < buf_len {
break
}
}
if c < docker.buf_len {
break
}
}
// Decode chunked response
return http.parse_response(res.bytestr())
// Decode chunked response
return http.parse_response(res.bytestr())
}
fn get(url urllib.URL) ?http.Response {
return request('GET', url)
return request('GET', url)
}

View File

@ -53,13 +53,13 @@ fn reader_to_file(mut reader io.BufferedReader, length int, path string) ? {
}
fn main() {
if os.args.len == 1 {
exit_with_message(1, 'No action provided.')
}
if os.args.len == 1 {
exit_with_message(1, 'No action provided.')
}
match os.args[1] {
'server' { server() }
'build' { build() }
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
}
match os.args[1] {
'server' { server() }
'build' { build() }
else { exit_with_message(1, 'Unknown action: ${os.args[1]}') }
}
}