sdformat: init at 0.2.0

This commit is contained in:
Thiago Kenji Okada
2026-04-12 20:35:10 +01:00
parent 539624fb3a
commit f7afb1dc30
2 changed files with 88 additions and 0 deletions
+49
View File
@@ -0,0 +1,49 @@
{
lib,
stdenv,
fetchFromGitHub,
util-linuxMinimal,
installShellFiles,
}:
stdenv.mkDerivation (finalAttrs: {
pname = "sdformat";
version = "0.2.0";
__structuredAttrs = true;
src = fetchFromGitHub {
owner = "profi200";
repo = "sdFormatLinux";
rev = "v${finalAttrs.version}";
hash = "sha256-AoAhP1dr+hQSnOpZC0oHt0j3fUVNVhD+3jWm6iMfskk=";
};
nativeBuildInputs = [ installShellFiles ];
patches = [ ./remove-hardcoded-lsblk-path.diff ];
strictDeps = true;
makeFlags = [
"TARGET=${finalAttrs.pname}"
"LSBLK_PATH=${lib.getExe' util-linuxMinimal "lsblk"}"
];
installPhase = ''
runHook preInstall
installBin ${finalAttrs.pname}
runHook postInstall
'';
meta = {
description = "Format your SD card the way the SD Association intended";
homepage = "https://github.com/profi200/sdFormatLinux";
license = lib.licenses.mit;
mainProgram = finalAttrs.pname;
maintainers = with lib.maintainers; [ thiagokokada ];
platforms = lib.platforms.linux;
};
})
@@ -0,0 +1,39 @@
diff --git i/Makefile w/Makefile
index c45b32b..258189c 100644
--- i/Makefile
+++ w/Makefile
@@ -5,7 +5,8 @@ TARGET := $(notdir $(CURDIR))
BUILD := build
INCLUDES := include
SOURCES := source
-DEFINES := -D_FORTIFY_SOURCE=2
+LSBLK_PATH ?= /usr/bin/lsblk
+DEFINES := -D_FORTIFY_SOURCE=2 -DLSBLK_PATH=\"$(LSBLK_PATH)\"
# Compiler settings
diff --git i/source/blockdev.cpp w/source/blockdev.cpp
index a179604..b4de5ef 100644
--- i/source/blockdev.cpp
+++ w/source/blockdev.cpp
@@ -7,6 +7,7 @@
#include <errno.h>
#include <fcntl.h> // open()...
#include <linux/fs.h> // BLKGETSIZE64...
+#include <limits.h> // PATH_MAX
#include <sys/ioctl.h> // ioctl()...
#include <sys/stat.h> // S_IRUSR, S_IWUSR...
#include <unistd.h> // write(), close()...
@@ -21,8 +22,10 @@
static int checkDevice(const char *const path)
{
int res = EINVAL; // By default assume the given path is not a suitable device.
- char cmd[64] = "/usr/bin/lsblk -dnr -oTYPE,HOTPLUG,PHY-SEC ";
- strncpy(&cmd[43], path, sizeof(cmd) - 43);
+ const char cmdPrefix[] = LSBLK_PATH " -dnr -oTYPE,HOTPLUG,PHY-SEC ";
+ char cmd[sizeof(cmdPrefix) + PATH_MAX];
+ memcpy(cmd, cmdPrefix, sizeof(cmdPrefix) - 1);
+ strncpy(&cmd[sizeof(cmdPrefix) - 1], path, sizeof(cmd) - sizeof(cmdPrefix));
cmd[sizeof(cmd) - 1] = '\0';
FILE *const p = ::popen(cmd, "r");
if(p == nullptr)