-
Notifications
You must be signed in to change notification settings - Fork 511
/
Copy pathpmem2_utils_linux.c
95 lines (79 loc) · 1.82 KB
/
pmem2_utils_linux.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
// SPDX-License-Identifier: BSD-3-Clause
/* Copyright 2014-2023, Intel Corporation */
#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/stat.h>
#include <sys/sysmacros.h>
#include "libpmem2.h"
#include "out.h"
#include "pmem2_utils.h"
#include "region_namespace.h"
#include "source.h"
#include "alloc.h"
/*
* pmem2_get_type_from_stat -- determine type of file based on output of stat
* syscall
*/
int
pmem2_get_type_from_stat(const os_stat_t *st, enum pmem2_file_type *type)
{
if (S_ISREG(st->st_mode)) {
*type = PMEM2_FTYPE_REG;
return 0;
}
if (S_ISDIR(st->st_mode)) {
*type = PMEM2_FTYPE_DIR;
return 0;
}
if (!S_ISCHR(st->st_mode)) {
ERR("file type 0%o not supported", st->st_mode & S_IFMT);
return PMEM2_E_INVALID_FILE_TYPE;
}
int ret = 0;
char *spath = NULL;
char *npath = NULL;
spath = Malloc(PATH_MAX * sizeof(char));
if (spath == NULL) {
ERR("!Malloc");
return PMEM2_E_ERRNO;
}
ret = util_snprintf(spath, PATH_MAX,
"/sys/dev/char/%u:%u/subsystem",
os_major(st->st_rdev), os_minor(st->st_rdev));
if (ret < 0) {
/* impossible */
ERR("!snprintf");
ASSERTinfo(0, "snprintf failed");
ret = PMEM2_E_ERRNO;
goto end;
}
LOG(4, "device subsystem path \"%s\"", spath);
npath = Malloc(PATH_MAX * sizeof(char));
if (npath == NULL) {
ERR("!Malloc");
ret = PMEM2_E_ERRNO;
goto end;
}
char *rpath = realpath(spath, npath);
if (rpath == NULL) {
ERR("!realpath \"%s\"", spath);
ret = PMEM2_E_ERRNO;
goto end;
}
char *basename = strrchr(rpath, '/');
if (!basename || strcmp("dax", basename + 1) != 0) {
LOG(3, "%s path does not match device dax prefix path", rpath);
ret = PMEM2_E_INVALID_FILE_TYPE;
goto end;
}
*type = PMEM2_FTYPE_DEVDAX;
end:
if (npath)
Free(npath);
if (spath)
Free(spath);
return ret;
}