@ng-native/desktop 0.0.0-stage → 0.23.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.
- package/LICENSE +21 -0
- package/README.md +51 -2
- package/app.plugin.cjs +17 -0
- package/bin/app-config.mjs +168 -0
- package/bin/autolink.mjs +224 -0
- package/bin/cli.mjs +158 -0
- package/bin/host.mjs +427 -0
- package/bin/ng-native-desktop.mjs +4 -0
- package/bin/package.mjs +971 -0
- package/bin/plist.mjs +68 -0
- package/bin/prebuilt.mjs +328 -0
- package/bin/toolchain.mjs +192 -0
- package/native/CMakeLists.txt +276 -0
- package/native/appkit/CMakeLists.txt +109 -0
- package/native/appkit/address-search.h +26 -0
- package/native/appkit/address-search.mm +277 -0
- package/native/appkit/animation-frames.h +14 -0
- package/native/appkit/animation-frames.mm +106 -0
- package/native/appkit/app-info.h +12 -0
- package/native/appkit/app-info.mm +98 -0
- package/native/appkit/app-updater.mm +128 -0
- package/native/appkit/app-windows.h +81 -0
- package/native/appkit/app-windows.mm +318 -0
- package/native/appkit/appkit-backend.h +73 -0
- package/native/appkit/appkit-backend.mm +276 -0
- package/native/appkit/apple-authentication.h +19 -0
- package/native/appkit/apple-authentication.mm +248 -0
- package/native/appkit/apple-button-view.h +10 -0
- package/native/appkit/apple-button-view.mm +37 -0
- package/native/appkit/apple-id-button.h +16 -0
- package/native/appkit/apple-id-button.mm +50 -0
- package/native/appkit/audio.h +16 -0
- package/native/appkit/audio.mm +275 -0
- package/native/appkit/battery.h +13 -0
- package/native/appkit/battery.mm +92 -0
- package/native/appkit/bookmarks.h +17 -0
- package/native/appkit/bookmarks.mm +145 -0
- package/native/appkit/calendar.h +25 -0
- package/native/appkit/calendar.mm +660 -0
- package/native/appkit/camera-view.h +15 -0
- package/native/appkit/camera-view.mm +130 -0
- package/native/appkit/camera.h +39 -0
- package/native/appkit/camera.mm +362 -0
- package/native/appkit/cg-canvas.h +17 -0
- package/native/appkit/cg-canvas.mm +518 -0
- package/native/appkit/component-view.h +17 -0
- package/native/appkit/crypto.h +19 -0
- package/native/appkit/crypto.mm +78 -0
- package/native/appkit/desktop-expo.h +29 -0
- package/native/appkit/desktop-expo.mm +385 -0
- package/native/appkit/expo-image-drawing.h +46 -0
- package/native/appkit/expo-image-drawing.mm +131 -0
- package/native/appkit/expo-image-view.h +15 -0
- package/native/appkit/expo-image-view.mm +182 -0
- package/native/appkit/expo-ui-renderer.h +68 -0
- package/native/appkit/expo-ui-renderer.mm +101 -0
- package/native/appkit/expo-ui-view.h +26 -0
- package/native/appkit/expo-ui-view.mm +134 -0
- package/native/appkit/expo-ui.swift +1284 -0
- package/native/appkit/file-system.h +16 -0
- package/native/appkit/file-system.mm +140 -0
- package/native/appkit/fonts.h +12 -0
- package/native/appkit/fonts.mm +48 -0
- package/native/appkit/geocoder.h +19 -0
- package/native/appkit/geocoder.mm +101 -0
- package/native/appkit/glass-effect.h +22 -0
- package/native/appkit/glass-effect.mm +63 -0
- package/native/appkit/glass-view.h +8 -0
- package/native/appkit/glass-view.mm +65 -0
- package/native/appkit/header-toolbar.h +60 -0
- package/native/appkit/header-toolbar.mm +629 -0
- package/native/appkit/host-view.h +54 -0
- package/native/appkit/host-view.mm +1200 -0
- package/native/appkit/image-manipulator.h +14 -0
- package/native/appkit/image-manipulator.mm +191 -0
- package/native/appkit/image-request.h +22 -0
- package/native/appkit/image-request.mm +62 -0
- package/native/appkit/image-view.h +8 -0
- package/native/appkit/image-view.mm +142 -0
- package/native/appkit/input-replay.h +25 -0
- package/native/appkit/input-replay.mm +418 -0
- package/native/appkit/language-model.h +14 -0
- package/native/appkit/language-model.mm +99 -0
- package/native/appkit/language-model.swift +472 -0
- package/native/appkit/local-authentication.h +15 -0
- package/native/appkit/local-authentication.mm +133 -0
- package/native/appkit/location.h +16 -0
- package/native/appkit/location.mm +214 -0
- package/native/appkit/main.mm +1515 -0
- package/native/appkit/map-view.h +15 -0
- package/native/appkit/map-view.mm +109 -0
- package/native/appkit/maps.h +35 -0
- package/native/appkit/maps.mm +343 -0
- package/native/appkit/menu-bar.h +22 -0
- package/native/appkit/menu-bar.mm +208 -0
- package/native/appkit/modal-focus.h +16 -0
- package/native/appkit/modal-focus.mm +26 -0
- package/native/appkit/modal-host-view.h +79 -0
- package/native/appkit/modal-host-view.mm +442 -0
- package/native/appkit/native-controls.h +22 -0
- package/native/appkit/native-controls.mm +431 -0
- package/native/appkit/network-state.h +12 -0
- package/native/appkit/network-state.mm +64 -0
- package/native/appkit/network.h +14 -0
- package/native/appkit/network.mm +396 -0
- package/native/appkit/notifications.h +19 -0
- package/native/appkit/notifications.mm +309 -0
- package/native/appkit/photo-library.h +33 -0
- package/native/appkit/photo-library.mm +548 -0
- package/native/appkit/pointer-buttons.h +32 -0
- package/native/appkit/presentation-label.h +15 -0
- package/native/appkit/presentation-label.mm +12 -0
- package/native/appkit/presented-screen.h +19 -0
- package/native/appkit/presented-screen.mm +181 -0
- package/native/appkit/printing.h +42 -0
- package/native/appkit/printing.mm +393 -0
- package/native/appkit/push.h +19 -0
- package/native/appkit/push.mm +81 -0
- package/native/appkit/red-box.h +26 -0
- package/native/appkit/red-box.mm +387 -0
- package/native/appkit/screen-capture.h +21 -0
- package/native/appkit/screen-capture.mm +92 -0
- package/native/appkit/scroll-view.h +13 -0
- package/native/appkit/scroll-view.mm +251 -0
- package/native/appkit/scrollers.h +25 -0
- package/native/appkit/scrollers.mm +43 -0
- package/native/appkit/search-field.h +34 -0
- package/native/appkit/search-field.mm +291 -0
- package/native/appkit/search-toolbar-item.h +19 -0
- package/native/appkit/search-toolbar-item.mm +269 -0
- package/native/appkit/share-picker.h +34 -0
- package/native/appkit/share-picker.mm +121 -0
- package/native/appkit/split-view.h +77 -0
- package/native/appkit/split-view.mm +1206 -0
- package/native/appkit/store-review.h +13 -0
- package/native/appkit/store-review.mm +28 -0
- package/native/appkit/surface-view.h +45 -0
- package/native/appkit/surface-view.mm +558 -0
- package/native/appkit/svg-view.h +17 -0
- package/native/appkit/svg-view.mm +57 -0
- package/native/appkit/symbol-drawing.h +24 -0
- package/native/appkit/symbol-drawing.mm +117 -0
- package/native/appkit/symbol-view.h +8 -0
- package/native/appkit/symbol-view.mm +33 -0
- package/native/appkit/system-access.h +34 -0
- package/native/appkit/system-access.mm +210 -0
- package/native/appkit/system.h +24 -0
- package/native/appkit/system.mm +403 -0
- package/native/appkit/test-launch.h +36 -0
- package/native/appkit/test-launch.mm +115 -0
- package/native/appkit/text-input-field.h +76 -0
- package/native/appkit/text-input-field.mm +444 -0
- package/native/appkit/text-layout.h +58 -0
- package/native/appkit/text-layout.mm +477 -0
- package/native/appkit/text-recognition.h +13 -0
- package/native/appkit/text-recognition.mm +102 -0
- package/native/appkit/tracking.h +13 -0
- package/native/appkit/tracking.mm +51 -0
- package/native/appkit/tray-and-dock.h +45 -0
- package/native/appkit/tray-and-dock.mm +130 -0
- package/native/appkit/undo.h +23 -0
- package/native/appkit/undo.mm +125 -0
- package/native/appkit/updates.h +17 -0
- package/native/appkit/updates.mm +71 -0
- package/native/appkit/video-view.h +10 -0
- package/native/appkit/video-view.mm +42 -0
- package/native/appkit/video.h +38 -0
- package/native/appkit/video.mm +404 -0
- package/native/appkit/web-browser.h +12 -0
- package/native/appkit/web-browser.mm +97 -0
- package/native/appkit/web-view-component.h +9 -0
- package/native/appkit/web-view-component.mm +56 -0
- package/native/appkit/web-view.h +31 -0
- package/native/appkit/web-view.mm +394 -0
- package/native/cmake/libraries/react-native-reanimated.cmake +45 -0
- package/native/cmake/libraries/react-native-worklets.cmake +46 -0
- package/native/cmake/libraries.cmake +109 -0
- package/native/cmake/linked-modules.cmake +112 -0
- package/native/cmake/react-native.cmake +175 -0
- package/native/cmake/runtime-dependencies.cmake +34 -0
- package/native/cmake/swift.cmake +43 -0
- package/native/cmake/third-party.cmake +74 -0
- package/native/core/include/ngn/absent-views.h +27 -0
- package/native/core/include/ngn/accessibility.h +140 -0
- package/native/core/include/ngn/address-search.h +69 -0
- package/native/core/include/ngn/animation-frames.h +39 -0
- package/native/core/include/ngn/app-info.h +52 -0
- package/native/core/include/ngn/app-menus.h +146 -0
- package/native/core/include/ngn/app-windows.h +181 -0
- package/native/core/include/ngn/apple-authentication.h +116 -0
- package/native/core/include/ngn/asset.h +28 -0
- package/native/core/include/ngn/audio.h +125 -0
- package/native/core/include/ngn/back-swipe.h +112 -0
- package/native/core/include/ngn/battery.h +48 -0
- package/native/core/include/ngn/bytes.h +59 -0
- package/native/core/include/ngn/calendar.h +180 -0
- package/native/core/include/ngn/camera.h +128 -0
- package/native/core/include/ngn/control-sizes.h +111 -0
- package/native/core/include/ngn/core-modules.h +105 -0
- package/native/core/include/ngn/crypto.h +38 -0
- package/native/core/include/ngn/default-menu.h +91 -0
- package/native/core/include/ngn/desktop-expo.h +193 -0
- package/native/core/include/ngn/desktop-modal.h +109 -0
- package/native/core/include/ngn/desktop-props.h +75 -0
- package/native/core/include/ngn/desktop-view-props.h +110 -0
- package/native/core/include/ngn/dev-server.h +33 -0
- package/native/core/include/ngn/download.h +24 -0
- package/native/core/include/ngn/expo-image.h +192 -0
- package/native/core/include/ngn/expo-ui.h +133 -0
- package/native/core/include/ngn/file-system.h +274 -0
- package/native/core/include/ngn/focus-order.h +63 -0
- package/native/core/include/ngn/fonts.h +64 -0
- package/native/core/include/ngn/gestures.h +305 -0
- package/native/core/include/ngn/glass.h +67 -0
- package/native/core/include/ngn/header-bar.h +112 -0
- package/native/core/include/ngn/host-state.h +243 -0
- package/native/core/include/ngn/image-manipulator.h +70 -0
- package/native/core/include/ngn/input-dispatcher.h +211 -0
- package/native/core/include/ngn/input-script.h +56 -0
- package/native/core/include/ngn/input.h +112 -0
- package/native/core/include/ngn/key-value-store.h +41 -0
- package/native/core/include/ngn/language-model.h +69 -0
- package/native/core/include/ngn/launch-frame.h +47 -0
- package/native/core/include/ngn/launch-settings.h +98 -0
- package/native/core/include/ngn/layout-animations.h +24 -0
- package/native/core/include/ngn/library-components.h +13 -0
- package/native/core/include/ngn/library-glue.h +53 -0
- package/native/core/include/ngn/live-regions.h +54 -0
- package/native/core/include/ngn/local-authentication.h +74 -0
- package/native/core/include/ngn/location.h +89 -0
- package/native/core/include/ngn/maps.h +226 -0
- package/native/core/include/ngn/module-registry.h +51 -0
- package/native/core/include/ngn/mounting-backend.h +96 -0
- package/native/core/include/ngn/mounting-manager.h +155 -0
- package/native/core/include/ngn/native-controls.h +161 -0
- package/native/core/include/ngn/native-module.h +109 -0
- package/native/core/include/ngn/network-state.h +44 -0
- package/native/core/include/ngn/network.h +100 -0
- package/native/core/include/ngn/notifications.h +185 -0
- package/native/core/include/ngn/photo-library.h +130 -0
- package/native/core/include/ngn/printing.h +69 -0
- package/native/core/include/ngn/processes.h +114 -0
- package/native/core/include/ngn/red-box.h +119 -0
- package/native/core/include/ngn/reload-thread.h +258 -0
- package/native/core/include/ngn/runtime.h +161 -0
- package/native/core/include/ngn/safe-areas.h +77 -0
- package/native/core/include/ngn/screen-capture.h +35 -0
- package/native/core/include/ngn/screen-presentation.h +85 -0
- package/native/core/include/ngn/screen-stacks.h +80 -0
- package/native/core/include/ngn/scrollers.h +106 -0
- package/native/core/include/ngn/sharing.h +39 -0
- package/native/core/include/ngn/spawn-rules.h +78 -0
- package/native/core/include/ngn/splash-screen.h +41 -0
- package/native/core/include/ngn/split-view.h +270 -0
- package/native/core/include/ngn/sqlite.h +38 -0
- package/native/core/include/ngn/store-review.h +26 -0
- package/native/core/include/ngn/surface-book.h +101 -0
- package/native/core/include/ngn/svg.h +393 -0
- package/native/core/include/ngn/symbol.h +90 -0
- package/native/core/include/ngn/system.h +102 -0
- package/native/core/include/ngn/tabs-host.h +171 -0
- package/native/core/include/ngn/text-input.h +125 -0
- package/native/core/include/ngn/text-measurer.h +38 -0
- package/native/core/include/ngn/text-recognition.h +55 -0
- package/native/core/include/ngn/tracking.h +34 -0
- package/native/core/include/ngn/tray-and-dock.h +107 -0
- package/native/core/include/ngn/undo.h +37 -0
- package/native/core/include/ngn/updates.h +253 -0
- package/native/core/include/ngn/video.h +126 -0
- package/native/core/include/ngn/view-box.h +126 -0
- package/native/core/include/ngn/view-transform.h +26 -0
- package/native/core/include/ngn/view-tree.h +85 -0
- package/native/core/include/ngn/web-browser.h +45 -0
- package/native/core/include/ngn/web-view.h +133 -0
- package/native/core/include/ngn/worklet-host.h +98 -0
- package/native/core/src/absent-views.cpp +41 -0
- package/native/core/src/accessibility.cpp +374 -0
- package/native/core/src/animation-frames.cpp +49 -0
- package/native/core/src/app-menus.cpp +352 -0
- package/native/core/src/app-windows.cpp +444 -0
- package/native/core/src/back-swipe.cpp +91 -0
- package/native/core/src/bytes.cpp +158 -0
- package/native/core/src/control-sizes.cpp +232 -0
- package/native/core/src/default-menu.cpp +96 -0
- package/native/core/src/desktop-modal.cpp +162 -0
- package/native/core/src/desktop-view-props.cpp +80 -0
- package/native/core/src/dev-server-helper.cpp +152 -0
- package/native/core/src/dev-server.cpp +103 -0
- package/native/core/src/download.cpp +53 -0
- package/native/core/src/expo-image.cpp +548 -0
- package/native/core/src/expo-ui.cpp +203 -0
- package/native/core/src/focus-order.cpp +441 -0
- package/native/core/src/gestures.cpp +1806 -0
- package/native/core/src/glass.cpp +49 -0
- package/native/core/src/header-bar.cpp +104 -0
- package/native/core/src/host-services.cpp +50 -0
- package/native/core/src/host-state.cpp +61 -0
- package/native/core/src/input-dispatcher.cpp +727 -0
- package/native/core/src/input-script.cpp +123 -0
- package/native/core/src/key-value-store.cpp +169 -0
- package/native/core/src/launch-settings.cpp +148 -0
- package/native/core/src/layout-animations.cpp +62 -0
- package/native/core/src/libraries/RNCWebViewSpec.cpp +80 -0
- package/native/core/src/libraries/react-native-reanimated.cpp +216 -0
- package/native/core/src/libraries/react-native-worklets.cpp +192 -0
- package/native/core/src/libraries/rnscreens.cpp +125 -0
- package/native/core/src/libraries/rnsvg.cpp +345 -0
- package/native/core/src/libraries/safeareacontext.cpp +70 -0
- package/native/core/src/libraries/worklets-idle-frames.js +70 -0
- package/native/core/src/library-glue.cpp +35 -0
- package/native/core/src/live-regions.cpp +122 -0
- package/native/core/src/module-registry.cpp +50 -0
- package/native/core/src/modules/accessibility-manager.cpp +98 -0
- package/native/core/src/modules/address-search.cpp +219 -0
- package/native/core/src/modules/alert-manager.cpp +140 -0
- package/native/core/src/modules/app-info.cpp +74 -0
- package/native/core/src/modules/app-modules.cpp +405 -0
- package/native/core/src/modules/apple-authentication.cpp +188 -0
- package/native/core/src/modules/asset.cpp +111 -0
- package/native/core/src/modules/async-storage.cpp +192 -0
- package/native/core/src/modules/audio.cpp +541 -0
- package/native/core/src/modules/battery.cpp +88 -0
- package/native/core/src/modules/calendar.cpp +457 -0
- package/native/core/src/modules/camera.cpp +221 -0
- package/native/core/src/modules/core-modules.cpp +62 -0
- package/native/core/src/modules/crypto.cpp +117 -0
- package/native/core/src/modules/desktop-expo.cpp +527 -0
- package/native/core/src/modules/file-system.cpp +941 -0
- package/native/core/src/modules/fonts.cpp +163 -0
- package/native/core/src/modules/image-loader.cpp +105 -0
- package/native/core/src/modules/image-manipulator.cpp +305 -0
- package/native/core/src/modules/language-model.cpp +195 -0
- package/native/core/src/modules/local-authentication.cpp +140 -0
- package/native/core/src/modules/location.cpp +186 -0
- package/native/core/src/modules/maps.cpp +500 -0
- package/native/core/src/modules/network-state.cpp +72 -0
- package/native/core/src/modules/networking.cpp +387 -0
- package/native/core/src/modules/notifications.cpp +549 -0
- package/native/core/src/modules/photo-library.cpp +371 -0
- package/native/core/src/modules/printing.cpp +201 -0
- package/native/core/src/modules/screen-capture.cpp +71 -0
- package/native/core/src/modules/sharing.cpp +59 -0
- package/native/core/src/modules/splash-screen.cpp +70 -0
- package/native/core/src/modules/sqlite.cpp +421 -0
- package/native/core/src/modules/store-review.cpp +23 -0
- package/native/core/src/modules/text-recognition.cpp +107 -0
- package/native/core/src/modules/tracking.cpp +46 -0
- package/native/core/src/modules/undo.cpp +59 -0
- package/native/core/src/modules/video.cpp +366 -0
- package/native/core/src/modules/web-browser.cpp +100 -0
- package/native/core/src/modules/web-socket.cpp +266 -0
- package/native/core/src/mounting-manager.cpp +380 -0
- package/native/core/src/native-controls.cpp +129 -0
- package/native/core/src/native-module.cpp +250 -0
- package/native/core/src/network.cpp +219 -0
- package/native/core/src/posix-launcher.cpp +366 -0
- package/native/core/src/processes.cpp +324 -0
- package/native/core/src/red-box-errors.js +83 -0
- package/native/core/src/red-box.cpp +306 -0
- package/native/core/src/runtime.cpp +422 -0
- package/native/core/src/safe-areas.cpp +130 -0
- package/native/core/src/screen-presentation.cpp +61 -0
- package/native/core/src/screen-stacks.cpp +254 -0
- package/native/core/src/scrollers.cpp +181 -0
- package/native/core/src/spawn-rules.cpp +292 -0
- package/native/core/src/split-view.cpp +343 -0
- package/native/core/src/svg.cpp +1883 -0
- package/native/core/src/symbol.cpp +94 -0
- package/native/core/src/tabs-host.cpp +234 -0
- package/native/core/src/text-input.cpp +126 -0
- package/native/core/src/text-layout-manager.cpp +63 -0
- package/native/core/src/tray-and-dock.cpp +154 -0
- package/native/core/src/updates.cpp +965 -0
- package/native/core/src/view-box.cpp +788 -0
- package/native/core/src/view-transform.cpp +45 -0
- package/native/core/src/view-tree.cpp +234 -0
- package/native/core/src/web-view.cpp +143 -0
- package/native/core/src/worklet-host.cpp +10 -0
- package/native/scripts/build-hermes.sh +44 -0
- package/native/scripts/build.sh +171 -0
- package/native/scripts/cache-dir.sh +18 -0
- package/native/scripts/codegen-library.sh +193 -0
- package/native/scripts/compiler-launcher.sh +17 -0
- package/native/scripts/fetch.sh +174 -0
- package/native/scripts/hermes-built.sh +10 -0
- package/native/scripts/host-dir.sh +16 -0
- package/native/scripts/macos-target.sh +8 -0
- package/native/scripts/make-prebuilt.mjs +360 -0
- package/native/scripts/prebuilt-host-cache.mjs +158 -0
- package/native/shim/openssl/sha.h +22 -0
- package/native/shim/view-platform/react/renderer/components/view/HostPlatformViewTraitsInitializer.h +37 -0
- package/package.json +70 -3
- package/prebuilt.json +33 -0
|
@@ -0,0 +1,941 @@
|
|
|
1
|
+
#include "ngn/file-system.h"
|
|
2
|
+
|
|
3
|
+
#include "ngn/bytes.h"
|
|
4
|
+
|
|
5
|
+
#include <folly/json.h>
|
|
6
|
+
|
|
7
|
+
#include <fcntl.h>
|
|
8
|
+
#include <sys/stat.h>
|
|
9
|
+
|
|
10
|
+
#include <unistd.h>
|
|
11
|
+
|
|
12
|
+
#include <algorithm>
|
|
13
|
+
#include <atomic>
|
|
14
|
+
#include <cctype>
|
|
15
|
+
#include <cerrno>
|
|
16
|
+
#include <chrono>
|
|
17
|
+
#include <cstdio>
|
|
18
|
+
#include <filesystem>
|
|
19
|
+
#include <fstream>
|
|
20
|
+
#include <map>
|
|
21
|
+
#include <sstream>
|
|
22
|
+
#include <stdexcept>
|
|
23
|
+
|
|
24
|
+
namespace fs = std::filesystem;
|
|
25
|
+
|
|
26
|
+
namespace ngn {
|
|
27
|
+
|
|
28
|
+
std::string fileUri(const std::string &path) {
|
|
29
|
+
static const char *hex = "0123456789ABCDEF";
|
|
30
|
+
std::string out = "file://";
|
|
31
|
+
for (unsigned char c : path) {
|
|
32
|
+
if (std::isalnum(c) || c == '/' || c == '-' || c == '.' || c == '_' || c == '~') {
|
|
33
|
+
out += static_cast<char>(c);
|
|
34
|
+
} else {
|
|
35
|
+
out += '%';
|
|
36
|
+
out += hex[c >> 4];
|
|
37
|
+
out += hex[c & 15];
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return out;
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
bool replaceFile(const std::string &path, const std::string &bytes) {
|
|
44
|
+
static std::atomic<unsigned> written{0};
|
|
45
|
+
auto partial = path + ".part-" + std::to_string(::getpid()) + "-" + std::to_string(++written);
|
|
46
|
+
std::error_code ignored;
|
|
47
|
+
// Created here or not at all: an entry already at the name, a link to another file included,
|
|
48
|
+
// is never opened, so nothing is written through it and the failure removes nothing.
|
|
49
|
+
int fd = ::open(partial.c_str(), O_WRONLY | O_CREAT | O_EXCL | O_CLOEXEC, 0666);
|
|
50
|
+
if (fd < 0) {
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
bool whole = true;
|
|
54
|
+
for (size_t offset = 0; offset < bytes.size();) {
|
|
55
|
+
auto n = ::write(fd, bytes.data() + offset, bytes.size() - offset);
|
|
56
|
+
if (n < 0 && errno == EINTR) {
|
|
57
|
+
continue;
|
|
58
|
+
}
|
|
59
|
+
if (n <= 0) {
|
|
60
|
+
whole = false;
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
offset += static_cast<size_t>(n);
|
|
64
|
+
}
|
|
65
|
+
if (::close(fd) != 0) {
|
|
66
|
+
whole = false;
|
|
67
|
+
}
|
|
68
|
+
if (!whole) {
|
|
69
|
+
fs::remove(partial, ignored);
|
|
70
|
+
return false;
|
|
71
|
+
}
|
|
72
|
+
std::error_code error;
|
|
73
|
+
fs::rename(partial, path, error);
|
|
74
|
+
if (error) {
|
|
75
|
+
fs::remove(partial, ignored);
|
|
76
|
+
return false;
|
|
77
|
+
}
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
std::optional<std::string> pathOfUri(const std::string &uri) {
|
|
82
|
+
constexpr std::string_view scheme = "file://";
|
|
83
|
+
if (uri.starts_with('/')) {
|
|
84
|
+
return uri;
|
|
85
|
+
}
|
|
86
|
+
if (!uri.starts_with(scheme) || uri.size() == scheme.size() || uri[scheme.size()] != '/') {
|
|
87
|
+
return std::nullopt;
|
|
88
|
+
}
|
|
89
|
+
std::string out;
|
|
90
|
+
for (size_t i = scheme.size(); i < uri.size(); i++) {
|
|
91
|
+
if (uri[i] == '%' && i + 2 < uri.size() && std::isxdigit(static_cast<unsigned char>(uri[i + 1])) &&
|
|
92
|
+
std::isxdigit(static_cast<unsigned char>(uri[i + 2]))) {
|
|
93
|
+
out += static_cast<char>(std::stoi(uri.substr(i + 1, 2), nullptr, 16));
|
|
94
|
+
i += 2;
|
|
95
|
+
} else {
|
|
96
|
+
out += uri[i];
|
|
97
|
+
}
|
|
98
|
+
}
|
|
99
|
+
return out;
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
namespace {
|
|
103
|
+
|
|
104
|
+
// The path with every symbolic link in the part that exists followed, and `.` and `..` gone.
|
|
105
|
+
std::string resolved(const std::string &path) {
|
|
106
|
+
std::error_code error;
|
|
107
|
+
auto out = fs::weakly_canonical(fs::path(path), error);
|
|
108
|
+
return error ? fs::path(path).lexically_normal().string() : out.string();
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
// The directory entry a path names: its folder with every link followed, and its last part as it
|
|
112
|
+
// is, so a final symbolic link is the link itself. Two links to one file are two entries.
|
|
113
|
+
std::string entryOf(const std::string &path) {
|
|
114
|
+
auto p = fs::path(path);
|
|
115
|
+
auto last = p.filename();
|
|
116
|
+
if (last.empty() || last == "." || last == "..") {
|
|
117
|
+
return resolved(path);
|
|
118
|
+
}
|
|
119
|
+
return (fs::path(resolved(p.parent_path().string())) / last).string();
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
bool inside(const std::string &path, const std::string &root) {
|
|
123
|
+
return path == root || (path.starts_with(root) && (root.ends_with('/') || path[root.size()] == '/'));
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
} // namespace
|
|
127
|
+
|
|
128
|
+
FileAccess::FileAccess(Folders folders, const folly::dynamic &declared) : folders_(std::move(folders)) {
|
|
129
|
+
for (const auto *own : {&folders_.cache, &folders_.document, &folders_.appData, &folders_.temp}) {
|
|
130
|
+
add(*own, Access::ReadWrite);
|
|
131
|
+
}
|
|
132
|
+
if (!declared.isArray()) {
|
|
133
|
+
return;
|
|
134
|
+
}
|
|
135
|
+
for (const auto &entry : declared) {
|
|
136
|
+
if (!entry.isObject() || !entry.count("path") || !entry["path"].isString()) {
|
|
137
|
+
continue;
|
|
138
|
+
}
|
|
139
|
+
auto path = entry["path"].asString();
|
|
140
|
+
if (path == "~" || path.starts_with("~/")) {
|
|
141
|
+
path = folders_.home + path.substr(1);
|
|
142
|
+
}
|
|
143
|
+
if (!path.starts_with('/')) {
|
|
144
|
+
continue;
|
|
145
|
+
}
|
|
146
|
+
bool write = entry.count("access") && entry["access"] == "readwrite";
|
|
147
|
+
add(path, write ? Access::ReadWrite : Access::Read);
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
void FileAccess::add(const std::string &path, Access access) {
|
|
152
|
+
if (path.empty()) {
|
|
153
|
+
return;
|
|
154
|
+
}
|
|
155
|
+
std::lock_guard lock(mutex_);
|
|
156
|
+
roots_.push_back({.path = resolved(path), .access = access});
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
void FileAccess::allowPicked(const std::string &path) {
|
|
160
|
+
add(path, Access::ReadWrite);
|
|
161
|
+
std::shared_ptr<PickStore> picks;
|
|
162
|
+
{
|
|
163
|
+
std::lock_guard lock(mutex_);
|
|
164
|
+
picks = picks_;
|
|
165
|
+
}
|
|
166
|
+
if (picks) {
|
|
167
|
+
picks->remember(resolved(path));
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
void FileAccess::keepPicks(std::shared_ptr<PickStore> store) {
|
|
172
|
+
for (const auto &path : store->restore()) {
|
|
173
|
+
add(path, Access::ReadWrite);
|
|
174
|
+
}
|
|
175
|
+
std::lock_guard lock(mutex_);
|
|
176
|
+
picks_ = std::move(store);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
FileAccess::Use FileAccess::use(const std::string &path) const {
|
|
180
|
+
std::shared_ptr<PickStore> picks;
|
|
181
|
+
{
|
|
182
|
+
std::lock_guard lock(mutex_);
|
|
183
|
+
picks = picks_;
|
|
184
|
+
}
|
|
185
|
+
if (!picks || !path.starts_with('/')) {
|
|
186
|
+
return {};
|
|
187
|
+
}
|
|
188
|
+
auto bookmark = picks->bookmarkFor(resolved(path));
|
|
189
|
+
if (!bookmark || !picks->bookmarks().startAccess(*bookmark)) {
|
|
190
|
+
return {};
|
|
191
|
+
}
|
|
192
|
+
return Use(std::move(picks), std::move(*bookmark));
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
FileRefused::FileRefused(const std::string &path, bool write)
|
|
196
|
+
: std::runtime_error(
|
|
197
|
+
write ? path + " is outside the files this app may write: its own folders, the files the user picked and the "
|
|
198
|
+
"paths its capabilities declare readwrite"
|
|
199
|
+
: path + " is outside the files this app may read: its own folders, the files the user picked, the paths "
|
|
200
|
+
"its capabilities declare and its bundle's assets") {}
|
|
201
|
+
|
|
202
|
+
std::shared_ptr<FileAccess::Use> FileAccess::readFor(const std::string &path) const {
|
|
203
|
+
if (!path.starts_with('/')) {
|
|
204
|
+
throw FileRefused(path);
|
|
205
|
+
}
|
|
206
|
+
if (accessTo(path) < Access::Read) {
|
|
207
|
+
if (folders_.bundle.empty() || !inside(resolved(path), resolved(folders_.bundle))) {
|
|
208
|
+
throw FileRefused(path);
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
return std::make_shared<Use>(use(path));
|
|
212
|
+
}
|
|
213
|
+
|
|
214
|
+
std::shared_ptr<FileAccess::Use> FileAccess::writeFor(const std::string &path) const {
|
|
215
|
+
if (!path.starts_with('/') || accessTo(path) < Access::ReadWrite) {
|
|
216
|
+
throw FileRefused(path, true);
|
|
217
|
+
}
|
|
218
|
+
return std::make_shared<Use>(use(path));
|
|
219
|
+
}
|
|
220
|
+
|
|
221
|
+
bool isFileScheme(const std::string &uri) {
|
|
222
|
+
return uri.size() >= 5 && uri[4] == ':' &&
|
|
223
|
+
std::equal(uri.begin(), uri.begin() + 4, "file", [](char a, char b) { return std::tolower(static_cast<unsigned char>(a)) == b; });
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
ReadableSource readableSource(const FileAccess *access, const std::string &uri) {
|
|
227
|
+
bool scheme = isFileScheme(uri);
|
|
228
|
+
if (!scheme && !uri.starts_with('/')) {
|
|
229
|
+
return {.uri = uri, .use = nullptr};
|
|
230
|
+
}
|
|
231
|
+
// A uri's query and fragment are not part of the file it names.
|
|
232
|
+
auto file = scheme ? "file" + uri.substr(4, uri.find_first_of("?#") - 4) : uri;
|
|
233
|
+
auto path = pathOfUri(file);
|
|
234
|
+
if (!path) {
|
|
235
|
+
throw FileRefused(uri);
|
|
236
|
+
}
|
|
237
|
+
return {.uri = fileUri(*path), .use = readableFile(access, *path)};
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
namespace {
|
|
241
|
+
std::mutex viewAccessMutex;
|
|
242
|
+
std::shared_ptr<FileAccess> viewAccess;
|
|
243
|
+
} // namespace
|
|
244
|
+
|
|
245
|
+
void useViewFileAccess(std::shared_ptr<FileAccess> access) {
|
|
246
|
+
std::lock_guard lock(viewAccessMutex);
|
|
247
|
+
viewAccess = std::move(access);
|
|
248
|
+
}
|
|
249
|
+
|
|
250
|
+
std::shared_ptr<FileAccess> viewFileAccess() {
|
|
251
|
+
std::lock_guard lock(viewAccessMutex);
|
|
252
|
+
return viewAccess;
|
|
253
|
+
}
|
|
254
|
+
|
|
255
|
+
std::shared_ptr<FileAccess::Use> readableFile(const FileAccess *access, const std::string &path) {
|
|
256
|
+
if (access == nullptr) {
|
|
257
|
+
throw FileRefused(path);
|
|
258
|
+
}
|
|
259
|
+
return access->readFor(path);
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
FileAccess::Use::Use(std::shared_ptr<PickStore> store, std::string bookmark)
|
|
263
|
+
: store_(std::move(store)), bookmark_(std::move(bookmark)) {}
|
|
264
|
+
|
|
265
|
+
FileAccess::Use::Use(Use &&other) noexcept
|
|
266
|
+
: store_(std::move(other.store_)), bookmark_(std::move(other.bookmark_)) {
|
|
267
|
+
other.store_ = nullptr;
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
FileAccess::Use &FileAccess::Use::operator=(Use &&other) noexcept {
|
|
271
|
+
if (this != &other) {
|
|
272
|
+
release();
|
|
273
|
+
store_ = std::move(other.store_);
|
|
274
|
+
bookmark_ = std::move(other.bookmark_);
|
|
275
|
+
other.store_ = nullptr;
|
|
276
|
+
}
|
|
277
|
+
return *this;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
FileAccess::Use::~Use() {
|
|
281
|
+
release();
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
void FileAccess::Use::release() {
|
|
285
|
+
if (store_) {
|
|
286
|
+
store_->bookmarks().stopAccess(bookmark_);
|
|
287
|
+
store_ = nullptr;
|
|
288
|
+
}
|
|
289
|
+
}
|
|
290
|
+
|
|
291
|
+
PickStore::PickStore(Persistence persistence, std::shared_ptr<Bookmarks> bookmarks)
|
|
292
|
+
: persistence_(std::move(persistence)), bookmarks_(std::move(bookmarks)) {
|
|
293
|
+
auto text = persistence_.load ? persistence_.load() : std::nullopt;
|
|
294
|
+
if (!text) {
|
|
295
|
+
return;
|
|
296
|
+
}
|
|
297
|
+
try {
|
|
298
|
+
auto json = folly::parseJson(*text);
|
|
299
|
+
if (!json.isObject() || json.getDefault("version", 0) != 1 || !json.getDefault("picks").isArray()) {
|
|
300
|
+
return;
|
|
301
|
+
}
|
|
302
|
+
for (const auto &pick : json["picks"]) {
|
|
303
|
+
if (!pick.isObject() || !pick.getDefault("path").isString() || !pick.getDefault("bookmark").isString()) {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
if (auto bookmark = base64Decode(pick["bookmark"].asString())) {
|
|
307
|
+
picks_.push_back({.path = pick["path"].asString(), .bookmark = std::move(*bookmark)});
|
|
308
|
+
}
|
|
309
|
+
}
|
|
310
|
+
} catch (const std::exception &) {
|
|
311
|
+
// A store that does not parse holds nothing.
|
|
312
|
+
}
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
std::vector<std::string> PickStore::restore() {
|
|
316
|
+
std::lock_guard lock(mutex_);
|
|
317
|
+
bool changed = false;
|
|
318
|
+
std::vector<Pick> kept;
|
|
319
|
+
for (auto &pick : picks_) {
|
|
320
|
+
auto resolved = bookmarks_->resolve(pick.bookmark);
|
|
321
|
+
if (!resolved) {
|
|
322
|
+
changed = true;
|
|
323
|
+
continue;
|
|
324
|
+
}
|
|
325
|
+
if (resolved->stale) {
|
|
326
|
+
// Made again where it points now; one that cannot be made as it was keeps the stale bookmark,
|
|
327
|
+
// which still resolves.
|
|
328
|
+
if (auto fresh = bookmarks_->renew(pick.bookmark, resolved->path)) {
|
|
329
|
+
pick.bookmark = std::move(*fresh);
|
|
330
|
+
}
|
|
331
|
+
changed = true;
|
|
332
|
+
}
|
|
333
|
+
if (resolved->path != pick.path) {
|
|
334
|
+
pick.path = resolved->path;
|
|
335
|
+
changed = true;
|
|
336
|
+
}
|
|
337
|
+
kept.push_back(std::move(pick));
|
|
338
|
+
}
|
|
339
|
+
picks_ = std::move(kept);
|
|
340
|
+
if (changed) {
|
|
341
|
+
save();
|
|
342
|
+
}
|
|
343
|
+
std::vector<std::string> paths;
|
|
344
|
+
for (const auto &pick : picks_) {
|
|
345
|
+
paths.push_back(pick.path);
|
|
346
|
+
}
|
|
347
|
+
return paths;
|
|
348
|
+
}
|
|
349
|
+
|
|
350
|
+
void PickStore::remember(const std::string &path) {
|
|
351
|
+
auto bookmark = bookmarks_->make(path);
|
|
352
|
+
if (!bookmark) {
|
|
353
|
+
return;
|
|
354
|
+
}
|
|
355
|
+
std::lock_guard lock(mutex_);
|
|
356
|
+
auto it = std::find_if(picks_.begin(), picks_.end(), [&](const Pick &pick) { return pick.path == path; });
|
|
357
|
+
if (it != picks_.end()) {
|
|
358
|
+
it->bookmark = std::move(*bookmark);
|
|
359
|
+
} else {
|
|
360
|
+
picks_.push_back({.path = path, .bookmark = std::move(*bookmark)});
|
|
361
|
+
}
|
|
362
|
+
save();
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
std::optional<std::string> PickStore::bookmarkFor(const std::string &path) const {
|
|
366
|
+
std::lock_guard lock(mutex_);
|
|
367
|
+
const Pick *best = nullptr;
|
|
368
|
+
for (const auto &pick : picks_) {
|
|
369
|
+
if (inside(path, pick.path) && (!best || pick.path.size() > best->path.size())) {
|
|
370
|
+
best = &pick;
|
|
371
|
+
}
|
|
372
|
+
}
|
|
373
|
+
return best ? std::optional<std::string>(best->bookmark) : std::nullopt;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
void PickStore::save() {
|
|
377
|
+
if (!persistence_.save) {
|
|
378
|
+
return;
|
|
379
|
+
}
|
|
380
|
+
folly::dynamic picks = folly::dynamic::array();
|
|
381
|
+
for (const auto &pick : picks_) {
|
|
382
|
+
picks.push_back(folly::dynamic::object("path", pick.path)("bookmark", base64Encode(pick.bookmark)));
|
|
383
|
+
}
|
|
384
|
+
persistence_.save(folly::toJson(folly::dynamic::object("version", 1)("picks", std::move(picks))));
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
FileAccess::Access FileAccess::accessTo(const std::string &path) const {
|
|
388
|
+
if (!path.starts_with('/')) {
|
|
389
|
+
return Access::None;
|
|
390
|
+
}
|
|
391
|
+
auto target = resolved(path);
|
|
392
|
+
Access best = Access::None;
|
|
393
|
+
std::lock_guard lock(mutex_);
|
|
394
|
+
for (const auto &root : roots_) {
|
|
395
|
+
if (inside(target, root.path) && root.access > best) {
|
|
396
|
+
best = root.access;
|
|
397
|
+
}
|
|
398
|
+
}
|
|
399
|
+
return best;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
FileChanges::FileChanges(std::string watched, const Exists &exists) : watched_(std::move(watched)) {
|
|
403
|
+
std::error_code error;
|
|
404
|
+
folder_ = fs::is_directory(watched_, error);
|
|
405
|
+
auto here = exists(watched_);
|
|
406
|
+
if (here.value_or(false)) {
|
|
407
|
+
seen_.insert(watched_);
|
|
408
|
+
}
|
|
409
|
+
listed_ = here.has_value() && (!folder_ || list(seen_));
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
bool FileChanges::list(std::set<std::string> &into) const {
|
|
413
|
+
// Advanced with an error code: a range-for's ++ throws on an I/O error mid-listing.
|
|
414
|
+
std::error_code error;
|
|
415
|
+
fs::directory_iterator it(watched_, error);
|
|
416
|
+
for (; !error && it != fs::directory_iterator(); it.increment(error)) {
|
|
417
|
+
into.insert(it->path().string());
|
|
418
|
+
}
|
|
419
|
+
return !error;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
bool FileChanges::concerns(const std::string &path) const {
|
|
423
|
+
return path == watched_ || (folder_ && fs::path(path).parent_path() == fs::path(watched_));
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
namespace {
|
|
427
|
+
folly::dynamic change(const char *type, const std::string &path) {
|
|
428
|
+
return folly::dynamic::object("type", type)("uri", fileUri(path));
|
|
429
|
+
}
|
|
430
|
+
} // namespace
|
|
431
|
+
|
|
432
|
+
void FileChanges::rescan(folly::dynamic &out, const Exists &exists) {
|
|
433
|
+
std::set<std::string> now;
|
|
434
|
+
auto known = exists(watched_);
|
|
435
|
+
if (!known) {
|
|
436
|
+
rescanOwed_ = true;
|
|
437
|
+
return;
|
|
438
|
+
}
|
|
439
|
+
bool here = *known;
|
|
440
|
+
if (here) {
|
|
441
|
+
now.insert(watched_);
|
|
442
|
+
}
|
|
443
|
+
// A folder not listed keeps what was seen, and is listed again at the next event, unless it is
|
|
444
|
+
// confirmed gone (it went after the check above) or no longer a folder (a file took its place):
|
|
445
|
+
// either took its entries with it. A type that cannot be read (an ancestor no longer searchable)
|
|
446
|
+
// confirms nothing.
|
|
447
|
+
rescanOwed_ = false;
|
|
448
|
+
if (folder_ && here && !list(now)) {
|
|
449
|
+
std::error_code error;
|
|
450
|
+
// The entry itself decides whether it went; a link left in its place (even one leading
|
|
451
|
+
// nowhere) keeps it, with its old entries gone.
|
|
452
|
+
auto entry = fs::symlink_status(watched_, error);
|
|
453
|
+
std::error_code targetError;
|
|
454
|
+
auto target = fs::status(watched_, targetError);
|
|
455
|
+
if (entry.type() == fs::file_type::not_found) {
|
|
456
|
+
now.clear();
|
|
457
|
+
} else if (!error && (target.type() == fs::file_type::not_found ||
|
|
458
|
+
(!targetError && !fs::is_directory(target)))) {
|
|
459
|
+
now = {watched_};
|
|
460
|
+
} else {
|
|
461
|
+
rescanOwed_ = true;
|
|
462
|
+
return;
|
|
463
|
+
}
|
|
464
|
+
}
|
|
465
|
+
for (auto it = seen_.begin(); it != seen_.end();) {
|
|
466
|
+
if (concerns(*it) && !now.contains(*it)) {
|
|
467
|
+
out.push_back(change("deleted", *it));
|
|
468
|
+
it = seen_.erase(it);
|
|
469
|
+
} else {
|
|
470
|
+
++it;
|
|
471
|
+
}
|
|
472
|
+
}
|
|
473
|
+
for (const auto &path : now) {
|
|
474
|
+
if (seen_.insert(path).second) {
|
|
475
|
+
out.push_back(change("created", path));
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
folly::dynamic FileChanges::changes(const std::vector<FileWatcher::Event> &events, const Exists &exists) {
|
|
481
|
+
folly::dynamic out = folly::dynamic::array();
|
|
482
|
+
if (rescanOwed_) {
|
|
483
|
+
rescan(out, exists);
|
|
484
|
+
}
|
|
485
|
+
for (size_t i = 0; i < events.size(); i++) {
|
|
486
|
+
const auto &event = events[i];
|
|
487
|
+
if (event.rescan) {
|
|
488
|
+
rescan(out, exists);
|
|
489
|
+
continue;
|
|
490
|
+
}
|
|
491
|
+
auto found = exists(event.path);
|
|
492
|
+
// FSEvents reports a rename as two events in a row: the old path, then the new one.
|
|
493
|
+
auto foundTo = found == false && event.renamed && i + 1 < events.size() && events[i + 1].renamed
|
|
494
|
+
? exists(events[i + 1].path)
|
|
495
|
+
: std::optional<bool>(false);
|
|
496
|
+
if (!found || !foundTo) {
|
|
497
|
+
// Unknown, not gone: the listing owed next settles what came and went.
|
|
498
|
+
rescanOwed_ = true;
|
|
499
|
+
continue;
|
|
500
|
+
}
|
|
501
|
+
bool here = *found;
|
|
502
|
+
if (*foundTo) {
|
|
503
|
+
const auto &to = events[i + 1].path;
|
|
504
|
+
i++;
|
|
505
|
+
seen_.erase(event.path);
|
|
506
|
+
bool known = seen_.contains(to);
|
|
507
|
+
seen_.insert(to);
|
|
508
|
+
if (concerns(event.path)) {
|
|
509
|
+
auto renamed = change("renamed", event.path);
|
|
510
|
+
renamed["newUri"] = fileUri(to);
|
|
511
|
+
out.push_back(std::move(renamed));
|
|
512
|
+
} else if (concerns(to)) {
|
|
513
|
+
out.push_back(change(known ? "modified" : "created", to));
|
|
514
|
+
}
|
|
515
|
+
continue;
|
|
516
|
+
}
|
|
517
|
+
bool known = seen_.contains(event.path);
|
|
518
|
+
if (here) {
|
|
519
|
+
seen_.insert(event.path);
|
|
520
|
+
} else {
|
|
521
|
+
seen_.erase(event.path);
|
|
522
|
+
}
|
|
523
|
+
if (!concerns(event.path)) {
|
|
524
|
+
continue;
|
|
525
|
+
}
|
|
526
|
+
if (!here) {
|
|
527
|
+
out.push_back(change("deleted", event.path));
|
|
528
|
+
} else if (!known) {
|
|
529
|
+
out.push_back(change("created", event.path));
|
|
530
|
+
} else {
|
|
531
|
+
out.push_back(change("modified", event.path));
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return out;
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
namespace {
|
|
538
|
+
|
|
539
|
+
struct FileError : std::runtime_error {
|
|
540
|
+
using std::runtime_error::runtime_error;
|
|
541
|
+
};
|
|
542
|
+
|
|
543
|
+
std::string stringArg(const folly::dynamic &args, size_t index) {
|
|
544
|
+
return args.size() > index && args[index].isString() ? args[index].asString() : std::string{};
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
const folly::dynamic &option(const folly::dynamic &args, size_t index, const char *key) {
|
|
548
|
+
static const folly::dynamic none = nullptr;
|
|
549
|
+
if (args.size() > index && args[index].isObject()) {
|
|
550
|
+
if (const auto *value = args[index].get_ptr(key)) {
|
|
551
|
+
return *value;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
return none;
|
|
555
|
+
}
|
|
556
|
+
|
|
557
|
+
bool flagOption(const folly::dynamic &args, size_t index, const char *key) {
|
|
558
|
+
const auto &value = option(args, index, key);
|
|
559
|
+
return value.isBool() && value.asBool();
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
int64_t millisSince1970(const struct timespec &time) {
|
|
563
|
+
return static_cast<int64_t>(time.tv_sec) * 1000 + time.tv_nsec / 1000000;
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
folly::dynamic statOf(const std::string &uri, const std::string &path) {
|
|
567
|
+
struct stat info {};
|
|
568
|
+
if (::stat(path.c_str(), &info) != 0) {
|
|
569
|
+
return nullptr;
|
|
570
|
+
}
|
|
571
|
+
bool directory = S_ISDIR(info.st_mode);
|
|
572
|
+
#if defined(__APPLE__)
|
|
573
|
+
folly::dynamic modified = millisSince1970(info.st_mtimespec);
|
|
574
|
+
folly::dynamic created = millisSince1970(info.st_birthtimespec);
|
|
575
|
+
#else
|
|
576
|
+
folly::dynamic modified = millisSince1970(info.st_mtim);
|
|
577
|
+
folly::dynamic created = nullptr;
|
|
578
|
+
#endif
|
|
579
|
+
return folly::dynamic::object("uri", uri)("directory", directory)(
|
|
580
|
+
"size", directory ? int64_t{0} : static_cast<int64_t>(info.st_size))("modified", modified)("created", created);
|
|
581
|
+
}
|
|
582
|
+
|
|
583
|
+
std::string readAll(const std::string &path) {
|
|
584
|
+
std::ifstream in(path, std::ios::binary);
|
|
585
|
+
if (!in) {
|
|
586
|
+
throw FileError("The file could not be read: " + path);
|
|
587
|
+
}
|
|
588
|
+
std::ostringstream bytes;
|
|
589
|
+
bytes << in.rdbuf();
|
|
590
|
+
return bytes.str();
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
// fs::copy's recursive copy, with each file it writes checked against `access` on its own: a
|
|
594
|
+
// symbolic link inside the folder is copied as a link, never followed out of reach, and a link
|
|
595
|
+
// already at a destination is never written through to a file the app may not write.
|
|
596
|
+
void copyEntry(const FileAccess &access, const fs::path &from, const fs::path &to, bool top) {
|
|
597
|
+
auto status = top ? fs::status(from) : fs::symlink_status(from);
|
|
598
|
+
auto writable = [&](const fs::path &path) {
|
|
599
|
+
if (access.accessTo(path.string()) < FileAccess::Access::ReadWrite) {
|
|
600
|
+
throw FileError(path.string() + " is outside the folders the app can write");
|
|
601
|
+
}
|
|
602
|
+
};
|
|
603
|
+
if (fs::is_symlink(status)) {
|
|
604
|
+
if (fs::is_symlink(fs::symlink_status(to)) || fs::exists(to)) {
|
|
605
|
+
fs::remove(to);
|
|
606
|
+
}
|
|
607
|
+
fs::copy_symlink(from, to);
|
|
608
|
+
} else if (fs::is_directory(status)) {
|
|
609
|
+
writable(to);
|
|
610
|
+
if (!fs::exists(to)) {
|
|
611
|
+
fs::create_directory(to, from);
|
|
612
|
+
}
|
|
613
|
+
for (const auto &entry : fs::directory_iterator(from)) {
|
|
614
|
+
copyEntry(access, entry.path(), to / entry.path().filename(), false);
|
|
615
|
+
}
|
|
616
|
+
} else if (fs::is_regular_file(status)) {
|
|
617
|
+
writable(to);
|
|
618
|
+
fs::copy_file(from, to, fs::copy_options::overwrite_existing);
|
|
619
|
+
}
|
|
620
|
+
}
|
|
621
|
+
|
|
622
|
+
void copyWithin(const FileAccess &access, const std::string &from, const std::string &to) {
|
|
623
|
+
// A file onto a folder lands in it, as fs::copy puts it.
|
|
624
|
+
fs::path target = to;
|
|
625
|
+
if (!fs::is_directory(from) && fs::is_directory(to)) {
|
|
626
|
+
target /= fs::path(from).filename();
|
|
627
|
+
}
|
|
628
|
+
// A folder copied into itself would list each copy it makes, nesting until a path is too long.
|
|
629
|
+
if (fs::is_directory(from)) {
|
|
630
|
+
auto source = fs::weakly_canonical(from);
|
|
631
|
+
auto destination = fs::weakly_canonical(target);
|
|
632
|
+
if (std::mismatch(source.begin(), source.end(), destination.begin(), destination.end()).first == source.end()) {
|
|
633
|
+
throw FileError(from + " cannot be copied into itself");
|
|
634
|
+
}
|
|
635
|
+
}
|
|
636
|
+
copyEntry(access, from, target, true);
|
|
637
|
+
}
|
|
638
|
+
|
|
639
|
+
// The open watches and where their changes go, outliving the module for a callback in flight.
|
|
640
|
+
struct WatchSink {
|
|
641
|
+
std::mutex mutex;
|
|
642
|
+
bool open{true};
|
|
643
|
+
std::function<void(folly::dynamic)> emit;
|
|
644
|
+
};
|
|
645
|
+
|
|
646
|
+
} // namespace
|
|
647
|
+
|
|
648
|
+
std::shared_ptr<NativeModule> makeFileSystemModule(std::shared_ptr<FileAccess> access, std::shared_ptr<FileWatcher> watcher) {
|
|
649
|
+
struct FileSystem : NativeModule {
|
|
650
|
+
std::shared_ptr<FileWatcher> watcher;
|
|
651
|
+
std::shared_ptr<WatchSink> sink = std::make_shared<WatchSink>();
|
|
652
|
+
std::mutex watchesMutex;
|
|
653
|
+
std::vector<int> watches;
|
|
654
|
+
// Each watch's pick, open until it stops.
|
|
655
|
+
std::map<int, std::shared_ptr<FileAccess::Use>> uses;
|
|
656
|
+
|
|
657
|
+
FileSystem(std::shared_ptr<FileAccess> access, std::shared_ptr<FileWatcher> fileWatcher)
|
|
658
|
+
: NativeModule("NgNativeFileSystem"), watcher(std::move(fileWatcher)) {
|
|
659
|
+
sink->emit = [this](folly::dynamic body) { emit("ngNativeFileSystem", std::move(body)); };
|
|
660
|
+
|
|
661
|
+
// The path a uri names, when the app may do `needed` there.
|
|
662
|
+
auto reach = [access](const std::string &uri, FileAccess::Access needed) {
|
|
663
|
+
auto path = pathOfUri(uri);
|
|
664
|
+
if (!path) {
|
|
665
|
+
throw FileError("Not a file uri or an absolute path: " + uri);
|
|
666
|
+
}
|
|
667
|
+
// No trailing slash, so a folder's path compares equal however its uri was written. The
|
|
668
|
+
// path checked is the one handed on: `..` after a symbolic link names another file once
|
|
669
|
+
// it is taken away, so the check never sees a path the call does not use.
|
|
670
|
+
auto normal = fs::path(*path).lexically_normal().string();
|
|
671
|
+
if (normal.size() > 1 && normal.ends_with('/')) {
|
|
672
|
+
normal.pop_back();
|
|
673
|
+
}
|
|
674
|
+
// And the path handed on is the one the OS would use: a `..` after a symbolic link climbs
|
|
675
|
+
// out of where the link leads, not out of the link, so a path where the two differ is
|
|
676
|
+
// refused rather than acted on as another file.
|
|
677
|
+
auto parts = fs::path(*path);
|
|
678
|
+
if (std::find(parts.begin(), parts.end(), "..") != parts.end() && entryOf(*path) != entryOf(normal)) {
|
|
679
|
+
throw FileError(*path +
|
|
680
|
+
" climbs out of a symbolic link with .., which names another file: use the path it leads to.");
|
|
681
|
+
}
|
|
682
|
+
auto had = access->accessTo(normal);
|
|
683
|
+
if (had < needed) {
|
|
684
|
+
throw FileError(
|
|
685
|
+
*path +
|
|
686
|
+
(had == FileAccess::Access::Read
|
|
687
|
+
? " can only be read: declare it with access 'readwrite' in the @ng-native/desktop plugin's capabilities.paths."
|
|
688
|
+
: " is outside the folders the app can reach: declare it in the @ng-native/desktop plugin's capabilities.paths."));
|
|
689
|
+
}
|
|
690
|
+
return normal;
|
|
691
|
+
};
|
|
692
|
+
constexpr auto Read = FileAccess::Access::Read;
|
|
693
|
+
constexpr auto Write = FileAccess::Access::ReadWrite;
|
|
694
|
+
|
|
695
|
+
auto paths = [access] {
|
|
696
|
+
const auto &f = access->folders();
|
|
697
|
+
folly::dynamic out = folly::dynamic::object();
|
|
698
|
+
for (const auto &[name, path] : std::vector<std::pair<const char *, std::string>>{
|
|
699
|
+
{"cache", f.cache}, {"document", f.document}, {"appData", f.appData}, {"temp", f.temp},
|
|
700
|
+
{"home", f.home}, {"desktop", f.desktop}, {"downloads", f.downloads}}) {
|
|
701
|
+
if (!path.empty() && access->accessTo(path) != FileAccess::Access::None) {
|
|
702
|
+
out[name] = fileUri(path) + "/";
|
|
703
|
+
}
|
|
704
|
+
}
|
|
705
|
+
return out;
|
|
706
|
+
};
|
|
707
|
+
property("paths", paths);
|
|
708
|
+
property("documentDirectory", [access] { return folly::dynamic(fileUri(access->folders().document) + "/"); });
|
|
709
|
+
property("cacheDirectory", [access] { return folly::dynamic(fileUri(access->folders().cache) + "/"); });
|
|
710
|
+
constants([paths, access] {
|
|
711
|
+
return folly::dynamic::object("paths", paths())("documentDirectory", fileUri(access->folders().document) + "/")(
|
|
712
|
+
"cacheDirectory", fileUri(access->folders().cache) + "/");
|
|
713
|
+
});
|
|
714
|
+
|
|
715
|
+
// A function that throws its failure, as a promise under `name` and as a synchronous call
|
|
716
|
+
// under the name with `Async` (or nothing) swapped for `Sync`, which throws instead.
|
|
717
|
+
auto answer = [this](const std::string &name, std::function<folly::dynamic(const folly::dynamic &)> fn) {
|
|
718
|
+
auto base = name.ends_with("Async") ? name.substr(0, name.size() - 5) : name;
|
|
719
|
+
sync(base + "Sync", fn);
|
|
720
|
+
promise(name, [fn = std::move(fn)](const folly::dynamic &args, JsPromise promise) {
|
|
721
|
+
folly::dynamic value;
|
|
722
|
+
try {
|
|
723
|
+
value = fn(args);
|
|
724
|
+
} catch (const std::exception &error) {
|
|
725
|
+
promise.reject(error.what());
|
|
726
|
+
return;
|
|
727
|
+
}
|
|
728
|
+
promise.resolve(std::move(value));
|
|
729
|
+
});
|
|
730
|
+
};
|
|
731
|
+
|
|
732
|
+
answer("readdir", [reach, access](const folly::dynamic &args) {
|
|
733
|
+
auto path = reach(stringArg(args, 0), Read);
|
|
734
|
+
auto use = access->use(path);
|
|
735
|
+
if (!fs::is_directory(path)) {
|
|
736
|
+
throw FileError("Not a folder: " + path);
|
|
737
|
+
}
|
|
738
|
+
std::vector<fs::directory_entry> entries(fs::directory_iterator(path), fs::directory_iterator{});
|
|
739
|
+
std::sort(entries.begin(), entries.end(), [](const auto &a, const auto &b) { return a.path().filename() < b.path().filename(); });
|
|
740
|
+
folly::dynamic out = folly::dynamic::array();
|
|
741
|
+
for (const auto &entry : entries) {
|
|
742
|
+
out.push_back(folly::dynamic::object("name", entry.path().filename().string())("uri", fileUri(entry.path().string()))(
|
|
743
|
+
"directory", entry.is_directory()));
|
|
744
|
+
}
|
|
745
|
+
return out;
|
|
746
|
+
});
|
|
747
|
+
answer("stat", [reach, access](const folly::dynamic &args) {
|
|
748
|
+
auto uri = stringArg(args, 0);
|
|
749
|
+
auto path = reach(uri, Read);
|
|
750
|
+
auto use = access->use(path);
|
|
751
|
+
return statOf(uri, path);
|
|
752
|
+
});
|
|
753
|
+
answer("readAsStringAsync", [reach, access](const folly::dynamic &args) {
|
|
754
|
+
auto path = reach(stringArg(args, 0), Read);
|
|
755
|
+
auto use = access->use(path);
|
|
756
|
+
auto bytes = readAll(path);
|
|
757
|
+
return folly::dynamic(option(args, 1, "encoding") == "base64" ? base64Encode(bytes) : bytes);
|
|
758
|
+
});
|
|
759
|
+
answer("writeAsStringAsync", [reach, access](const folly::dynamic &args) {
|
|
760
|
+
auto path = reach(stringArg(args, 0), Write);
|
|
761
|
+
auto use = access->use(path);
|
|
762
|
+
std::string bytes = stringArg(args, 1);
|
|
763
|
+
if (option(args, 2, "encoding") == "base64") {
|
|
764
|
+
auto decoded = base64Decode(bytes);
|
|
765
|
+
if (!decoded) {
|
|
766
|
+
throw FileError("The contents are not base64");
|
|
767
|
+
}
|
|
768
|
+
bytes = std::move(*decoded);
|
|
769
|
+
}
|
|
770
|
+
std::ofstream out(path, std::ios::binary | (flagOption(args, 2, "append") ? std::ios::app : std::ios::trunc));
|
|
771
|
+
out.write(bytes.data(), static_cast<std::streamsize>(bytes.size()));
|
|
772
|
+
// Closed first, so a flush that fails is reported rather than lost in the destructor.
|
|
773
|
+
out.close();
|
|
774
|
+
if (!out) {
|
|
775
|
+
throw FileError("The file could not be written: " + path);
|
|
776
|
+
}
|
|
777
|
+
return folly::dynamic(nullptr);
|
|
778
|
+
});
|
|
779
|
+
answer("deleteAsync", [reach, access](const folly::dynamic &args) {
|
|
780
|
+
auto path = reach(stringArg(args, 0), Write);
|
|
781
|
+
auto use = access->use(path);
|
|
782
|
+
if (!fs::exists(fs::symlink_status(path))) {
|
|
783
|
+
if (flagOption(args, 1, "idempotent")) {
|
|
784
|
+
return folly::dynamic(nullptr);
|
|
785
|
+
}
|
|
786
|
+
throw FileError("Nothing to delete at " + path);
|
|
787
|
+
}
|
|
788
|
+
fs::remove_all(path);
|
|
789
|
+
return folly::dynamic(nullptr);
|
|
790
|
+
});
|
|
791
|
+
answer("makeDirectoryAsync", [reach, access](const folly::dynamic &args) {
|
|
792
|
+
auto path = reach(stringArg(args, 0), Write);
|
|
793
|
+
auto use = access->use(path);
|
|
794
|
+
if (flagOption(args, 1, "intermediates")) {
|
|
795
|
+
fs::create_directories(path);
|
|
796
|
+
} else if (!fs::create_directory(path)) {
|
|
797
|
+
throw FileError("The folder is there already: " + path);
|
|
798
|
+
}
|
|
799
|
+
return folly::dynamic(nullptr);
|
|
800
|
+
});
|
|
801
|
+
answer("moveAsync", [reach, access](const folly::dynamic &args) {
|
|
802
|
+
auto from = reach(option(args, 0, "from").isString() ? option(args, 0, "from").asString() : "", Write);
|
|
803
|
+
auto to = reach(option(args, 0, "to").isString() ? option(args, 0, "to").asString() : "", Write);
|
|
804
|
+
auto useFrom = access->use(from);
|
|
805
|
+
auto useTo = access->use(to);
|
|
806
|
+
fs::rename(from, to);
|
|
807
|
+
return folly::dynamic(nullptr);
|
|
808
|
+
});
|
|
809
|
+
answer("copyAsync", [reach, access](const folly::dynamic &args) {
|
|
810
|
+
auto from = reach(option(args, 0, "from").isString() ? option(args, 0, "from").asString() : "", Read);
|
|
811
|
+
auto to = reach(option(args, 0, "to").isString() ? option(args, 0, "to").asString() : "", Write);
|
|
812
|
+
auto useFrom = access->use(from);
|
|
813
|
+
auto useTo = access->use(to);
|
|
814
|
+
copyWithin(*access, from, to);
|
|
815
|
+
return folly::dynamic(nullptr);
|
|
816
|
+
});
|
|
817
|
+
answer("createFile", [reach, access](const folly::dynamic &args) {
|
|
818
|
+
auto folder = reach(stringArg(args, 0), Write);
|
|
819
|
+
auto use = access->use(folder);
|
|
820
|
+
auto name = stringArg(args, 1);
|
|
821
|
+
if (name.empty() || name == "." || name == ".." || name.find('/') != std::string::npos) {
|
|
822
|
+
throw FileError("Not a file name: " + name);
|
|
823
|
+
}
|
|
824
|
+
if (!fs::is_directory(folder)) {
|
|
825
|
+
throw FileError("Not a folder: " + folder);
|
|
826
|
+
}
|
|
827
|
+
auto path = (fs::path(folder) / name).string();
|
|
828
|
+
// "x" fails when the file is there, rather than emptying it.
|
|
829
|
+
FILE *file = std::fopen(path.c_str(), "wx");
|
|
830
|
+
if (!file) {
|
|
831
|
+
throw FileError("The file could not be created (it may be there already): " + path);
|
|
832
|
+
}
|
|
833
|
+
std::fclose(file);
|
|
834
|
+
return folly::dynamic::object("uri", fileUri(path));
|
|
835
|
+
});
|
|
836
|
+
|
|
837
|
+
sync("watch", [this, reach, access](const folly::dynamic &args) -> folly::dynamic {
|
|
838
|
+
std::string path;
|
|
839
|
+
try {
|
|
840
|
+
path = reach(stringArg(args, 0), Read);
|
|
841
|
+
} catch (const FileError &) {
|
|
842
|
+
return nullptr;
|
|
843
|
+
}
|
|
844
|
+
// Open for as long as the watch runs.
|
|
845
|
+
auto use = std::make_shared<FileAccess::Use>(access->use(path));
|
|
846
|
+
if (!fs::exists(path)) {
|
|
847
|
+
return nullptr;
|
|
848
|
+
}
|
|
849
|
+
// The watch's id, which start() answers only after the watcher may have sent a batch:
|
|
850
|
+
// the changes before it are held, then sent with the id, ahead of any later ones.
|
|
851
|
+
struct Id {
|
|
852
|
+
std::mutex mutex;
|
|
853
|
+
int value{0};
|
|
854
|
+
std::vector<folly::dynamic> held;
|
|
855
|
+
};
|
|
856
|
+
auto id = std::make_shared<Id>();
|
|
857
|
+
auto send = [sink = sink](folly::dynamic changes, int watch) {
|
|
858
|
+
std::lock_guard lock(sink->mutex);
|
|
859
|
+
if (!sink->open) {
|
|
860
|
+
return;
|
|
861
|
+
}
|
|
862
|
+
for (auto &change : changes) {
|
|
863
|
+
change["watch"] = watch;
|
|
864
|
+
sink->emit(std::move(change));
|
|
865
|
+
}
|
|
866
|
+
};
|
|
867
|
+
auto exists = [](const std::string &p) -> std::optional<bool> {
|
|
868
|
+
std::error_code error;
|
|
869
|
+
auto status = fs::symlink_status(p, error);
|
|
870
|
+
if (error && status.type() != fs::file_type::not_found) {
|
|
871
|
+
return std::nullopt;
|
|
872
|
+
}
|
|
873
|
+
return fs::exists(status);
|
|
874
|
+
};
|
|
875
|
+
// Called on the watcher's own serial queue, one batch at a time.
|
|
876
|
+
auto tracker = std::make_shared<FileChanges>(path, exists);
|
|
877
|
+
// Without a full listing to start from, an entry already there would later be reported
|
|
878
|
+
// created: a folder that cannot be listed cannot be watched.
|
|
879
|
+
if (!tracker->listed()) {
|
|
880
|
+
return nullptr;
|
|
881
|
+
}
|
|
882
|
+
int handle = watcher->start(path, [send, tracker, exists, id](std::vector<FileWatcher::Event> events) {
|
|
883
|
+
auto changes = tracker->changes(events, exists);
|
|
884
|
+
std::lock_guard lock(id->mutex);
|
|
885
|
+
if (id->value == 0) {
|
|
886
|
+
id->held.push_back(std::move(changes));
|
|
887
|
+
return;
|
|
888
|
+
}
|
|
889
|
+
send(std::move(changes), id->value);
|
|
890
|
+
});
|
|
891
|
+
// A watch the OS would not start would never report: there is none.
|
|
892
|
+
if (handle == 0) {
|
|
893
|
+
return nullptr;
|
|
894
|
+
}
|
|
895
|
+
{
|
|
896
|
+
std::lock_guard lock(id->mutex);
|
|
897
|
+
id->value = handle;
|
|
898
|
+
for (auto &changes : id->held) {
|
|
899
|
+
send(std::move(changes), handle);
|
|
900
|
+
}
|
|
901
|
+
id->held.clear();
|
|
902
|
+
}
|
|
903
|
+
std::lock_guard lock(watchesMutex);
|
|
904
|
+
watches.push_back(handle);
|
|
905
|
+
uses[handle] = use;
|
|
906
|
+
return handle;
|
|
907
|
+
});
|
|
908
|
+
sync("unwatch", [this](const folly::dynamic &args) -> folly::dynamic {
|
|
909
|
+
if (args.empty() || !args[0].isNumber()) {
|
|
910
|
+
return nullptr;
|
|
911
|
+
}
|
|
912
|
+
int handle = static_cast<int>(args[0].asDouble());
|
|
913
|
+
{
|
|
914
|
+
std::lock_guard lock(watchesMutex);
|
|
915
|
+
auto it = std::find(watches.begin(), watches.end(), handle);
|
|
916
|
+
if (it == watches.end()) {
|
|
917
|
+
return nullptr;
|
|
918
|
+
}
|
|
919
|
+
watches.erase(it);
|
|
920
|
+
}
|
|
921
|
+
watcher->stop(handle);
|
|
922
|
+
std::lock_guard lock(watchesMutex);
|
|
923
|
+
uses.erase(handle);
|
|
924
|
+
return nullptr;
|
|
925
|
+
});
|
|
926
|
+
}
|
|
927
|
+
|
|
928
|
+
~FileSystem() override {
|
|
929
|
+
{
|
|
930
|
+
std::lock_guard lock(sink->mutex);
|
|
931
|
+
sink->open = false;
|
|
932
|
+
}
|
|
933
|
+
for (int handle : watches) {
|
|
934
|
+
watcher->stop(handle);
|
|
935
|
+
}
|
|
936
|
+
}
|
|
937
|
+
};
|
|
938
|
+
return std::make_shared<FileSystem>(std::move(access), std::move(watcher));
|
|
939
|
+
}
|
|
940
|
+
|
|
941
|
+
} // namespace ngn
|