Skip to content

Instantly share code, notes, and snippets.

View harrisonturton's full-sized avatar
🏠
Working from home

Harry Turton harrisonturton

🏠
Working from home
  • Canva
  • Sydney, Australia
View GitHub Profile
@harrisonturton
harrisonturton / auto_build.sh
Last active September 16, 2018 12:07
Script to autobuild COMP2310 Assignment 1. It automatically detects your OS, and allows you to easily set the build mode from the commandline.
#!/bin/bash
# Harrison Turton 2018
# --------------------------------------
# This script will automatically detect your
# operating system, and build accordingly.
#
# A build mode flag can be included, wherein
# the script will supply the relevant build
# mode flags to gnatmake. Defaults to
@harrisonturton
harrisonturton / queue.go
Last active August 21, 2024 10:04
RabbitMQ client that automatically reconnects when the connection fails, and has a confirmed push method (i.e. the server is guaranteed to recieve the message)
package main
import (
"errors"
"github.com/streadway/amqp"
"log"
"os"
"time"
)
@harrisonturton
harrisonturton / database.rs
Last active June 2, 2022 03:31
Generic, async database trait for tokio-postgres
#![feature(generic_associated_types)]
use anyhow::Error;
use async_trait::async_trait;
use tokio_postgres::{row::Row, types::ToSql};
/// This is an example of how to abstract Rust database libraries (e.g. tokio-postgres)
/// behind a trait. This is useful to swap out a concrete implementation with a fake,
/// or for changing the underlying database implementation (e.g. switching to sqlx)
/// without needing to update the consumers of this module.
@harrisonturton
harrisonturton / tuntap.rs
Last active June 14, 2023 23:01
Persistent TUN/TAP device in Rust
use nix::errno::Errno;
use nix::fcntl::{open, OFlag};
use nix::ioctl_write_int;
use nix::libc::{__c_anonymous_ifr_ifru, c_char, c_short, ifreq, IFNAMSIZ};
use nix::sys::stat::Mode;
use nix::unistd::close;
use std::ffi::{CString, NulError};
use std::os::fd::RawFd;
use std::ptr::copy_nonoverlapping;
@harrisonturton
harrisonturton / kvm.c
Created July 17, 2023 13:45
Using the KVM API (LWN, 2015)
/*
* This follows the LWN tutorial "Using the KVM API":
*
* https://lwn.net/Articles/658511/
*
* Which runs a virtual machine that executes the following
* 16-bit x86 code (the assembled binary is hardcoded in):
*
* mov $0x3f8, %dx
* add %bl, %al
@harrisonturton
harrisonturton / myfs.c
Last active July 17, 2023 13:51
Creating Linux virtual filesystems (LWN, 2003)
/*
* Author: Harrison Turton <[email protected]>
*
* This follows the LWN article "Creating Linux virtual filesystems":
*
* https://lwn.net/Articles/57369/
*
* However, this was published in 2003. The VFS has changed quite a
* bit since then, and so the code had to be tweaked to work. So
* isn't a byte-for-byte copy, I had to go digging through the kernel
@harrisonturton
harrisonturton / Makefile
Last active January 29, 2024 11:30
Using libjxl to transcode a JPEG file into a JPEG-XL file
COPTS=-std=c++17 -Wall
INCLUDE=-I/usr/local/include/jxl/ -L/usr/local/lib/
LIBS=-ljxl -ljxl_threads
BIN=main
$(BIN): main.cc
clang++ $(COPTS) $(INCLUDE) $(LIBS) -o $@ $<
.PHONY: clean
clean:
@harrisonturton
harrisonturton / io_uring_readv.c
Last active May 27, 2024 01:48
Read a file with io_uring
#include <errno.h>
#include <fcntl.h>
#include <linux/io_uring.h>
#include <signal.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <sys/syscall.h>