/
fs.c
157 lines (127 loc) · 3.23 KB
/
fs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/***************************************************************************
begin........: April 2015
copyright....: Sebastian Fedrau
email........: sebastian.fedrau@gmail.com
***************************************************************************/
/***************************************************************************
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License v3 as published by
the Free Software Foundation.
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License v3 for more details.
***************************************************************************/
/**
@file fs.c
@brief Filesystem related functions.
@author Sebastian Fedrau <sebastian.fedrau@gmail.com>
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <mntent.h>
#include <assert.h>
#include "fs.h"
#include "log.h"
#include "utils.h"
#include "gettext.h"
/*! Name of unknown filesystems. */
const char *FS_UNKNOWN = "Unknown";
static int
_fs_map_compare_entries(const void *a, const void *b)
{
assert(a != NULL);
assert(b != NULL);
return strcmp((*((MountPoint **)b))->path, (*((MountPoint **)a))->path);
}
FSMap *
fs_map_load(void)
{
FSMap *map = NULL;
FILE *fp;
fp = setmntent("/proc/mounts", "r");
if(fp)
{
/* store available mount points in array */
map = utils_new(1, FSMap);
map->mps = utils_new(32, MountPoint *);
map->size = 32;
map->len = 0;
struct mntent *ent = getmntent(fp);
while(ent)
{
if(map->len == map->size - 1)
{
if(map->size > SIZE_MAX / 2)
{
FATAL("misc", "Integer overflow.");
fprintf(stderr, _("Couldn't allocate memory.\n"));
abort();
}
map->size *= 2;
map->mps = utils_renew(map->mps, map->size, MountPoint *);
}
map->mps[map->len] = utils_new(1, MountPoint);
memset(map->mps[map->len]->fs, 0, FS_NAME_MAX);
strncpy(map->mps[map->len]->fs, ent->mnt_type, FS_NAME_MAX - 1);
memset(map->mps[map->len]->path, 0, PATH_MAX);
strncpy(map->mps[map->len]->path, ent->mnt_dir, PATH_MAX - 1);
++map->len;
ent = getmntent(fp);
}
endmntent(fp);
/* sort array by path in descending order */
if(map->len)
{
qsort(map->mps, map->len, sizeof(MountPoint *), &_fs_map_compare_entries);
}
}
else
{
fprintf(stderr, "setmntent() failed.\n");
}
return map;
}
void
fs_map_destroy(FSMap *map)
{
if(map)
{
for(size_t i = 0; i < map->len; ++i)
{
free(map->mps[i]);
}
free(map->mps);
free(map);
}
}
const char *
fs_map_path(FSMap *map, const char *path)
{
assert(map != NULL);
assert(path != NULL);
char filename[PATH_MAX];
if(realpath(path, filename))
{
for(size_t i = 0; i < map->len; ++i)
{
size_t len = strlen(map->mps[i]->path);
if(len <= strlen(filename))
{
if(!strncmp(map->mps[i]->path, filename, len))
{
return map->mps[i]->fs;
}
}
}
}
else
{
fprintf(stderr, _("Couldn't retrieve real path from file: %s\n"), path);
perror("realpath()");
}
return FS_UNKNOWN;
}