@ohos-ports/mountutils 0.0.0-stage → 2.0.8-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,148 @@
1
+ /*
2
+ * Copyright 2017 resin.io
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include <sys/stat.h>
18
+ #include <sys/mount.h>
19
+ #include <mntent.h>
20
+ #include <errno.h>
21
+ #include "../mountutils.hpp"
22
+
23
+ MOUNTUTILS_RESULT unmount_disk(const char *device_path) {
24
+ const char *mount_path = NULL;
25
+ std::vector<std::string> mount_dirs = {};
26
+
27
+ // Stat the device to make sure it exists
28
+ struct stat stats;
29
+
30
+ if (stat(device_path, &stats) != 0) {
31
+ MountUtilsLog("Stat failed");
32
+
33
+ // TODO(jhermsmeier): See TODO below
34
+ // v8::Local<v8::Value> argv[1] = {
35
+ // Nan::ErrnoException(errno, "stat", NULL, device_path)
36
+ // };
37
+ // Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, 1, argv);
38
+ return MOUNTUTILS_ERROR_GENERAL;
39
+ } else if (S_ISDIR(stats.st_mode)) {
40
+ MountUtilsLog("Device is a directory");
41
+
42
+ // TODO(jhermsmeier): See TODO below
43
+ // v8::Local<v8::Value> argv[1] = {
44
+ // Nan::Error("Invalid device, path is a directory")
45
+ // };
46
+ // Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, 1, argv);
47
+ return MOUNTUTILS_ERROR_INVALID_DRIVE;
48
+ }
49
+
50
+ // Get mountpaths from the device path, as `umount(device)`
51
+ // has been removed in Linux 2.3+
52
+ struct mntent *mnt_p, data;
53
+ // See https://github.com/RasPlex/aufs-utils/commit/2d1a37468cdc1f9c779cbf22267c5ae491a44f8e
54
+ char mnt_buf[4096 + 1024];
55
+ FILE *proc_mounts;
56
+
57
+ MountUtilsLog("Reading /proc/mounts");
58
+ proc_mounts = setmntent("/proc/mounts", "r");
59
+
60
+ if (proc_mounts == NULL) {
61
+ MountUtilsLog("Couldn't read /proc/mounts");
62
+ // TODO(jhermsmeier): Refactor MOUNTUTILS_RESULT into a struct
63
+ // with error_msg, errno, error_code etc. and set the respective
64
+ // values on the struct and move creation of proper errors with
65
+ // the right errno messages etc. into the AsyncWorkers (even better:
66
+ // create a function which creates the proper error from a
67
+ // MOUNTUTILS_RESULT struct).
68
+ // v8::Local<v8::Value> argv[1] = {
69
+ // Nan::ErrnoException(errno, "setmntent", NULL, "/proc/mounts")
70
+ // };
71
+ // Nan::MakeCallback(Nan::GetCurrentContext()->Global(), callback, 1, argv);
72
+ return MOUNTUTILS_ERROR_GENERAL;
73
+ }
74
+
75
+ while ((mnt_p = getmntent_r(proc_mounts, &data, mnt_buf, sizeof(mnt_buf)))) {
76
+ mount_path = mnt_p->mnt_fsname;
77
+ if (strncmp(mount_path, device_path, strlen(device_path)) == 0) {
78
+ MountUtilsLog("Mount point " + std::string(mount_path) +
79
+ " belongs to drive " + std::string(device_path));
80
+ mount_dirs.push_back(std::string(mnt_p->mnt_dir));
81
+ }
82
+ }
83
+
84
+ MountUtilsLog("Closing /proc/mounts");
85
+ endmntent(proc_mounts);
86
+
87
+ // Use umount2() with the MNT_DETACH flag, which performs a lazy unmount;
88
+ // making the mount point unavailable for new accesses,
89
+ // and only actually unmounting when the mount point ceases to be busy
90
+ // TODO(jhermsmeier): See TODO above
91
+ // v8::Local<v8::Value> argv[1] = {
92
+ // Nan::ErrnoException(errno, "umount2", NULL, mnt_p->mnt_dir)
93
+ // };
94
+ // v8::Local<v8::Object> ctx = Nan::GetCurrentContext()->Global();
95
+ // Nan::MakeCallback(ctx, callback, 1, argv);
96
+
97
+ size_t unmounts = 0;
98
+ MOUNTUTILS_RESULT result_code = MOUNTUTILS_SUCCESS;
99
+
100
+ for (std::string mount_dir : mount_dirs) {
101
+ MountUtilsLog("Unmounting " + mount_dir + "...");
102
+
103
+ mount_path = mount_dir.c_str();
104
+
105
+ if (umount2(mount_path, MNT_EXPIRE) != 0) {
106
+ MountUtilsLog("Unmount MNT_EXPIRE " + mount_dir + ": EAGAIN");
107
+ if (umount2(mount_path, MNT_EXPIRE) != 0) {
108
+ MountUtilsLog("Unmount MNT_EXPIRE " + mount_dir + " failed: " +
109
+ std::string(strerror(errno)));
110
+ } else {
111
+ MountUtilsLog("Unmount " + mount_dir + ": success");
112
+ unmounts++;
113
+ continue;
114
+ }
115
+ } else {
116
+ MountUtilsLog("Unmount " + mount_dir + ": success");
117
+ unmounts++;
118
+ continue;
119
+ }
120
+
121
+ if (umount2(mount_path, MNT_DETACH) != 0) {
122
+ MountUtilsLog("Unmount MNT_DETACH " + mount_dir + " failed: " +
123
+ std::string(strerror(errno)));
124
+ } else {
125
+ MountUtilsLog("Unmount " + mount_dir + ": success");
126
+ unmounts++;
127
+ continue;
128
+ }
129
+
130
+ if (umount2(mount_path, MNT_FORCE) != 0) {
131
+ MountUtilsLog("Unmount MNT_FORCE " + mount_dir + " failed: " +
132
+ std::string(strerror(errno)));
133
+ } else {
134
+ MountUtilsLog("Unmount " + mount_dir + ": success");
135
+ unmounts++;
136
+ continue;
137
+ }
138
+ }
139
+
140
+ return unmounts == mount_dirs.size() ?
141
+ MOUNTUTILS_SUCCESS : MOUNTUTILS_ERROR_GENERAL;
142
+ }
143
+
144
+ // FIXME: This is just a stub copy of `UnmountDisk()`,
145
+ // and needs implementation!
146
+ MOUNTUTILS_RESULT eject_disk(const char *device) {
147
+ return unmount_disk(device);
148
+ }
@@ -0,0 +1,33 @@
1
+ /*
2
+ * Copyright 2017 resin.io
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License");
5
+ * you may not use this file except in compliance with the License.
6
+ * You may obtain a copy of the License at
7
+ *
8
+ * http://www.apache.org/licenses/LICENSE-2.0
9
+ *
10
+ * Unless required by applicable law or agreed to in writing, software
11
+ * distributed under the License is distributed on an "AS IS" BASIS,
12
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
+ * See the License for the specific language governing permissions and
14
+ * limitations under the License.
15
+ */
16
+
17
+ #include <cstdlib>
18
+ #include <iostream>
19
+ #include "mountutils.hpp"
20
+
21
+ NAN_MODULE_INIT(MountUtilsInit) {
22
+ NAN_EXPORT(target, unmountDisk);
23
+ NAN_EXPORT(target, eject);
24
+ }
25
+
26
+ void MountUtilsLog(std::string string) {
27
+ const char* debug = std::getenv("MOUNTUTILS_DEBUG");
28
+ if (debug != NULL) {
29
+ std::cout << "[mountutils] " << string << std::endl;
30
+ }
31
+ }
32
+
33
+ NODE_MODULE(MountUtils, MountUtilsInit)
@@ -0,0 +1,39 @@
1
+ #ifndef SRC_MOUNTUTILS_HPP_
2
+ #define SRC_MOUNTUTILS_HPP_
3
+
4
+ /*
5
+ * Copyright 2017 resin.io
6
+ *
7
+ * Licensed under the Apache License, Version 2.0 (the "License");
8
+ * you may not use this file except in compliance with the License.
9
+ * You may obtain a copy of the License at
10
+ *
11
+ * http://www.apache.org/licenses/LICENSE-2.0
12
+ *
13
+ * Unless required by applicable law or agreed to in writing, software
14
+ * distributed under the License is distributed on an "AS IS" BASIS,
15
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16
+ * See the License for the specific language governing permissions and
17
+ * limitations under the License.
18
+ */
19
+
20
+ #include <nan.h>
21
+ #include <string>
22
+
23
+ enum MOUNTUTILS_RESULT {
24
+ MOUNTUTILS_SUCCESS,
25
+ MOUNTUTILS_ERROR_INVALID_DRIVE,
26
+ MOUNTUTILS_ERROR_ACCESS_DENIED,
27
+ MOUNTUTILS_ERROR_AGAIN,
28
+ MOUNTUTILS_ERROR_GENERAL
29
+ };
30
+
31
+ void MountUtilsLog(std::string string);
32
+
33
+ MOUNTUTILS_RESULT unmount_disk(const char *device);
34
+ MOUNTUTILS_RESULT eject_disk(const char *device);
35
+
36
+ NAN_METHOD(unmountDisk);
37
+ NAN_METHOD(eject);
38
+
39
+ #endif // SRC_MOUNTUTILS_HPP_