Merge the CLI and Daemon portable interface

This commit is contained in:
Andrea Mazzoleni 2026-06-24 00:15:15 +02:00
parent 3302518566
commit 3ce5a5b4e5
46 changed files with 4110 additions and 2042 deletions

View file

@ -1,7 +1,7 @@
// SPDX-License-Identifier: GPL-3.0-or-later
// Copyright (C) 2011 Andrea Mazzoleni
#include "portable.h"
#include "os/portable.h"
#ifdef __MINGW32__ /* Only for MingW */
@ -22,7 +22,7 @@ int exit_sync_needed = 2;
volatile int global_interrupt = 0;
int app_global_interrupt(void)
int os_signal_interrupt(void)
{
return global_interrupt;
}
@ -33,6 +33,598 @@ void app_signal_handler(int signum)
global_interrupt = signum;
}
/****************************************************************************/
/* fs */
int fsinfo(const char* path, int* has_persistent_inode, int* has_syncronized_hardlinks, uint64_t* total_space, uint64_t* free_space, char* fstype, size_t fstype_size, char* fslabel, size_t fslabel_size)
{
wchar_t conv_buf[CONV_MAX];
/* all FAT/exFAT/NTFS when managed from Windows have persistent inodes */
if (has_persistent_inode)
*has_persistent_inode = 1;
/* NTFS doesn't synchronize hardlinks metadata */
if (has_syncronized_hardlinks)
*has_syncronized_hardlinks = 0;
if (free_space || total_space) {
ULARGE_INTEGER total_bytes;
ULARGE_INTEGER total_free_bytes;
DWORD attr;
char dir[PATH_MAX];
if (strlen(path) + 1 > sizeof(dir)) {
windows_errno(ERROR_BUFFER_OVERFLOW);
return -1;
}
strcpy(dir, path);
/* get the file attributes */
attr = GetFileAttributesW(convert(conv_buf, dir));
if (attr == INVALID_FILE_ATTRIBUTES) {
DWORD error = GetLastError();
if (error != ERROR_FILE_NOT_FOUND) {
windows_errno(error);
return -1;
}
/*
* If it doesn't exist, we assume a file
* and we check for the containing dir
*/
attr = 0;
}
/* if it's not a directory, truncate the file name */
if ((attr & FILE_ATTRIBUTE_DIRECTORY) == 0) {
char* slash = strrchr(dir, '/');
/**
* Cut the file name, but leave the last slash.
*
* This is done because a MSDN comment about using of UNC paths.
*
* MSDN 'GetDiskFreeSpaceEx function'
* http://msdn.microsoft.com/en-us/library/windows/desktop/aa364937%28v=vs.85%29.aspx
* If this parameter is a UNC name, it must include a trailing backslash,
* for example, "\\MyServer\MyShare\".
*/
if (slash)
slash[1] = 0;
}
/*
* Get the free space of the directory
* note that it must be a directory
*/
if (!GetDiskFreeSpaceExW(convert(conv_buf, dir), 0, &total_bytes, &total_free_bytes)) {
windows_errno(GetLastError());
return -1;
}
if (total_space)
*total_space = total_bytes.QuadPart;
if (free_space)
*free_space = total_free_bytes.QuadPart;
}
if (fstype && fslabel) {
wchar_t volume_root[PATH_MAX];
wchar_t fs_name[PATH_MAX];
wchar_t vol_name[PATH_MAX];
fstype[0] = 0;
fslabel[0] = 0;
if (GetVolumePathNameW(convert(conv_buf, path), volume_root, PATH_MAX)) {
if (GetVolumeInformationW(volume_root, vol_name, PATH_MAX, 0, 0, 0, fs_name, PATH_MAX)) {
char u8[CONV_MAX];
size_t len;
char* ret;
ret = u16tou8_mayfail(u8, sizeof(u8), fs_name, wcslen(fs_name), &len);
if (ret != 0 && len + 1 <= fstype_size) {
memcpy(fstype, u8, len);
fstype[len] = 0;
}
ret = u16tou8_mayfail(u8, sizeof(u8), vol_name, wcslen(vol_name), &len);
if (ret != 0 && len + 1 <= fslabel_size) {
memcpy(fslabel, u8, len);
fslabel[len] = 0;
}
}
}
}
return 0;
}
/****************************************************************************/
/* snapshot */
size_t windows_direct_size(void)
{
SYSTEM_INFO si;
GetSystemInfo(&si);
/*
* MSDN 'File Buffering'
* https://msdn.microsoft.com/en-us/library/windows/desktop/cc644950%28v=vs.85%29.aspx
*
* "Therefore, in most situations, page-aligned memory will also be sector-aligned,"
* "because the case where the sector size is larger than the page size is rare."
*/
return si.dwPageSize;
}
#define WINDOWS_NTFS_MAGIC 0x5346544E /* 'N','T','F','S' */
#define PS_CMD_MAX 4096
#define SNAPSHOT_GUID ".guid"
/*
* PowerShell helper
*
* Runs a PowerShell one-liner via _popen() and captures the first line
* of stdout into `out` as UTF-8.
* `out` may be NULL when output is not needed.
*
* The caller builds `command` as a string using snprintf().
* Any embedded path literals must use single quotes so they survive
* the outer double-quote wrapping passed to cmd.exe.
*
* Returns 0 on success, -1 on failure.
*/
static int windows_ps(const char* ps_command, char* out, size_t out_size)
{
char cmd[PS_CMD_MAX];
FILE* fp;
int ret;
ret = snprintf(cmd, sizeof(cmd), "powershell.exe -NoProfile -NonInteractive -Command \"%s\" 2>nul", ps_command);
if (ret < 0 || ret >= (int)sizeof(cmd)) {
errno = EINVAL;
return -1;
}
fp = _popen(cmd, "r");
if (!fp) {
windows_errno(GetLastError());
log_error(errno, "Failed to run PowerShell command '%s' (from popen).\n", cmd);
return -1;
}
if (out && out_size > 0) {
/* get the first line */
if (!fgets(out, (int)out_size, fp)) {
out[0] = 0;
} else {
/* trim spaces and newlines */
strtrim(out);
}
}
ret = _pclose(fp);
if (ret == -1) {
errno = EINVAL;
log_error(errno, "Failed to run PowerShell command '%s' (from pclose).\n", cmd);
return -1;
}
if (ret != 0) {
errno = EINVAL;
log_error(errno, "PowerShell command '%s' failed with '%d' (from pclose).\n", cmd, ret);
return -1;
}
return 0;
}
/**
* Validates a GUID string of the form:
* xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
* Does NOT accept braced forms like {xxxxxxxx-...}.
*
* Returns true if valid, false otherwise.
*/
int windows_guid_is_valid(const char* guid, int len)
{
if (!guid || len != 38)
return -1;
if (guid[0] != '{' || guid[37] != '}')
return -1;
const char* inner = guid + 1;
for (int i = 0; i < 36; ++i) {
if (i == 8 || i == 13 || i == 18 || i == 23) {
if (inner[i] != '-')
return -1;
} else {
char c = inner[i];
int is_digit = (c >= '0' && c <= '9');
int is_upper_hex = (c >= 'A' && c <= 'F');
int is_lower_hex = (c >= 'a' && c <= 'f');
if (!is_digit && !is_upper_hex && !is_lower_hex)
return -1;
}
}
return 0;
}
static int windows_read_guid(const char* guid_link, char* guid, size_t guid_size)
{
int len = windows_readlink(guid_link, guid, guid_size);
if (len < 0 && errno == ENOENT) {
guid[0] = 0;
return 0;
}
if (len < 0) {
log_error(errno, "Error readlink '%s'. %s.\n", guid_link, strerror(errno));
return -1;
}
if ((size_t)len >= guid_size) {
errno = ENAMETOOLONG;
log_error(errno, "GUID too long in '%s'.\n", guid_link);
return -1;
}
guid[len] = 0;
/* validate that the GUID contains only safe characters: [A-Za-z0-9-{}] */
if (windows_guid_is_valid(guid, len) != 0)
return -1;
return 0;
}
static int windows_rebuild_link(const struct fssnapshot_struct* fss, const char* name)
{
char cmd[PS_CMD_MAX];
char device_path[PATH_MAX];
char target_path[PATH_MAX];
char link_path[PATH_MAX];
char guid_link[PATH_MAX];
char guid[PATH_MAX];
pathcpy(guid_link, sizeof(guid_link), fss->snapshot_dir);
pathcat(guid_link, sizeof(guid_link), name);
pathcat(guid_link, sizeof(guid_link), SNAPSHOT_GUID);
pathcpy(link_path, sizeof(link_path), fss->snapshot_dir);
pathcat(link_path, sizeof(link_path), name);
int ret = windows_read_guid(guid_link, guid, sizeof(guid));
if (ret != 0) {
return -1;
}
/* if no GUID, assume it absent */
if (guid[0] == 0) {
/* remove stale links */
windows_rmdir(link_path);
return 0;
}
snprintf(cmd, sizeof(cmd),
"(Get-WmiObject Win32_ShadowCopy | Where-Object {$_.ID -eq '%s'}).DeviceObject",
guid);
if (windows_ps(cmd, device_path, sizeof(device_path)) != 0) {
log_error(errno, "Error getting DeviceObject from GUID '%s'. %s.\n", guid, strerror(errno));
return -1;
}
/* if no device path, assume snapshot absent */
if (device_path[0] == 0) {
/* remove stale links */
windows_rmdir(guid_link);
windows_rmdir(link_path);
return 0;
}
pathimport(target_path, sizeof(target_path), device_path);
pathslash(target_path, sizeof(target_path));
/* remove stale link if present */
windows_rmdir(link_path);
if (windows_symlink_directory(target_path, link_path) != 0) {
log_error(errno, "Error creating symlink '%s'. %s.\n", link_path, strerror(errno));
return -1;
}
return 0;
}
static int windows_delete_link(const struct fssnapshot_struct* fss, const char* name)
{
char link_path[PATH_MAX];
pathcpy(link_path, sizeof(link_path), fss->snapshot_dir);
pathcat(link_path, sizeof(link_path), name);
/* remove link if present */
windows_rmdir(link_path);
return 0;
}
int fssnapshot_mount(const char* dir, struct fssnapshot_struct* fss)
{
wchar_t conv_buf_vol[CONV_MAX];
char conv_buf_root[CONV_MAX];
wchar_t volume_root[PATH_MAX];
wchar_t volume_name[PATH_MAX];
wchar_t fs_name[32];
uint32_t magic;
/*
* GetVolumePathNameW() accepts any path: file, directory, or deep
* subdirectory, and resolves it to the volume root (e.g. L"C:\").
* The result always carries a trailing backslash.
*
* Use convert_if_required() to avoid the automatic addition of \\?\
* made by convert() that is propagated in the resulting volume_root
*/
if (!GetVolumePathNameW(convert_if_required(conv_buf_vol, dir), volume_root, PATH_MAX)) {
windows_errno(GetLastError());
log_error(errno, "Error getting VolumeRoot from '%s'. %s.\n", dir, strerror(errno));
return -1;
}
if (!GetVolumeInformationW(volume_root, 0, 0, 0, 0, 0, fs_name, 32)) {
windows_errno(GetLastError());
log_error(errno, "Error getting information of VolumeRoot '%s'. %s.\n", u16tou8(conv_buf_root, volume_root), strerror(errno));
return -1;
}
if (wcscmp(fs_name, L"NTFS") == 0)
magic = WINDOWS_NTFS_MAGIC;
else
return -1; /* support only NTFS */
/*
* Obtain the canonical volume name: \\?\Volume{GUID}\
*
* GetVolumePathNameW() may return a drive letter path ("C:\") or a
* directory mount point ("D:\Mount\"). Neither is guaranteed to be
* accepted by Win32_ShadowCopy.Create() on volumes without a drive
* letter. GetVolumeNameForVolumeMountPointW() always returns the
* stable kernel-form GUID path that VSS requires, regardless of how
* many (or how few) mount points the volume has.
*
* We store it in fss->dataset.
*/
if (!GetVolumeNameForVolumeMountPointW(volume_root, volume_name, PATH_MAX)) {
windows_errno(GetLastError());
log_error(errno, "Error getting VolumeName of VolumeRoot '%s'. %s.\n", u16tou8(conv_buf_root, volume_root), strerror(errno));
return -1;
}
/* don't use pathimport for dataset to keep backslashes */
pathcpy(fss->dataset, sizeof(fss->dataset), u16tou8(conv_buf_root, volume_name));
/* use pathimport to convert backslashes to slashes */
pathimport(fss->root_dir, sizeof(fss->root_dir), u16tou8(conv_buf_root, volume_root));
/* the returned root_dir should match the start of the passed dir */
if (pathncmp(fss->root_dir, dir, strlen(fss->root_dir)) != 0) {
errno = EINVAL;
log_error(errno, "Not matching VolumeRoot '%s' for '%s'.\n", fss->root_dir, dir);
return -1;
}
pathcpy(fss->snapshot_dir, sizeof(fss->snapshot_dir), fss->root_dir);
pathcat(fss->snapshot_dir, sizeof(fss->snapshot_dir), SNAPSHOT_CONTAINER "/");
if (windows_mkdir(fss->snapshot_dir) != 0 && errno != EEXIST) {
log_error(errno, "Error creating directory '%s'. %s.\n", fss->snapshot_dir, strerror(errno));
return -1;
}
/* refresh the links, after reboots they gets invalidated */
if (windows_rebuild_link(fss, SNAPSHOT_PENDING) != 0) {
return -1;
}
if (windows_rebuild_link(fss, SNAPSHOT_STABLE) != 0) {
return -1;
}
fss->magic = magic;
return 0;
}
int fssnapshot_stat(struct fssnapshot_struct* fss, const char* name, struct stat* st)
{
char link_path[PATH_MAX];
pathcpy(link_path, sizeof(link_path), fss->snapshot_dir);
pathcat(link_path, sizeof(link_path), name);
pathcat(link_path, sizeof(link_path), SNAPSHOT_GUID); /* we check the GUID as it's the one that matter */
/* use lstat because we want to check the link */
if (windows_lstat(link_path, st) != 0)
return -1;
/* it must be a link to a directory */
if (!S_ISLNKDIR(st->st_mode))
return -1;
return 0;
}
int fssnapshot_create(const struct fssnapshot_struct* fss, const char* name)
{
char cmd[PS_CMD_MAX];
char device_path[PATH_MAX];
char target_path[PATH_MAX];
char link_path[PATH_MAX];
char guid_link[PATH_MAX];
char out[PATH_MAX];
/* create VSS shadow copy and capture the system-assigned GUID */
snprintf(cmd, sizeof(cmd),
"$r = (Get-WmiObject -List Win32_ShadowCopy).Create('%s', 'ClientAccessible'); "
"if ($r.ReturnValue -eq 0) { "
"Write-Output ('ID_' + $r.ShadowID); "
"} else { "
"Write-Output ('0x{0:X8}' -f $r.ReturnValue); "
"}",
fss->dataset);
if (windows_ps(cmd, out, sizeof(out)) != 0) {
log_error(errno, "Error creating snapshot of VolumeName '%s'. %s.\n", fss->dataset, strerror(errno));
return -1;
}
if (strncmp(out, "ID_", 3) != 0) {
errno = ENODATA;
log_error(errno, "VSS snapshot creation of '%s' failed with error %s.\n", fss->dataset, out);
return -1;
}
const char* guid = out + 3;
/* resolve the shadow copy's device object path */
snprintf(cmd, sizeof(cmd), "(Get-WmiObject Win32_ShadowCopy | Where-Object {$_.ID -eq '%s'}).DeviceObject", guid);
if (windows_ps(cmd, device_path, sizeof(device_path)) != 0) {
log_error(errno, "Error getting the snapshot DeviceObject from GUID '%s'. %s.\n", guid, strerror(errno));
goto bail_and_delete;
}
if (device_path[0] == 0) {
errno = ENODATA;
log_error(errno, "Empty snapshot DeviceObject of GUID '%s'. %s.\n", guid, strerror(errno));
goto bail_and_delete;
}
/* use pathimport to convert backslashes to slashes */
pathimport(target_path, sizeof(target_path), device_path);
/* the ending slash is required, otherwise the link won't work */
pathslash(target_path, sizeof(target_path));
pathcpy(link_path, sizeof(link_path), fss->snapshot_dir);
pathcat(link_path, sizeof(link_path), name);
/* remove potential stale link */
windows_rmdir(link_path);
int ret = windows_symlink_directory(target_path, link_path);
if (ret != 0) {
log_error(errno, "Error creating symlink '%s'. %s.\n", link_path, strerror(errno));
goto bail_and_delete;
}
pathcpy(guid_link, sizeof(guid_link), fss->snapshot_dir);
pathcat(guid_link, sizeof(guid_link), name);
pathcat(guid_link, sizeof(guid_link), SNAPSHOT_GUID);
/* store GUID persistently as symlink target */
ret = windows_symlink_directory(guid, guid_link);
if (ret != 0) {
log_error(errno, "Error creating GUID symlink '%s'. %s.\n", guid_link, strerror(errno));
windows_rmdir(link_path);
goto bail_and_delete;
}
return 0;
bail_and_delete:
/* destroy the shadow copy we just created */
snprintf(cmd, sizeof(cmd), "Get-WmiObject Win32_ShadowCopy | Where-Object {$_.ID -eq '%s'} | Remove-WmiObject", guid);
windows_ps(cmd, 0, 0);
return -1;
}
int fssnapshot_delete(const struct fssnapshot_struct* fss, const char* name)
{
char guid[PATH_MAX];
char cmd[PS_CMD_MAX];
char guid_link[PATH_MAX];
pathcpy(guid_link, sizeof(guid_link), fss->snapshot_dir);
pathcat(guid_link, sizeof(guid_link), name);
pathcat(guid_link, sizeof(guid_link), SNAPSHOT_GUID);
if (windows_read_guid(guid_link, guid, sizeof(guid)) != 0)
return -1;
/* if the GUID link is gone, assume everything is gone */
if (guid[0] == 0) {
/* remove as not valid anymore */
windows_delete_link(fss, name);
return 0;
}
/* destroy the VSS shadow copy */
snprintf(cmd, sizeof(cmd), "Get-WmiObject Win32_ShadowCopy | Where-Object {$_.ID -eq '%s'} | Remove-WmiObject", guid);
int ret = windows_ps(cmd, 0, 0);
if (ret != 0) {
log_error(errno, "Error destroy the snapshot from GUID '%s'. %s.\n", guid, strerror(errno));
/* remove as not valid anymore */
windows_delete_link(fss, name);
return -1;
}
/* remove live symlink if present */
windows_delete_link(fss, name);
/* remove persistent GUID symlink */
if (windows_rmdir(guid_link) != 0) {
log_error(errno, "Error rmdir '%s'. %s.\n", guid_link, strerror(errno));
return -1;
}
return 0;
}
int fssnapshot_rename(const struct fssnapshot_struct* fss, const char* old_name, const char* new_name)
{
char old_guid[PATH_MAX];
char new_guid[PATH_MAX];
pathcpy(old_guid, sizeof(old_guid), fss->snapshot_dir);
pathcat(old_guid, sizeof(old_guid), old_name);
pathcat(old_guid, sizeof(old_guid), SNAPSHOT_GUID);
pathcpy(new_guid, sizeof(new_guid), fss->snapshot_dir);
pathcat(new_guid, sizeof(new_guid), new_name);
pathcat(new_guid, sizeof(new_guid), SNAPSHOT_GUID);
if (windows_rename(old_guid, new_guid) != 0) {
log_error(errno, "Error renaming '%s' to '%s'. %s.\n", old_guid, new_guid, strerror(errno));
return -1;
}
/* the rename is the latest operation, and then we don't care about the links, we just remove both */
windows_delete_link(fss, old_name);
windows_delete_link(fss, new_name);
return 0;
}
void fssnapshot_unmount(const struct fssnapshot_struct* fss)
{
/*
* Remove symlinks on exit to prevent "Dangling Links."
* Since \Device\HarddiskVolumeShadowCopyN paths are reassigned by the kernel
* at boot, a stale link would either point to a non-existent device or,
* worse, a different disk's snapshot.
*
* Deleting the link ensures system hygiene and prevents users from seeing
* "Location not available" errors or incorrect data after a reboot.
*/
windows_delete_link(fss, SNAPSHOT_PENDING);
windows_delete_link(fss, SNAPSHOT_STABLE);
}
/****************************************************************************/
/* resolve */
@ -425,15 +1017,15 @@ static void devattr_property(HANDLE h, char* serial, char* model, char* inter)
case BusTypeiScsi : type = "iSCSI"; break;
case BusTypeSas : type = "SAS"; break;
case BusTypeSata : type = "SATA"; break;
case BusTypeSd : type = "SD"; break;
case BusTypeMmc : type = "MMC"; break;
case BusTypeSd : type = "SD"; break; /* Secure Digital cards */
case BusTypeMmc : type = "MMC"; break; /* NAND Embedded MultiMediaCards (eMMC) */
case BusTypeVirtual : type = "Virtual"; break;
case BusTypeFileBackedVirtual : type = "Virtual"; break;
case BusTypeSpaces : type = "Storage Spaces"; break;
case BusTypeNvme : type = "NVMe"; break;
case BusTypeSCM : type = "SCM"; break;
case BusTypeUfs : type = "UFS"; break;
case BusTypeNvmeof : type = "NVMe"; break;
case BusTypeNvme : type = "NVMe"; break; /* NAND */
case BusTypeSCM : type = "SCM"; break; /* NAND Storage Class Memory */
case BusTypeUfs : type = "UFS"; break; /* NAND Universal Flash Storage */
case BusTypeNvmeof : type = "NVMe"; break; /* NAND NVMe over Fabrics */
default :
}
if (inter && type) /* always override interface as more detailed than smartctl */