Skip to content

Instantly share code, notes, and snippets.

View leiless's full-sized avatar
🎯
Focusing

Fishbone° leiless

🎯
Focusing
  • Shanghai, China
  • 05:07 (UTC +08:00)
View GitHub Profile
package main
import (
"encoding/json"
"fmt"
"io"
"net/http"
"time"
)
@leiless
leiless / lmdb_MDB_DUPSORT_write.rs
Created October 30, 2023 02:09
Test LMDB MDB_DUPSORT write with same key-value.
use lmdb::{Cursor, Transaction};
fn test_lmdb_dup_sort_update_in_place() -> anyhow::Result<()> {
let db_dir = "lmdb-dir";
if let Err(err) = std::fs::remove_dir_all(db_dir) {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err.try_into()?);
}
}
@leiless
leiless / lmdb_INTEGER_KEY_cursor_get_bug.rs
Created October 12, 2023 07:17
LMDB INTEGER_KEY big key `mdb_cursor_get(MDB_SET_RANGE)` bug
use lmdb::{Cursor, Transaction};
fn test_lmdb_dup_sort_update_in_place() -> anyhow::Result<()> {
let db_dir = "lmdb-dir";
if let Err(err) = std::fs::remove_dir_all(db_dir) {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err.try_into()?);
}
}
@leiless
leiless / test_lmdb_dup_sort_update_in_place.rs
Last active October 10, 2023 06:50
LMDB `DUP_SORT` `cursor.put(CURRENT)` different value size bug
use lmdb::{Cursor, Transaction};
fn test_lmdb_dup_sort_update_in_place() -> anyhow::Result<()> {
let db_dir = "lmdb-dir";
if let Err(err) = std::fs::remove_dir_all(db_dir) {
if err.kind() != std::io::ErrorKind::NotFound {
return Err(err.try_into()?);
}
}
@leiless
leiless / server.c
Last active September 18, 2023 08:12
Windows AF_UNIX UNIX domain socket example
// Taken from with modifications
// https://devblogs.microsoft.com/commandline/windowswsl-interop-with-af_unix/#windows-server-code
#undef UNICODE
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <afunix.h>
#include <stdlib.h>
@leiless
leiless / std_deviation.rs
Created August 31, 2023 12:33
Standard deviation in Rust
fn mean(data: &[u32]) -> f64 {
let sum = data.iter().sum::<u32>() as f64;
let count = data.len();
sum / count as f64
}
fn std_deviation(data: &[u32]) -> f64 {
if data.len() != 0 {
let data_mean = mean(data);
let variance = data.iter().map(|v| {
@leiless
leiless / windows-FILETIME-to-unix-time-epoch.rs
Created August 30, 2023 12:49
Windows FILETIME to UNIX time epoch.
use std::os::windows::fs::MetadataExt;
// Nanoseconds between [Jan 1 1601, Jan 1 1970]
const UNIX_EPOCH_OFFSET: u64 = 0x019d_b1de_d53e_8000;
// Panic if file_time is below Jan 1 1970
fn filetime_to_unix_ns(file_time: u64) -> u64 {
(file_time - UNIX_EPOCH_OFFSET) * 100
}
@leiless
leiless / win_sh_get_localized_name.rs
Last active August 29, 2023 08:59
Rust: windows-rs: SHGetLocalizedName() example
const MAX_PATH_LEN: usize = 2048;
#[allow(overflowing_literals)]
const ERR_MOD_NOT_FOUND: windows::core::HRESULT = windows::core::HRESULT(0x8007007Ei32);
pub fn win_sh_get_localized_name(path: &str) -> anyhow::Result<String> {
let path_hstr = windows::core::HSTRING::from(path);
let mut res_path = Vec::with_capacity(MAX_PATH_LEN);
unsafe { res_path.set_len(res_path.capacity()); }
let mut res_id = 0i32;
@leiless
leiless / get-current-logged-in-users.rs
Last active August 25, 2023 09:00
Rust + windows-rs: Get current logged-in users
const WTS_CONNECT_STATE_ACTIVE: i32 = 0;
// https://learn.microsoft.com/en-us/windows/win32/api/wtsapi32/ne-wtsapi32-wts_connectstate_class#syntax
fn wts_state_to_string(state: windows::Win32::System::RemoteDesktop::WTS_CONNECTSTATE_CLASS) -> String {
match state.0 {
0 => "Active".to_string(),
1 => "Connected".to_string(),
2 => "ConnectQuery".to_string(),
3 => "Shadow".to_string(),
4 => "Disconnected".to_string(),
@leiless
leiless / rust-resolve-lnk-file.rs
Last active August 30, 2023 03:27
Rust: how to resolve target location of a `.lnk` file
use windows::core::ComInterface;
// https://learn.microsoft.com/en-us/windows/win32/shell/links#resolving-a-shortcut
fn resolve_lnk_target(lnk_file: &str) -> Result<String, Box<dyn std::error::Error>> {
let psl: windows::Win32::UI::Shell::IShellLinkW = unsafe {
windows::Win32::System::Com::CoInitialize(None)?;
windows::Win32::System::Com::CoCreateInstance(
&windows::Win32::UI::Shell::ShellLink,
None,