McdeSys: McdeSysMount and mcde_sys_mounts() -- /proc/mounts on Linux
(label from /dev/disk/by-label, removable from /sys/block), getmntinfo
on NetBSD; pseudo file systems are marked volume=0, network file systems
network=1; test_sysinfo checks that / is mounted once.
dtwm: WmMcdeDesktop.c is the 4Dwm-style desktop: one override-redirect
window per icon in the top right corner, filled from the top down, then
the next column left; XPM glyphs; labels cut with '...'; the computer
('/'), disk mounts, removable media (mounted or not), network mounts and
the person's files from ~/.dt/Desktop and ~/Desktop; right click menu
(Open, Open Home, Show on All/This Workspace, Unmount, Move to Trash);
drag and drop; every icon's place and workspace in ~/.dt/desktop.conf
(kept for good); not mounted media are mounted by the double click.
WmWrkspace.c calls McdeDesktopWorkspaceChanged() on a workspace change.
tools/mcde-shot.sh: MCDE_DESK_TEST_MOUNTS, MCDE_DESK_TEST_DISKS,
MCDE_DESK_TEST_DESKTOP and MCDE_SHOT_KEEP_HOME for the tests.
147 lines
3.7 KiB
C
147 lines
3.7 KiB
C
/*
|
|
* mount_linux.c -- MCDE: the mounts of a Linux system (McdeSys)
|
|
*
|
|
* License: CNCL 1.0 (Could Not Care Less), see LICENSE at the top of the tree.
|
|
*
|
|
* /proc/mounts (the same list as mount(8) shows): device, mount point, file
|
|
* system type, options. The kernel escapes a space, a tab, a newline and a
|
|
* backslash in a name as \040, \011, \012 and \134; the name is unescaped
|
|
* here. A volume label is looked up in /dev/disk/by-label (the udev links),
|
|
* the removable flag in /sys/block/<drive>/removable -- both only when they
|
|
* are there, so a container without udev still gets its list.
|
|
*
|
|
* C99 and POSIX only (ext/src/mcde/AGENTS.md).
|
|
*/
|
|
|
|
#define _POSIX_C_SOURCE 200809L
|
|
|
|
#include <ctype.h>
|
|
#include <dirent.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <unistd.h>
|
|
|
|
#include <Mcde/McdeSys.h>
|
|
|
|
#include "private.h"
|
|
|
|
/* the kernel's escape: \040, \011, \012, \134 (and the octal \NNN form) */
|
|
static void
|
|
Unescape(const char *in, char *out, size_t len)
|
|
{
|
|
size_t o = 0;
|
|
|
|
while (*in != '\0' && o + 1 < len) {
|
|
if (*in == '\\' && isdigit((unsigned char) in[1]) &&
|
|
isdigit((unsigned char) in[2]) &&
|
|
isdigit((unsigned char) in[3])) {
|
|
unsigned int v = (unsigned int) ((in[1] - '0') * 64 +
|
|
(in[2] - '0') * 8 + (in[3] - '0'));
|
|
out[o++] = (char) (v & 0xff);
|
|
in += 4;
|
|
} else {
|
|
out[o++] = *in++;
|
|
}
|
|
}
|
|
out[o] = '\0';
|
|
}
|
|
|
|
/* the label udev gives the device, or "" */
|
|
static void
|
|
LabelOf(const char *device, char *buf, size_t len)
|
|
{
|
|
DIR *dir;
|
|
struct dirent *de;
|
|
char path[512], target[512];
|
|
ssize_t n;
|
|
|
|
buf[0] = '\0';
|
|
if (strncmp(device, "/dev/", 5) != 0)
|
|
return;
|
|
dir = opendir("/dev/disk/by-label");
|
|
if (dir == NULL)
|
|
return;
|
|
while ((de = readdir(dir)) != NULL) {
|
|
if (de->d_name[0] == '.')
|
|
continue;
|
|
snprintf(path, sizeof path, "/dev/disk/by-label/%s",
|
|
de->d_name);
|
|
if ((n = readlink(path, target, sizeof target - 1)) < 0)
|
|
continue;
|
|
target[n] = '\0';
|
|
if (strcmp(target, device + 5) == 0 ||
|
|
strcmp(target, device) == 0) {
|
|
snprintf(buf, len, "%s", de->d_name);
|
|
break;
|
|
}
|
|
}
|
|
closedir(dir);
|
|
}
|
|
|
|
/* the drive part of a device name: sda1 -> sda, nvme0n1p1 -> nvme0n1 */
|
|
static void
|
|
DriveOf(const char *name, char *buf, size_t len)
|
|
{
|
|
size_t n = strlen(name);
|
|
|
|
/* a partition of a classic disk ends in a digit (sda1, vda2);
|
|
* nvme and mmc put a "p" before it (nvme0n1p1, mmcblk0p1) */
|
|
while (n > 0 && isdigit((unsigned char) name[n - 1]))
|
|
n--;
|
|
if (n > 0 && name[n - 1] == 'p' && n >= 2 &&
|
|
isdigit((unsigned char) name[n - 2]))
|
|
n--;
|
|
snprintf(buf, len, "%.*s", (int) n, name);
|
|
}
|
|
|
|
static int
|
|
IsRemovable(const char *device)
|
|
{
|
|
char drive[64], path[256], buf[16];
|
|
FILE *f;
|
|
const char *base;
|
|
|
|
if (strncmp(device, "/dev/", 5) != 0)
|
|
return 0;
|
|
base = device + 5;
|
|
DriveOf(base, drive, sizeof drive);
|
|
snprintf(path, sizeof path, "/sys/block/%s/removable", drive);
|
|
if ((f = fopen(path, "r")) == NULL)
|
|
return 0;
|
|
if (fgets(buf, sizeof buf, f) == NULL) {
|
|
fclose(f);
|
|
return 0;
|
|
}
|
|
fclose(f);
|
|
return buf[0] == '1';
|
|
}
|
|
|
|
int
|
|
mcde_sys_mounts_backend(McdeSysMount *m, size_t max)
|
|
{
|
|
FILE *f;
|
|
char line[2048], dev[512], mp[512], type[64], rest[1024];
|
|
size_t n = 0;
|
|
|
|
if ((f = fopen("/proc/mounts", "r")) == NULL)
|
|
return mcde_sys_fail(MCDE_SYS_EFAIL, "No /proc/mounts.");
|
|
while (n < max && fgets(line, sizeof line, f) != NULL) {
|
|
McdeSysMount *x = &m[n];
|
|
|
|
if (sscanf(line, "%511s %511s %63s %1023[^\n]", dev, mp,
|
|
type, rest) < 3)
|
|
continue;
|
|
memset(x, 0, sizeof *x);
|
|
Unescape(dev, x->source, sizeof x->source);
|
|
Unescape(mp, x->mountpoint, sizeof x->mountpoint);
|
|
snprintf(x->fstype, sizeof x->fstype, "%s", type);
|
|
x->removable = IsRemovable(x->source);
|
|
if (!x->removable)
|
|
LabelOf(x->source, x->label, sizeof x->label);
|
|
n++;
|
|
}
|
|
fclose(f);
|
|
return (int) n;
|
|
}
|