Debian Codegraph @

contrib/depends/patches/libfuse/mount.c.diff

raw
1 diff --git a/lib/mount.c b/lib/mount.c
2 index d71e6fc..e9c2ff0 100644
3 --- a/lib/mount.c
4 +++ b/lib/mount.c
5 @@ -41,7 +41,6 @@
6 #define umount2(mnt, flags) unmount(mnt, (flags == 2) ? MNT_FORCE : 0)
7 #endif
8
9 -#define FUSERMOUNT_PROG "fusermount3"
10 #define FUSE_COMMFD_ENV "_FUSE_COMMFD"
11
12 #ifndef HAVE_FORK
13 @@ -117,17 +116,87 @@ static const struct fuse_opt fuse_mount_opts[] = {
14 FUSE_OPT_END
15 };
16
17 +int fileExists(const char* path);
18 +char* findBinaryInFusermountDir(const char* binaryName);
19 +
20 +int fileExists(const char* path) {
21 + FILE* file = fopen(path, "r");
22 + if (file) {
23 + fclose(file);
24 + return 1;
25 + }
26 + return 0;
27 +}
28 +
29 +char* findBinaryInFusermountDir(const char* binaryName) {
30 + // For security reasons, we do not search the binary on the $PATH;
31 + // instead, we check if the binary exists in FUSERMOUNT_DIR
32 + // as defined in meson.build
33 + char* binaryPath = malloc(strlen(FUSERMOUNT_DIR) + strlen(binaryName) + 2);
34 + strcpy(binaryPath, FUSERMOUNT_DIR);
35 + strcat(binaryPath, "/");
36 + strcat(binaryPath, binaryName);
37 + if (fileExists(binaryPath)) {
38 + return binaryPath;
39 + }
40 +
41 + // Debian, Ubuntu
42 + char* binaryPath2 = malloc(strlen(binaryName) + 6);
43 + strcat(binaryPath2, "/bin/");
44 + strcat(binaryPath2, binaryName);
45 + if (fileExists(binaryPath2)) {
46 + return binaryPath2;
47 + }
48 +
49 + // If the binary does not exist in FUSERMOUNT_DIR, return NULL
50 + return NULL;
51 +}
52 +
53 +static const char *fuse_mount_prog(void)
54 +{
55 + // Check if the FUSERMOUNT_PROG environment variable is set and if so, use it
56 + const char *prog = getenv("FUSERMOUNT_PROG");
57 + if (prog) {
58 + if (access(prog, X_OK) == 0)
59 + return prog;
60 + }
61 +
62 + // Check if there is a binary "fusermount3"
63 + prog = findBinaryInFusermountDir("fusermount3");
64 + if (access(prog, X_OK) == 0)
65 + return prog;
66 +
67 + // Check if there is a binary called "fusermount"
68 + // This is known to work for our purposes
69 + prog = findBinaryInFusermountDir("fusermount");
70 + if (access(prog, X_OK) == 0)
71 + return prog;
72 +
73 + // For i = 4...99, check if there is a binary called "fusermount" + i
74 + // It is not yet known whether this will work for our purposes, but it is better than not even attempting
75 + for (int i = 4; i < 100; i++) {
76 + prog = findBinaryInFusermountDir("fusermount" + i);
77 + if (access(prog, X_OK) == 0)
78 + return prog;
79 + }
80 +
81 + // If all else fails, return NULL
82 + return NULL;
83 +}
84 +
85 static void exec_fusermount(const char *argv[])
86 {
87 - execv(FUSERMOUNT_DIR "/" FUSERMOUNT_PROG, (char **) argv);
88 - execvp(FUSERMOUNT_PROG, (char **) argv);
89 + const char *fusermount_prog = fuse_mount_prog();
90 + if (fusermount_prog) {
91 + execv(fusermount_prog, (char **) argv);
92 + }
93 }
94
95 void fuse_mount_version(void)
96 {
97 int pid = fork();
98 if (!pid) {
99 - const char *argv[] = { FUSERMOUNT_PROG, "--version", NULL };
100 + const char *argv[] = { fuse_mount_prog(), "--version", NULL };
101 exec_fusermount(argv);
102 _exit(1);
103 } else if (pid != -1)
104 @@ -300,7 +369,7 @@ void fuse_kern_unmount(const char *mountpoint, int fd)
105 return;
106
107 if(pid == 0) {
108 - const char *argv[] = { FUSERMOUNT_PROG, "-u", "-q", "-z",
109 + const char *argv[] = { fuse_mount_prog(), "-u", "-q", "-z",
110 "--", mountpoint, NULL };
111
112 exec_fusermount(argv);
113 @@ -346,7 +415,7 @@ static int setup_auto_unmount(const char *mountpoint, int quiet)
114 }
115 }
116
117 - argv[a++] = FUSERMOUNT_PROG;
118 + argv[a++] = fuse_mount_prog();
119 argv[a++] = "--auto-unmount";
120 argv[a++] = "--";
121 argv[a++] = mountpoint;
122 @@ -407,7 +476,7 @@ static int fuse_mount_fusermount(const char *mountpoint, struct mount_opts *mo,
123 }
124 }
125
126 - argv[a++] = FUSERMOUNT_PROG;
127 + argv[a++] = fuse_mount_prog();
128 if (opts) {
129 argv[a++] = "-o";
130 argv[a++] = opts;
131 @@ -421,7 +490,7 @@ static int fuse_mount_fusermount(const char *mountpoint, struct mount_opts *mo,
132 snprintf(env, sizeof(env), "%i", fds[0]);
133 setenv(FUSE_COMMFD_ENV, env, 1);
134 exec_fusermount(argv);
135 - perror("fuse: failed to exec fusermount3");
136 + perror("fuse: failed to exec fusermount");
137 _exit(1);
138 }
139