rclonefile 1.0.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.
package/.yarnrc.yml ADDED
@@ -0,0 +1,3 @@
1
+ nodeLinker: node-modules
2
+
3
+ yarnPath: .yarn/releases/yarn-3.4.1.cjs
package/Cargo.toml ADDED
@@ -0,0 +1,20 @@
1
+ [package]
2
+ edition = "2021"
3
+ name = "rclonefile"
4
+ version = "0.0.0"
5
+
6
+ [lib]
7
+ crate-type = ["cdylib"]
8
+
9
+ [dependencies]
10
+ # Default enable napi4 feature, see https://nodejs.org/api/n-api.html#node-api-version-matrix
11
+ napi = { version = "2.10.16", default-features = false, features = ["napi4"] }
12
+ napi-derive = "2.10.1"
13
+ libc = "0.2.139"
14
+ errno = "0.3.0"
15
+
16
+ [build-dependencies]
17
+ napi-build = "2.0.1"
18
+
19
+ [profile.release]
20
+ lto = true
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Sverre Johansen
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # rclonefile
2
+
3
+ The macOS API for creating copy on write clones of files.
4
+
5
+ This is a small wrapper around the
6
+ [clonefile](https://www.manpagez.com/man/2/clonefile/) API on macOS for cloning
7
+ files using the APFS file system.
8
+
9
+ # Usage
10
+
11
+ ## Sync API
12
+
13
+ ```js
14
+
15
+ import { cloneFileSync } from "rclonefile";
16
+
17
+ cloneFileSync("source/mario.txt", "target/mario-clone-txt");
18
+
19
+ ```
20
+
21
+ ## async/await
22
+
23
+ ```js
24
+
25
+ import { cloneFile } from "rclonefile";
26
+
27
+ await cloneFile("source/mario.txt", "target/mario-clone-txt");
28
+
29
+ ```
30
+
31
+ ## Promise
32
+
33
+ ```js
34
+
35
+ import { cloneFile } from "rclonefile";
36
+
37
+ cloneFile("source/mario.txt", "target/mario-clone-txt").then(() => {
38
+ // Success
39
+ })
40
+
41
+ ```
@@ -0,0 +1,6 @@
1
+ module.exports = {
2
+ presets: [
3
+ ["@babel/preset-env", { targets: { node: "current" } }],
4
+ "@babel/preset-typescript",
5
+ ],
6
+ };
package/build.rs ADDED
@@ -0,0 +1,5 @@
1
+ extern crate napi_build;
2
+
3
+ fn main() {
4
+ napi_build::setup();
5
+ }
package/index.d.ts ADDED
@@ -0,0 +1,12 @@
1
+ /* tslint:disable */
2
+ /* eslint-disable */
3
+
4
+ /* auto-generated by NAPI-RS */
5
+
6
+ export interface CloneFileOptions {
7
+ noFollow?: boolean
8
+ noOwnerCopy?: boolean
9
+ cloneAcl?: boolean
10
+ }
11
+ export function cloneFileSync(src: string, dst: string, options?: CloneFileOptions | undefined | null): number
12
+ export function cloneFile(src: string, dst: string, options?: CloneFileOptions | undefined | null): Promise<number>
package/index.js ADDED
@@ -0,0 +1,252 @@
1
+ const { existsSync, readFileSync } = require('fs')
2
+ const { join } = require('path')
3
+
4
+ const { platform, arch } = process
5
+
6
+ let nativeBinding = null
7
+ let localFileExisted = false
8
+ let loadError = null
9
+
10
+ function isMusl() {
11
+ // For Node 10
12
+ if (!process.report || typeof process.report.getReport !== 'function') {
13
+ try {
14
+ const lddPath = require('child_process').execSync('which ldd').toString().trim();
15
+ return readFileSync(lddPath, 'utf8').includes('musl')
16
+ } catch (e) {
17
+ return true
18
+ }
19
+ } else {
20
+ const { glibcVersionRuntime } = process.report.getReport().header
21
+ return !glibcVersionRuntime
22
+ }
23
+ }
24
+
25
+ switch (platform) {
26
+ case 'android':
27
+ switch (arch) {
28
+ case 'arm64':
29
+ localFileExisted = existsSync(join(__dirname, 'rclonefile.android-arm64.node'))
30
+ try {
31
+ if (localFileExisted) {
32
+ nativeBinding = require('./rclonefile.android-arm64.node')
33
+ } else {
34
+ nativeBinding = require('rclonefile-android-arm64')
35
+ }
36
+ } catch (e) {
37
+ loadError = e
38
+ }
39
+ break
40
+ case 'arm':
41
+ localFileExisted = existsSync(join(__dirname, 'rclonefile.android-arm-eabi.node'))
42
+ try {
43
+ if (localFileExisted) {
44
+ nativeBinding = require('./rclonefile.android-arm-eabi.node')
45
+ } else {
46
+ nativeBinding = require('rclonefile-android-arm-eabi')
47
+ }
48
+ } catch (e) {
49
+ loadError = e
50
+ }
51
+ break
52
+ default:
53
+ throw new Error(`Unsupported architecture on Android ${arch}`)
54
+ }
55
+ break
56
+ case 'win32':
57
+ switch (arch) {
58
+ case 'x64':
59
+ localFileExisted = existsSync(
60
+ join(__dirname, 'rclonefile.win32-x64-msvc.node')
61
+ )
62
+ try {
63
+ if (localFileExisted) {
64
+ nativeBinding = require('./rclonefile.win32-x64-msvc.node')
65
+ } else {
66
+ nativeBinding = require('rclonefile-win32-x64-msvc')
67
+ }
68
+ } catch (e) {
69
+ loadError = e
70
+ }
71
+ break
72
+ case 'ia32':
73
+ localFileExisted = existsSync(
74
+ join(__dirname, 'rclonefile.win32-ia32-msvc.node')
75
+ )
76
+ try {
77
+ if (localFileExisted) {
78
+ nativeBinding = require('./rclonefile.win32-ia32-msvc.node')
79
+ } else {
80
+ nativeBinding = require('rclonefile-win32-ia32-msvc')
81
+ }
82
+ } catch (e) {
83
+ loadError = e
84
+ }
85
+ break
86
+ case 'arm64':
87
+ localFileExisted = existsSync(
88
+ join(__dirname, 'rclonefile.win32-arm64-msvc.node')
89
+ )
90
+ try {
91
+ if (localFileExisted) {
92
+ nativeBinding = require('./rclonefile.win32-arm64-msvc.node')
93
+ } else {
94
+ nativeBinding = require('rclonefile-win32-arm64-msvc')
95
+ }
96
+ } catch (e) {
97
+ loadError = e
98
+ }
99
+ break
100
+ default:
101
+ throw new Error(`Unsupported architecture on Windows: ${arch}`)
102
+ }
103
+ break
104
+ case 'darwin':
105
+ localFileExisted = existsSync(join(__dirname, 'rclonefile.darwin-universal.node'))
106
+ try {
107
+ if (localFileExisted) {
108
+ nativeBinding = require('./rclonefile.darwin-universal.node')
109
+ } else {
110
+ nativeBinding = require('rclonefile-darwin-universal')
111
+ }
112
+ break
113
+ } catch {}
114
+ switch (arch) {
115
+ case 'x64':
116
+ localFileExisted = existsSync(join(__dirname, 'rclonefile.darwin-x64.node'))
117
+ try {
118
+ if (localFileExisted) {
119
+ nativeBinding = require('./rclonefile.darwin-x64.node')
120
+ } else {
121
+ nativeBinding = require('rclonefile-darwin-x64')
122
+ }
123
+ } catch (e) {
124
+ loadError = e
125
+ }
126
+ break
127
+ case 'arm64':
128
+ localFileExisted = existsSync(
129
+ join(__dirname, 'rclonefile.darwin-arm64.node')
130
+ )
131
+ try {
132
+ if (localFileExisted) {
133
+ nativeBinding = require('./rclonefile.darwin-arm64.node')
134
+ } else {
135
+ nativeBinding = require('rclonefile-darwin-arm64')
136
+ }
137
+ } catch (e) {
138
+ loadError = e
139
+ }
140
+ break
141
+ default:
142
+ throw new Error(`Unsupported architecture on macOS: ${arch}`)
143
+ }
144
+ break
145
+ case 'freebsd':
146
+ if (arch !== 'x64') {
147
+ throw new Error(`Unsupported architecture on FreeBSD: ${arch}`)
148
+ }
149
+ localFileExisted = existsSync(join(__dirname, 'rclonefile.freebsd-x64.node'))
150
+ try {
151
+ if (localFileExisted) {
152
+ nativeBinding = require('./rclonefile.freebsd-x64.node')
153
+ } else {
154
+ nativeBinding = require('rclonefile-freebsd-x64')
155
+ }
156
+ } catch (e) {
157
+ loadError = e
158
+ }
159
+ break
160
+ case 'linux':
161
+ switch (arch) {
162
+ case 'x64':
163
+ if (isMusl()) {
164
+ localFileExisted = existsSync(
165
+ join(__dirname, 'rclonefile.linux-x64-musl.node')
166
+ )
167
+ try {
168
+ if (localFileExisted) {
169
+ nativeBinding = require('./rclonefile.linux-x64-musl.node')
170
+ } else {
171
+ nativeBinding = require('rclonefile-linux-x64-musl')
172
+ }
173
+ } catch (e) {
174
+ loadError = e
175
+ }
176
+ } else {
177
+ localFileExisted = existsSync(
178
+ join(__dirname, 'rclonefile.linux-x64-gnu.node')
179
+ )
180
+ try {
181
+ if (localFileExisted) {
182
+ nativeBinding = require('./rclonefile.linux-x64-gnu.node')
183
+ } else {
184
+ nativeBinding = require('rclonefile-linux-x64-gnu')
185
+ }
186
+ } catch (e) {
187
+ loadError = e
188
+ }
189
+ }
190
+ break
191
+ case 'arm64':
192
+ if (isMusl()) {
193
+ localFileExisted = existsSync(
194
+ join(__dirname, 'rclonefile.linux-arm64-musl.node')
195
+ )
196
+ try {
197
+ if (localFileExisted) {
198
+ nativeBinding = require('./rclonefile.linux-arm64-musl.node')
199
+ } else {
200
+ nativeBinding = require('rclonefile-linux-arm64-musl')
201
+ }
202
+ } catch (e) {
203
+ loadError = e
204
+ }
205
+ } else {
206
+ localFileExisted = existsSync(
207
+ join(__dirname, 'rclonefile.linux-arm64-gnu.node')
208
+ )
209
+ try {
210
+ if (localFileExisted) {
211
+ nativeBinding = require('./rclonefile.linux-arm64-gnu.node')
212
+ } else {
213
+ nativeBinding = require('rclonefile-linux-arm64-gnu')
214
+ }
215
+ } catch (e) {
216
+ loadError = e
217
+ }
218
+ }
219
+ break
220
+ case 'arm':
221
+ localFileExisted = existsSync(
222
+ join(__dirname, 'rclonefile.linux-arm-gnueabihf.node')
223
+ )
224
+ try {
225
+ if (localFileExisted) {
226
+ nativeBinding = require('./rclonefile.linux-arm-gnueabihf.node')
227
+ } else {
228
+ nativeBinding = require('rclonefile-linux-arm-gnueabihf')
229
+ }
230
+ } catch (e) {
231
+ loadError = e
232
+ }
233
+ break
234
+ default:
235
+ throw new Error(`Unsupported architecture on Linux: ${arch}`)
236
+ }
237
+ break
238
+ default:
239
+ throw new Error(`Unsupported OS: ${platform}, architecture: ${arch}`)
240
+ }
241
+
242
+ if (!nativeBinding) {
243
+ if (loadError) {
244
+ throw loadError
245
+ }
246
+ throw new Error(`Failed to load native binding`)
247
+ }
248
+
249
+ const { cloneFileSync, cloneFile } = nativeBinding
250
+
251
+ module.exports.cloneFileSync = cloneFileSync
252
+ module.exports.cloneFile = cloneFile
package/package.json ADDED
@@ -0,0 +1,51 @@
1
+ {
2
+ "name": "rclonefile",
3
+ "version": "1.0.1",
4
+ "main": "index.js",
5
+ "types": "index.d.ts",
6
+ "napi": {
7
+ "name": "rclonefile",
8
+ "triples": {
9
+ "defaults": false,
10
+ "additional": [
11
+ "aarch64-apple-darwin",
12
+ "x86_64-apple-darwin"
13
+ ]
14
+ }
15
+ },
16
+ "license": "MIT",
17
+ "devDependencies": {
18
+ "@babel/core": "^7.20.12",
19
+ "@babel/preset-env": "^7.20.2",
20
+ "@babel/preset-typescript": "^7.18.6",
21
+ "@jest/globals": "^29.4.2",
22
+ "@napi-rs/cli": "^2.14.7",
23
+ "ava": "^5.1.1",
24
+ "babel-jest": "^29.4.2",
25
+ "jest": "^29.4.2",
26
+ "prettier": "^2.8.4",
27
+ "tempy": "^1.0.1"
28
+ },
29
+ "engines": {
30
+ "node": ">= 10"
31
+ },
32
+ "scripts": {
33
+ "artifacts": "napi artifacts",
34
+ "build": "napi build --platform --release",
35
+ "build:debug": "napi build --platform",
36
+ "prepublishOnly": "napi prepublish -t npm",
37
+ "test": "jest",
38
+ "universal": "napi universal",
39
+ "version": "napi version"
40
+ },
41
+ "packageManager": "yarn@3.4.1",
42
+ "dependencies": {
43
+ "esm": "^3.2.25"
44
+ },
45
+ "repository": "https://github.com/sverrejoh/rclonefile",
46
+ "description": "macOS API for creating copy on write clones of files",
47
+ "optionalDependencies": {
48
+ "rclonefile-darwin-arm64": "1.0.1",
49
+ "rclonefile-darwin-x64": "1.0.1"
50
+ }
51
+ }
package/src/lib.rs ADDED
@@ -0,0 +1,94 @@
1
+ #![deny(clippy::all)]
2
+
3
+ extern crate libc;
4
+ use errno::{errno, set_errno};
5
+ use libc::clonefile;
6
+ use napi::bindgen_prelude::AsyncTask;
7
+ use napi::{Env, Error, JsNumber, Result, Task};
8
+ use std::ffi::CString;
9
+
10
+ #[macro_use]
11
+ extern crate napi_derive;
12
+
13
+ #[napi(object)]
14
+ #[derive(Default, Clone, Debug)]
15
+ pub struct CloneFileOptions {
16
+ pub no_follow: Option<bool>,
17
+ pub no_owner_copy: Option<bool>,
18
+ pub clone_acl: Option<bool>,
19
+ }
20
+
21
+ const CLONE_NOFOLLOW: u32 = 1 << 0;
22
+ const CLONE_NOOWNERCOPY: u32 = 1 << 1;
23
+ const CLONE_ACL: u32 = 1 << 2;
24
+
25
+ fn flags_from_options(options: Option<CloneFileOptions>) -> u32 {
26
+ match options {
27
+ None => 0,
28
+ Some(options) => {
29
+ let CloneFileOptions {
30
+ no_follow,
31
+ no_owner_copy,
32
+ clone_acl,
33
+ } = options;
34
+
35
+ let flags = no_follow
36
+ .unwrap_or(false)
37
+ .then_some(CLONE_NOFOLLOW)
38
+ .unwrap_or(0)
39
+ | no_owner_copy
40
+ .unwrap_or(false)
41
+ .then_some(CLONE_NOOWNERCOPY)
42
+ .unwrap_or(0)
43
+ | clone_acl.unwrap_or(false).then_some(CLONE_ACL).unwrap_or(0);
44
+
45
+ flags
46
+ }
47
+ }
48
+ }
49
+
50
+ #[napi(js_name = "cloneFileSync")]
51
+ pub fn clonefile_sync(src: String, dst: String, options: Option<CloneFileOptions>) -> Result<i32> {
52
+ let src = CString::new(src)?;
53
+ let dst = CString::new(dst)?;
54
+ let flags = flags_from_options(options);
55
+
56
+ let res = unsafe { clonefile(src.as_ptr(), dst.as_ptr(), flags) };
57
+
58
+ if res == -1 {
59
+ let e = errno();
60
+ set_errno(e);
61
+ return Err(Error::from_reason(format!("{e}")));
62
+ } else {
63
+ return Ok(res);
64
+ }
65
+ }
66
+
67
+ pub struct AsyncClonefile {
68
+ src: String,
69
+ dst: String,
70
+ options: Option<CloneFileOptions>,
71
+ }
72
+
73
+ #[napi]
74
+ impl Task for AsyncClonefile {
75
+ type Output = i32;
76
+ type JsValue = JsNumber;
77
+
78
+ fn compute(&mut self) -> Result<Self::Output> {
79
+ clonefile_sync(self.src.clone(), self.dst.clone(), self.options.clone())
80
+ }
81
+
82
+ fn resolve(&mut self, env: Env, output: i32) -> Result<Self::JsValue> {
83
+ env.create_int32(output)
84
+ }
85
+ }
86
+
87
+ #[napi(js_name = "cloneFile")]
88
+ pub fn clonefile_task(
89
+ src: String,
90
+ dst: String,
91
+ options: Option<CloneFileOptions>,
92
+ ) -> AsyncTask<AsyncClonefile> {
93
+ AsyncTask::new(AsyncClonefile { src, dst, options })
94
+ }