forked from mafintosh/fuse-bindings
-
Notifications
You must be signed in to change notification settings - Fork 0
/
abstractions.cc
113 lines (80 loc) · 2.69 KB
/
abstractions.cc
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
#include "abstractions.h"
#ifndef _WIN32
#include <unistd.h>
#include <sys/wait.h>
int execute_command_and_wait (char* argv[]) {
// Fork our running process.
pid_t cpid = vfork();
// Check if we are the observer or the new process.
if (cpid > 0) {
int status = 0;
waitpid(cpid, &status, 0);
return WIFEXITED(status) ? WEXITSTATUS(status) : -1;
} else {
// At this point we are on our child process.
execvp(argv[0], argv);
exit(1);
// Something failed.
return -1;
}
}
#endif
#ifdef __APPLE__
#include <unistd.h>
#include <sys/wait.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void thread_create (abstr_thread_t* thread, thread_fn fn, void* data) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(thread, &attr, fn, data);
}
void thread_join (abstr_thread_t thread) {
pthread_join(thread, NULL);
}
int fusermount (char *path) {
char *argv[] = {(char *) "umount", path, NULL};
return execute_command_and_wait(argv);
}
#elif defined(_WIN32)
HANDLE mutex = CreateMutex(NULL, false, NULL);
void thread_create (HANDLE* thread, thread_fn fn, void* data) {
*thread = CreateThread(NULL, 0, fn, data, 0, NULL);
}
void thread_join (HANDLE thread) {
WaitForSingleObject(thread, INFINITE);
}
int fusermount (char *path) {
char* dokanPath = getenv("DOKAN_INSTALL_DIR");
char cmdLine[MAX_PATH];
if(dokanPath) sprintf(cmdLine, "\"%s/dokanctl.exe\" /u %s", dokanPath, path);
else sprintf(cmdLine, "dokanctl.exe /u %s", path);
STARTUPINFO info = {sizeof(info)};
PROCESS_INFORMATION procInfo;
CreateProcess(NULL, cmdLine, NULL, NULL, false, CREATE_NO_WINDOW, NULL, NULL, &info, &procInfo);
WaitForSingleObject(procInfo.hProcess, INFINITE);
DWORD exitCode = -1;
GetExitCodeProcess(procInfo.hProcess, &exitCode);
CloseHandle(procInfo.hProcess);
CloseHandle(procInfo.hThread);
return exitCode;
// dokanctl.exe requires admin permissions for some reason, so if node is not run as admin,
// it'll fail to create the process for unmounting. The path will be unmounted once
// the process is killed, however, so there's that!
}
#else
#include <unistd.h>
#include <sys/wait.h>
pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
void thread_create (abstr_thread_t* thread, thread_fn fn, void* data) {
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_create(thread, &attr, fn, data);
}
void thread_join (abstr_thread_t thread) {
pthread_join(thread, NULL);
}
int fusermount (char *path) {
char *argv[] = {(char *) "fusermount", (char *) "-q", (char *) "-u", path, NULL};
return execute_command_and_wait(argv);
}
#endif