@photostructure/fs-metadata 0.3.3 → 0.4.0

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.
@@ -15,6 +15,13 @@ GetHiddenWorker::GetHiddenWorker(std::string path,
15
15
 
16
16
  void GetHiddenWorker::Execute() {
17
17
  DEBUG_LOG("[GetHiddenWorker] checking hidden status for: %s", path_.c_str());
18
+
19
+ // Add path validation to prevent directory traversal
20
+ if (path_.find("..") != std::string::npos) {
21
+ SetError("Invalid path containing '..'");
22
+ return;
23
+ }
24
+
18
25
  struct stat statbuf;
19
26
  if (stat(path_.c_str(), &statbuf) != 0) {
20
27
  int error = errno;
@@ -73,6 +80,13 @@ SetHiddenWorker::SetHiddenWorker(std::string path, bool hidden,
73
80
  void SetHiddenWorker::Execute() {
74
81
  DEBUG_LOG("[SetHiddenWorker] setting hidden=%d for: %s", hidden_,
75
82
  path_.c_str());
83
+
84
+ // Add path validation to prevent directory traversal
85
+ if (path_.find("..") != std::string::npos) {
86
+ SetError("Invalid path containing '..'");
87
+ return;
88
+ }
89
+
76
90
  struct stat statbuf;
77
91
  if (stat(path_.c_str(), &statbuf) != 0) {
78
92
  int error = errno;
@@ -0,0 +1,85 @@
1
+ #pragma once
2
+
3
+ #include <CoreFoundation/CoreFoundation.h>
4
+ #include <sys/mount.h>
5
+
6
+ namespace FSMeta {
7
+
8
+ // Generic RAII wrapper for resources that need free()
9
+ template <typename T> class ResourceRAII {
10
+ private:
11
+ T *resource_;
12
+
13
+ public:
14
+ ResourceRAII() : resource_(nullptr) {}
15
+ ~ResourceRAII() {
16
+ if (resource_) {
17
+ free(resource_);
18
+ }
19
+ }
20
+
21
+ T **ptr() { return &resource_; }
22
+ T *get() { return resource_; }
23
+
24
+ // Add move operations for better resource management
25
+ ResourceRAII(ResourceRAII &&other) noexcept : resource_(other.resource_) {
26
+ other.resource_ = nullptr;
27
+ }
28
+
29
+ ResourceRAII &operator=(ResourceRAII &&other) noexcept {
30
+ if (this != &other) {
31
+ if (resource_)
32
+ free(resource_);
33
+ resource_ = other.resource_;
34
+ other.resource_ = nullptr;
35
+ }
36
+ return *this;
37
+ }
38
+
39
+ // Prevent copying
40
+ ResourceRAII(const ResourceRAII &) = delete;
41
+ ResourceRAII &operator=(const ResourceRAII &) = delete;
42
+ };
43
+
44
+ // Specialized for mount info
45
+ using MountBufferRAII = ResourceRAII<struct statfs>;
46
+
47
+ // CoreFoundation RAII wrapper
48
+ template <typename T> class CFReleaser {
49
+ private:
50
+ T ref_;
51
+
52
+ public:
53
+ explicit CFReleaser(T ref = nullptr) noexcept : ref_(ref) {}
54
+ ~CFReleaser() { reset(); }
55
+
56
+ void reset(T ref = nullptr) {
57
+ if (ref_) {
58
+ CFRelease(ref_);
59
+ }
60
+ ref_ = ref;
61
+ }
62
+
63
+ operator T() const noexcept { return ref_; }
64
+ T get() const noexcept { return ref_; }
65
+ bool isValid() const noexcept { return ref_ != nullptr; }
66
+
67
+ // Prevent copying
68
+ CFReleaser(const CFReleaser &) = delete;
69
+ CFReleaser &operator=(const CFReleaser &) = delete;
70
+
71
+ // Allow moving
72
+ CFReleaser(CFReleaser &&other) noexcept : ref_(other.ref_) {
73
+ other.ref_ = nullptr;
74
+ }
75
+ CFReleaser &operator=(CFReleaser &&other) noexcept {
76
+ if (this != &other) {
77
+ reset();
78
+ ref_ = other.ref_;
79
+ other.ref_ = nullptr;
80
+ }
81
+ return *this;
82
+ }
83
+ };
84
+
85
+ } // namespace FSMeta
@@ -2,6 +2,7 @@
2
2
 
3
3
  #include "../common/debug_log.h"
4
4
  #include "./fs_meta.h"
5
+ #include "./raii_utils.h"
5
6
 
6
7
  #include <CoreFoundation/CoreFoundation.h>
7
8
  #include <DiskArbitration/DiskArbitration.h>
@@ -36,55 +37,6 @@ static std::string CFStringToString(CFStringRef cfString) {
36
37
  return result;
37
38
  }
38
39
 
39
- // Improved CFReleaser with proper Core Foundation support
40
- template <typename T> class CFReleaser {
41
- private:
42
- T ref_;
43
-
44
- public:
45
- explicit CFReleaser(T ref = nullptr) noexcept : ref_(ref) {}
46
-
47
- // Delete copy operations
48
- CFReleaser(const CFReleaser &) = delete;
49
- CFReleaser &operator=(const CFReleaser &) = delete;
50
-
51
- // Move operations
52
- CFReleaser(CFReleaser &&other) noexcept : ref_(other.ref_) {
53
- other.ref_ = nullptr;
54
- }
55
-
56
- CFReleaser &operator=(CFReleaser &&other) noexcept {
57
- if (this != &other) {
58
- reset();
59
- ref_ = other.ref_;
60
- other.ref_ = nullptr;
61
- }
62
- return *this;
63
- }
64
-
65
- ~CFReleaser() { reset(); }
66
-
67
- void reset(T ref = nullptr) {
68
- if (ref_) {
69
- CFRelease(ref_);
70
- }
71
- ref_ = ref;
72
- }
73
-
74
- // Implicit conversion operator for Core Foundation APIs
75
- operator T() const noexcept { return ref_; }
76
-
77
- T get() const noexcept { return ref_; }
78
- bool isValid() const noexcept { return ref_ != nullptr; }
79
-
80
- // Release ownership
81
- T release() noexcept {
82
- T temp = ref_;
83
- ref_ = nullptr;
84
- return temp;
85
- }
86
- };
87
-
88
40
  class GetVolumeMetadataWorker : public MetadataWorkerBase {
89
41
  public:
90
42
  GetVolumeMetadataWorker(const std::string &mountPoint,
@@ -178,12 +130,12 @@ private:
178
130
  // Check if this is a network filesystem
179
131
  if (metadata.fstype == "smbfs" || metadata.fstype == "nfs" ||
180
132
  metadata.fstype == "afpfs" || metadata.fstype == "webdav") {
181
- // For network filesystems, we consider them healthy even without DA info
182
133
  metadata.remote = true;
183
134
  metadata.status = "healthy";
184
135
  return;
185
136
  }
186
137
 
138
+ // Create session on current thread
187
139
  CFReleaser<DASessionRef> session(DASessionCreate(kCFAllocatorDefault));
188
140
  if (!session.isValid()) {
189
141
  DEBUG_LOG("[GetVolumeMetadataWorker] Failed to create DA session");
@@ -193,21 +145,39 @@ private:
193
145
  }
194
146
 
195
147
  try {
196
- // RAII cleanup for RunLoop scheduling
148
+ // Get thread-local runloop or create new one if needed
149
+ CFRunLoopRef runLoop = CFRunLoopGetCurrent();
150
+ if (!runLoop) {
151
+ // If no runloop exists, create a new one for this thread
152
+ CFRunLoopRun();
153
+ runLoop = CFRunLoopGetCurrent();
154
+ if (!runLoop) {
155
+ throw std::runtime_error("Failed to create thread-local runloop");
156
+ }
157
+ }
158
+
159
+ // Schedule session with our runloop
160
+ DASessionScheduleWithRunLoop(session.get(), runLoop,
161
+ kCFRunLoopDefaultMode);
162
+
163
+ // Use RAII to ensure cleanup
197
164
  struct RunLoopCleaner {
198
165
  DASessionRef session;
199
- explicit RunLoopCleaner(DASessionRef s) : session(s) {}
166
+ CFRunLoopRef runLoop;
167
+ bool shouldStop;
168
+ RunLoopCleaner(DASessionRef s, CFRunLoopRef l, bool stop = false)
169
+ : session(s), runLoop(l), shouldStop(stop) {}
200
170
  ~RunLoopCleaner() {
201
- DASessionUnscheduleFromRunLoop(session, CFRunLoopGetCurrent(),
171
+ DASessionUnscheduleFromRunLoop(session, runLoop,
202
172
  kCFRunLoopDefaultMode);
173
+ if (shouldStop) {
174
+ CFRunLoopStop(runLoop);
175
+ }
203
176
  }
204
- };
177
+ } scopeGuard(session.get(), runLoop, !CFRunLoopGetCurrent());
205
178
 
206
- // Schedule session with RunLoop
207
- DASessionScheduleWithRunLoop(session.get(), CFRunLoopGetCurrent(),
208
- kCFRunLoopDefaultMode);
209
-
210
- auto scopeGuard = std::make_unique<RunLoopCleaner>(session.get());
179
+ // Run the run loop briefly to ensure DA is ready
180
+ CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, true);
211
181
 
212
182
  CFReleaser<DADiskRef> disk(DADiskCreateFromBSDName(
213
183
  kCFAllocatorDefault, session.get(), metadata.mountFrom.c_str()));
@@ -228,12 +198,17 @@ private:
228
198
  return;
229
199
  }
230
200
 
201
+ // Ensure we have a complete description before continuing
202
+ CFRunLoopRunInMode(kCFRunLoopDefaultMode, 0.1, false);
203
+
204
+ // Process description synchronously since we're already on the right
205
+ // thread
231
206
  ProcessDiskDescription(description.get());
232
207
 
233
- // Only set ready if we got this far without errors
234
208
  if (metadata.status != "partial") {
235
209
  metadata.status = "healthy";
236
210
  }
211
+
237
212
  } catch (const std::exception &e) {
238
213
  DEBUG_LOG("[GetVolumeMetadataWorker] Exception: %s", e.what());
239
214
  metadata.status = "error";
@@ -1,7 +1,8 @@
1
1
  // src/darwin/volume_mount_points.cpp
2
2
  #include "../common/volume_mount_points.h"
3
3
  #include "../common/debug_log.h"
4
- #include "fs_meta.h"
4
+ #include "./fs_meta.h"
5
+ #include "./raii_utils.h"
5
6
  #include <chrono>
6
7
  #include <future>
7
8
  #include <sys/mount.h>
@@ -24,9 +25,12 @@ public:
24
25
  void Execute() override {
25
26
  DEBUG_LOG("[GetVolumeMountPointsWorker] Executing");
26
27
  try {
27
- // Get mount list - this is fast
28
- struct statfs *mntbufp;
29
- int count = getmntinfo(&mntbufp, MNT_WAIT);
28
+ MountBufferRAII mntbuf;
29
+ // Use MNT_NOWAIT for better performance - we'll verify accessibility
30
+ // separately and our error handling already covers mount state changes
31
+ // See https://github.com/swiftlang/swift-corelibs-foundation/issues/4649
32
+
33
+ int count = getmntinfo_r_np(mntbuf.ptr(), MNT_NOWAIT);
30
34
 
31
35
  if (count <= 0) {
32
36
  throw std::runtime_error("Failed to get mount information");
@@ -34,36 +38,39 @@ public:
34
38
 
35
39
  for (int i = 0; i < count; i++) {
36
40
  MountPoint mp;
37
- mp.mountPoint = mntbufp[i].f_mntonname;
38
- mp.fstype = mntbufp[i].f_fstypename;
41
+ mp.mountPoint = mntbuf.get()[i].f_mntonname;
42
+ mp.fstype = mntbuf.get()[i].f_fstypename;
39
43
  mp.error = ""; // Initialize error field
40
44
 
41
45
  DEBUG_LOG("[GetVolumeMountPointsWorker] Checking mount point: %s",
42
46
  mp.mountPoint.c_str());
43
47
 
44
48
  try {
45
- // Use shared_future to allow multiple gets
46
- std::shared_future<bool> future =
49
+ // Use RAII to manage future
50
+ auto future = std::make_shared<std::future<bool>>(
47
51
  std::async(std::launch::async, [path = mp.mountPoint]() {
48
- return access(path.c_str(), R_OK) == 0;
49
- }).share();
52
+ // Use faccessat for better security
53
+ return faccessat(AT_FDCWD, path.c_str(), R_OK, AT_EACCESS) == 0;
54
+ }));
50
55
 
51
- auto status = future.wait_for(std::chrono::milliseconds(timeoutMs_));
56
+ auto status = future->wait_for(std::chrono::milliseconds(timeoutMs_));
52
57
 
53
- if (status == std::future_status::timeout) {
58
+ switch (status) {
59
+ case std::future_status::timeout:
54
60
  mp.status = "disconnected";
55
61
  mp.error = "Access check timed out";
56
- DEBUG_LOG(
57
- "[GetVolumeMountPointsWorker] Access check timed out for: %s",
58
- mp.mountPoint.c_str());
59
- } else if (status == std::future_status::ready) {
62
+ DEBUG_LOG("[GetVolumeMountPointsWorker] Access check timed out: %s",
63
+ mp.mountPoint.c_str());
64
+ break;
65
+
66
+ case std::future_status::ready:
60
67
  try {
61
- bool isAccessible = future.get();
68
+ bool isAccessible = future->get();
62
69
  mp.status = isAccessible ? "healthy" : "inaccessible";
63
70
  if (!isAccessible) {
64
71
  mp.error = "Path is not accessible";
65
72
  }
66
- DEBUG_LOG("[GetVolumeMountPointsWorker] Access check %s for: %s",
73
+ DEBUG_LOG("[GetVolumeMountPointsWorker] Access check %s: %s",
67
74
  isAccessible ? "succeeded" : "failed",
68
75
  mp.mountPoint.c_str());
69
76
  } catch (const std::exception &e) {
@@ -71,12 +78,14 @@ public:
71
78
  mp.error = std::string("Access check failed: ") + e.what();
72
79
  DEBUG_LOG("[GetVolumeMountPointsWorker] Exception: %s", e.what());
73
80
  }
74
- } else {
81
+ break;
82
+
83
+ default:
75
84
  mp.status = "error";
76
85
  mp.error = "Unexpected future status";
77
- DEBUG_LOG(
78
- "[GetVolumeMountPointsWorker] Unexpected future status for: %s",
79
- mp.mountPoint.c_str());
86
+ DEBUG_LOG("[GetVolumeMountPointsWorker] Unexpected status: %s",
87
+ mp.mountPoint.c_str());
88
+ break;
80
89
  }
81
90
  } catch (const std::exception &e) {
82
91
  mp.status = "error";
package/src/object.ts CHANGED
@@ -3,7 +3,7 @@
3
3
  import { isNotBlank, isString } from "./string.js";
4
4
 
5
5
  /**
6
- * Check if a value is an object
6
+ * @return true iff value is an object and not an array
7
7
  */
8
8
  export function isObject(value: unknown): value is object {
9
9
  // typeof null is 'object', so we need to check for that case YAY JAVASCRIPT
@@ -11,7 +11,8 @@ export function isObject(value: unknown): value is object {
11
11
  }
12
12
 
13
13
  /**
14
- * Map a value to another value, or undefined if the value is undefined
14
+ * @return undefined if `obj` is nullish, or the return value of `fn` applied
15
+ * against `obj` if `obj` is defined
15
16
  */
16
17
  export function map<T, U>(
17
18
  obj: T | undefined,
@@ -21,7 +22,7 @@ export function map<T, U>(
21
22
  }
22
23
 
23
24
  /**
24
- * Omit the specified fields from an object
25
+ * @return a shallow copy of `obj` that omits the specified `keys`
25
26
  */
26
27
  export function omit<T extends object, K extends keyof T>(
27
28
  obj: T,
@@ -40,6 +41,10 @@ export function omit<T extends object, K extends keyof T>(
40
41
  return result;
41
42
  }
42
43
 
44
+ /**
45
+ * @return a shallow copy of `obj` that only includes fields that are defined
46
+ * and not nullish or blank.
47
+ */
43
48
  export function compactValues<T extends object>(
44
49
  obj: T | undefined,
45
50
  ): Partial<T> {
package/src/options.ts CHANGED
@@ -51,7 +51,7 @@ export const SystemPathPatternsDefault = [
51
51
  "/System/Volumes/Update",
52
52
  "/System/Volumes/VM",
53
53
  "/System/Volumes/xarts",
54
- ];
54
+ ] as const;
55
55
 
56
56
  /**
57
57
  * Filesystem types that indicate system volumes
package/src/path.ts CHANGED
@@ -26,10 +26,10 @@ export function normalizePosixPath(
26
26
  ): string | undefined {
27
27
  if (isBlank(mountPoint)) return undefined;
28
28
  if (mountPoint === "/") return mountPoint;
29
-
29
+
30
30
  // Fast path: check last char only if no trailing slash
31
31
  if (mountPoint[mountPoint.length - 1] !== "/") return mountPoint;
32
-
32
+
33
33
  // Slower path: trim trailing slashes
34
34
  let end = mountPoint.length - 1;
35
35
  while (end > 0 && mountPoint[end] === "/") {