Fix for segfault

cron
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

@ -9,7 +9,7 @@ struct Container {
}
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,16 +5,19 @@ 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) ?
mut s := unix.connect_stream(docker.socket) ?
defer {
s.close() ?
// This or is required because otherwise, the V compiler segfaults for
// some reason
s.close() or {}
}
// Write the request to the socket
@ -23,18 +26,16 @@ fn request(method string, url urllib.URL) ?http.Response {
// Wait for the server to respond
s.wait_for_write() ?
mut buf := []byte{len: buf_len}
mut buf := []byte{len: docker.buf_len}
mut res := []byte{}
mut c := 0
for {
c = s.read(mut buf) or {
return error('Failed to read data from socket.')
}
c = s.read(mut buf) or { return error('Failed to read data from socket.') }
res << buf[..c]
if c < buf_len {
if c < docker.buf_len {
break
}
}