├── .github └── workflows │ └── test.yml ├── .gitignore ├── .npmrc ├── LICENSE ├── binding.gyp ├── index.d.ts ├── index.js ├── package.json ├── readme.md ├── src ├── async.c ├── async.h ├── error.c ├── error.h ├── sync.c ├── sync.h ├── util.c ├── util.h └── xattr.c └── test └── xattr.js /.github/workflows/test.yml: -------------------------------------------------------------------------------- 1 | name: Node CI 2 | 3 | on: 4 | pull_request: 5 | branches: 6 | - master 7 | 8 | jobs: 9 | test: 10 | strategy: 11 | matrix: 12 | os: [ubuntu-latest, macOS-latest] 13 | node: [12.20.0, 14.13.1, 16.0.0] 14 | runs-on: ${{ matrix.os }} 15 | steps: 16 | - uses: actions/checkout@v2 17 | - name: Use Node.js ${{ matrix.node }} 18 | uses: actions/setup-node@v2 19 | with: 20 | node-version: ${{ matrix.node }} 21 | - name: Install dependencies 22 | run: npm install 23 | - name: Run tests 24 | run: npm test 25 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /build/ 2 | /node_modules/ 3 | /npm-debug.log 4 | -------------------------------------------------------------------------------- /.npmrc: -------------------------------------------------------------------------------- 1 | package-lock=false 2 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2013-2020 Linus Unnebäck 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. 22 | -------------------------------------------------------------------------------- /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | { 4 | "target_name": "xattr", 5 | "sources": [ 6 | "src/async.c", 7 | "src/error.c", 8 | "src/sync.c", 9 | "src/util.c", 10 | "src/xattr.c" 11 | ] 12 | } 13 | ] 14 | } 15 | -------------------------------------------------------------------------------- /index.d.ts: -------------------------------------------------------------------------------- 1 | /** 2 | * Get extended attribute `attr` from file at `path`. 3 | * 4 | * @returns a `Promise` that will resolve with the value of the attribute. 5 | */ 6 | export function getAttribute (path: string, attr: string): Promise 7 | 8 | /** 9 | * Synchronous version of `getAttribute`. 10 | */ 11 | export function getAttributeSync (path: string, attr: string): Buffer 12 | 13 | /** 14 | * Set extended attribute `attr` to `value` on file at `path`. 15 | * 16 | * @returns a `Promise` that will resolve when the value has been set. 17 | */ 18 | export function setAttribute (path: string, attr: string, value: Buffer | string): Promise 19 | 20 | /** 21 | * Synchronous version of `setAttribute`. 22 | */ 23 | export function setAttributeSync (path: string, attr: string, value: Buffer | string): void 24 | 25 | /** 26 | * Remove extended attribute `attr` on file at `path`. 27 | * 28 | * @returns a `Promise` that will resolve when the value has been removed. 29 | */ 30 | export function removeAttribute (path: string, attr: string): Promise 31 | 32 | /** 33 | * Synchronous version of `removeAttribute`. 34 | */ 35 | export function removeAttributeSync (path: string, attr: string): void 36 | 37 | /** 38 | * List all attributes on file at `path`. 39 | * 40 | * @returns a `Promise` that will resolve with an array of strings, e.g. `['user.linusu.test', 'com.apple.FinderInfo']`. 41 | */ 42 | export function listAttributes (path: string): Promise 43 | 44 | /** 45 | * Synchronous version of `listAttributes`. 46 | */ 47 | export function listAttributesSync (path: string): string[] 48 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | import { createRequire } from 'node:module' 2 | 3 | const addon = createRequire(import.meta.url)('./build/Release/xattr') 4 | 5 | function validateArgument (key, val) { 6 | switch (key) { 7 | case 'path': 8 | if (typeof val === 'string') return val 9 | throw new TypeError('`path` must be a string') 10 | case 'attr': 11 | if (typeof val === 'string') return val 12 | throw new TypeError('`attr` must be a string') 13 | case 'value': 14 | if (typeof val === 'string') return Buffer.from(val) 15 | if (Buffer.isBuffer(val)) return val 16 | throw new TypeError('`value` must be a string or buffer') 17 | default: 18 | throw new Error(`Unknown argument: ${key}`) 19 | } 20 | } 21 | 22 | /* Async methods */ 23 | 24 | export function getAttribute (path, attr) { 25 | path = validateArgument('path', path) 26 | attr = validateArgument('attr', attr) 27 | 28 | return addon.get(path, attr) 29 | } 30 | 31 | export function setAttribute (path, attr, value) { 32 | path = validateArgument('path', path) 33 | attr = validateArgument('attr', attr) 34 | value = validateArgument('value', value) 35 | 36 | return addon.set(path, attr, value) 37 | } 38 | 39 | export function listAttributes (path) { 40 | path = validateArgument('path', path) 41 | 42 | return addon.list(path) 43 | } 44 | 45 | export function removeAttribute (path, attr) { 46 | path = validateArgument('path', path) 47 | attr = validateArgument('attr', attr) 48 | 49 | return addon.remove(path, attr) 50 | } 51 | 52 | /* Sync methods */ 53 | 54 | export function getAttributeSync (path, attr) { 55 | path = validateArgument('path', path) 56 | attr = validateArgument('attr', attr) 57 | 58 | return addon.getSync(path, attr) 59 | } 60 | 61 | export function setAttributeSync (path, attr, value) { 62 | path = validateArgument('path', path) 63 | attr = validateArgument('attr', attr) 64 | value = validateArgument('value', value) 65 | 66 | return addon.setSync(path, attr, value) 67 | } 68 | 69 | export function listAttributesSync (path) { 70 | path = validateArgument('path', path) 71 | 72 | return addon.listSync(path) 73 | } 74 | 75 | export function removeAttributeSync (path, attr) { 76 | path = validateArgument('path', path) 77 | attr = validateArgument('attr', attr) 78 | 79 | return addon.removeSync(path, attr) 80 | } 81 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "fs-xattr", 3 | "version": "0.4.0", 4 | "license": "MIT", 5 | "repository": "LinusU/fs-xattr", 6 | "type": "module", 7 | "exports": "./index.js", 8 | "files": [ 9 | "binding.gyp", 10 | "index.d.ts", 11 | "index.js", 12 | "src/" 13 | ], 14 | "scripts": { 15 | "test": "standard && mocha && ts-readme-generator --check", 16 | "lint": "standard" 17 | }, 18 | "devDependencies": { 19 | "fs-temp": "^1.1.2", 20 | "mocha": "^8.4.0", 21 | "standard": "^16.0.3", 22 | "ts-readme-generator": "^0.4.3" 23 | }, 24 | "engines": { 25 | "node": "^12.20.0 || ^14.13.1 || >=16.0.0" 26 | }, 27 | "os": [ 28 | "!win32" 29 | ] 30 | } 31 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # fs-xattr 2 | 3 | Node.js module for manipulating extended attributes. 4 | 5 | There are already some libraries for this, why use `fs-xattr`? 6 | 7 | - Very useful errors 8 | - No limits on value size 9 | - Clean and easy api 10 | - Proper asynchronous implementation 11 | 12 | ## Installation 13 | 14 | ```sh 15 | npm install --save fs-xattr 16 | ``` 17 | 18 | ## Usage 19 | 20 | ```javascript 21 | import { getAttribute, setAttribute } from 'fs-xattr' 22 | 23 | await setAttribute('index.js', 'user.linusu.test', 'Hello, World!') 24 | 25 | console.log(await getAttribute('index.js', 'user.linusu.test')) 26 | //=> Hello, World! 27 | ``` 28 | 29 | ## API 30 | 31 | ### `getAttribute(path, attr)` 32 | 33 | - `path` (`string`, required) 34 | - `attr` (`string`, required) 35 | - returns `Promise` - a `Promise` that will resolve with the value of the attribute. 36 | 37 | Get extended attribute `attr` from file at `path`. 38 | 39 | ### `getAttributeSync(path, attr)` 40 | 41 | - `path` (`string`, required) 42 | - `attr` (`string`, required) 43 | - returns `Buffer` 44 | 45 | Synchronous version of `getAttribute`. 46 | 47 | ### `setAttribute(path, attr, value)` 48 | 49 | - `path` (`string`, required) 50 | - `attr` (`string`, required) 51 | - `value` (`Buffer` or `string`, required) 52 | - returns `Promise` - a `Promise` that will resolve when the value has been set. 53 | 54 | Set extended attribute `attr` to `value` on file at `path`. 55 | 56 | ### `setAttributeSync(path, attr, value)` 57 | 58 | - `path` (`string`, required) 59 | - `attr` (`string`, required) 60 | - `value` (`Buffer` or `string`, required) 61 | 62 | Synchronous version of `setAttribute`. 63 | 64 | ### `removeAttribute(path, attr)` 65 | 66 | - `path` (`string`, required) 67 | - `attr` (`string`, required) 68 | - returns `Promise` - a `Promise` that will resolve when the value has been removed. 69 | 70 | Remove extended attribute `attr` on file at `path`. 71 | 72 | ### `removeAttributeSync(path, attr)` 73 | 74 | - `path` (`string`, required) 75 | - `attr` (`string`, required) 76 | 77 | Synchronous version of `removeAttribute`. 78 | 79 | ### `listAttributes(path)` 80 | 81 | - `path` (`string`, required) 82 | - returns `Promise>` - a `Promise` that will resolve with an array of strings, e.g. `['user.linusu.test', 'com.apple.FinderInfo']`. 83 | 84 | List all attributes on file at `path`. 85 | 86 | ### `listAttributesSync(path)` 87 | 88 | - `path` (`string`, required) 89 | - returns `Array` 90 | 91 | Synchronous version of `listAttributes`. 92 | 93 | ## Namespaces 94 | 95 | For the large majority of Linux filesystem there are currently 4 supported namespaces (`user`, `trusted`, `security`, and `system`) you can use. Some other systems, like FreeBSD have only 2 (`user` and `system`). 96 | 97 | Be sure to use a namespace that is appropriate for your supported platforms. You can read more about this in [the "Extended File Attributes" Wikipedia article](https://en.wikipedia.org/wiki/Extended_file_attributes#Implementations). 98 | 99 | Using a namespace like `com.linusu.test` would work on macOS, but would give you the following error on Debian Linux: 100 | 101 | > Error \[ENOTSUP]: The file system does not support extended attributes or has the feature disabled. 102 | -------------------------------------------------------------------------------- /src/async.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "error.h" 7 | #include "util.h" 8 | 9 | #include "async.h" 10 | 11 | typedef struct { 12 | char* filename; 13 | char* attribute; 14 | napi_deferred deferred; 15 | int e; 16 | ssize_t value_length; 17 | char* value; 18 | } XattrGetData; 19 | 20 | void xattr_get_execute(napi_env env, void* _data) { 21 | XattrGetData* data = _data; 22 | 23 | #ifdef __APPLE__ 24 | data->value_length = getxattr(data->filename, data->attribute, NULL, 0, 0, 0); 25 | #else 26 | data->value_length = getxattr(data->filename, data->attribute, NULL, 0); 27 | #endif 28 | 29 | if (data->value_length == -1) { 30 | data->e = errno; 31 | return ; 32 | } 33 | 34 | data->value = malloc((size_t) data->value_length); 35 | 36 | #ifdef __APPLE__ 37 | data->value_length = getxattr(data->filename, data->attribute, data->value, (size_t) data->value_length, 0, 0); 38 | #else 39 | data->value_length = getxattr(data->filename, data->attribute, data->value, (size_t) data->value_length); 40 | #endif 41 | 42 | if (data->value_length == -1) { 43 | data->e = errno; 44 | return ; 45 | } 46 | } 47 | 48 | void xattr_get_complete(napi_env env, napi_status status, void* _data) { 49 | XattrGetData* data = _data; 50 | 51 | free(data->filename); 52 | free(data->attribute); 53 | 54 | if (data->value_length == -1) { 55 | napi_value error; 56 | assert(create_xattr_error(env, data->e, &error) == napi_ok); 57 | assert(napi_reject_deferred(env, data->deferred, error) == napi_ok); 58 | return; 59 | } 60 | 61 | napi_value buffer; 62 | assert(napi_create_buffer_copy(env, data->value_length, data->value, NULL, &buffer) == napi_ok); 63 | assert(napi_resolve_deferred(env, data->deferred, buffer) == napi_ok); 64 | 65 | free(_data); 66 | } 67 | 68 | napi_value xattr_get(napi_env env, napi_callback_info info) { 69 | size_t argc = 2; 70 | napi_value args[2]; 71 | assert(napi_get_cb_info(env, info, &argc, args, NULL, NULL) == napi_ok); 72 | 73 | XattrGetData* data = malloc(sizeof(XattrGetData)); 74 | 75 | size_t filename_length; 76 | assert(napi_get_value_string_utf8(env, args[0], NULL, 0, &filename_length) == napi_ok); 77 | data->filename = malloc(filename_length + 1); 78 | assert(napi_get_value_string_utf8(env, args[0], data->filename, filename_length + 1, NULL) == napi_ok); 79 | 80 | size_t attribute_length; 81 | assert(napi_get_value_string_utf8(env, args[1], NULL, 0, &attribute_length) == napi_ok); 82 | data->attribute = malloc(attribute_length + 1); 83 | assert(napi_get_value_string_utf8(env, args[1], data->attribute, attribute_length + 1, NULL) == napi_ok); 84 | 85 | napi_value promise; 86 | assert(napi_create_promise(env, &data->deferred, &promise) == napi_ok); 87 | 88 | napi_value work_name; 89 | assert(napi_create_string_utf8(env, "fs-xattr:get", NAPI_AUTO_LENGTH, &work_name) == napi_ok); 90 | 91 | napi_async_work work; 92 | assert(napi_create_async_work(env, NULL, work_name, xattr_get_execute, xattr_get_complete, (void*) data, &work) == napi_ok); 93 | 94 | assert(napi_queue_async_work(env, work) == napi_ok); 95 | 96 | return promise; 97 | } 98 | 99 | typedef struct { 100 | char* filename; 101 | char* attribute; 102 | napi_deferred deferred; 103 | int e; 104 | ssize_t value_length; 105 | char* value; 106 | } XattrSetData; 107 | 108 | void xattr_set_execute(napi_env env, void* _data) { 109 | XattrSetData* data = _data; 110 | 111 | #ifdef __APPLE__ 112 | int res = setxattr(data->filename, data->attribute, data->value, data->value_length, 0, 0); 113 | #else 114 | int res = setxattr(data->filename, data->attribute, data->value, data->value_length, 0); 115 | #endif 116 | 117 | if (res == -1) { 118 | data->e = errno; 119 | } else { 120 | data->e = 0; 121 | } 122 | } 123 | 124 | void xattr_set_complete(napi_env env, napi_status status, void* _data) { 125 | XattrSetData* data = _data; 126 | 127 | free(data->filename); 128 | free(data->attribute); 129 | 130 | if (data->e != 0) { 131 | napi_value error; 132 | assert(create_xattr_error(env, data->e, &error) == napi_ok); 133 | assert(napi_reject_deferred(env, data->deferred, error) == napi_ok); 134 | return; 135 | } 136 | 137 | napi_value undefined; 138 | assert(napi_get_undefined(env, &undefined) == napi_ok); 139 | assert(napi_resolve_deferred(env, data->deferred, undefined) == napi_ok); 140 | 141 | free(_data); 142 | } 143 | 144 | napi_value xattr_set(napi_env env, napi_callback_info info) { 145 | size_t argc = 3; 146 | napi_value args[3]; 147 | assert(napi_get_cb_info(env, info, &argc, args, NULL, NULL) == napi_ok); 148 | 149 | XattrSetData* data = malloc(sizeof(XattrSetData)); 150 | 151 | size_t filename_length; 152 | assert(napi_get_value_string_utf8(env, args[0], NULL, 0, &filename_length) == napi_ok); 153 | data->filename = malloc(filename_length + 1); 154 | assert(napi_get_value_string_utf8(env, args[0], data->filename, filename_length + 1, NULL) == napi_ok); 155 | 156 | size_t attribute_length; 157 | assert(napi_get_value_string_utf8(env, args[1], NULL, 0, &attribute_length) == napi_ok); 158 | data->attribute = malloc(attribute_length + 1); 159 | assert(napi_get_value_string_utf8(env, args[1], data->attribute, attribute_length + 1, NULL) == napi_ok); 160 | 161 | assert(napi_get_buffer_info(env, args[2], (void**) &data->value, (size_t*) &data->value_length) == napi_ok); 162 | 163 | napi_value promise; 164 | assert(napi_create_promise(env, &data->deferred, &promise) == napi_ok); 165 | 166 | napi_value work_name; 167 | assert(napi_create_string_utf8(env, "fs-xattr:set", NAPI_AUTO_LENGTH, &work_name) == napi_ok); 168 | 169 | napi_async_work work; 170 | assert(napi_create_async_work(env, NULL, work_name, xattr_set_execute, xattr_set_complete, (void*) data, &work) == napi_ok); 171 | 172 | assert(napi_queue_async_work(env, work) == napi_ok); 173 | 174 | return promise; 175 | } 176 | 177 | typedef struct { 178 | char* filename; 179 | napi_deferred deferred; 180 | int e; 181 | ssize_t result_length; 182 | char* result; 183 | } XattrListData; 184 | 185 | void xattr_list_execute(napi_env env, void* _data) { 186 | XattrListData* data = _data; 187 | 188 | #ifdef __APPLE__ 189 | data->result_length = listxattr(data->filename, NULL, 0, 0); 190 | #else 191 | data->result_length = listxattr(data->filename, NULL, 0); 192 | #endif 193 | 194 | if (data->result_length == -1) { 195 | data->e = errno; 196 | return ; 197 | } 198 | 199 | data->result = (char *) malloc((size_t) data->result_length); 200 | 201 | #ifdef __APPLE__ 202 | data->result_length = listxattr(data->filename, data->result, (size_t) data->result_length, 0); 203 | #else 204 | data->result_length = listxattr(data->filename, data->result, (size_t) data->result_length); 205 | #endif 206 | 207 | if (data->result_length == -1) { 208 | data->e = errno; 209 | return ; 210 | } 211 | } 212 | 213 | void xattr_list_complete(napi_env env, napi_status status, void* _data) { 214 | XattrListData* data = _data; 215 | 216 | free(data->filename); 217 | 218 | if (data->result_length == -1) { 219 | napi_value error; 220 | assert(create_xattr_error(env, data->e, &error) == napi_ok); 221 | assert(napi_reject_deferred(env, data->deferred, error) == napi_ok); 222 | return; 223 | } 224 | 225 | napi_value array; 226 | assert(split_string_array(env, data->result, (size_t) data->result_length, &array) == napi_ok); 227 | assert(napi_resolve_deferred(env, data->deferred, array) == napi_ok); 228 | 229 | free(data->result); 230 | free(_data); 231 | } 232 | 233 | napi_value xattr_list(napi_env env, napi_callback_info info) { 234 | size_t argc = 1; 235 | napi_value args[1]; 236 | assert(napi_get_cb_info(env, info, &argc, args, NULL, NULL) == napi_ok); 237 | 238 | XattrListData* data = malloc(sizeof(XattrListData)); 239 | 240 | size_t filename_length; 241 | assert(napi_get_value_string_utf8(env, args[0], NULL, 0, &filename_length) == napi_ok); 242 | data->filename = malloc(filename_length + 1); 243 | assert(napi_get_value_string_utf8(env, args[0], data->filename, filename_length + 1, NULL) == napi_ok); 244 | 245 | napi_value promise; 246 | assert(napi_create_promise(env, &data->deferred, &promise) == napi_ok); 247 | 248 | napi_value work_name; 249 | assert(napi_create_string_utf8(env, "fs-xattr:list", NAPI_AUTO_LENGTH, &work_name) == napi_ok); 250 | 251 | napi_async_work work; 252 | assert(napi_create_async_work(env, NULL, work_name, xattr_list_execute, xattr_list_complete, (void*) data, &work) == napi_ok); 253 | 254 | assert(napi_queue_async_work(env, work) == napi_ok); 255 | 256 | return promise; 257 | } 258 | 259 | typedef struct { 260 | char* filename; 261 | char* attribute; 262 | napi_deferred deferred; 263 | int e; 264 | } XattrRemoveData; 265 | 266 | void xattr_remove_execute(napi_env env, void* _data) { 267 | XattrRemoveData* data = _data; 268 | 269 | #ifdef __APPLE__ 270 | int res = removexattr(data->filename, data->attribute, 0); 271 | #else 272 | int res = removexattr(data->filename, data->attribute); 273 | #endif 274 | 275 | if (res == -1) { 276 | data->e = errno; 277 | } else { 278 | data->e = 0; 279 | } 280 | } 281 | 282 | void xattr_remove_complete(napi_env env, napi_status status, void* _data) { 283 | XattrRemoveData* data = _data; 284 | 285 | free(data->filename); 286 | free(data->attribute); 287 | 288 | if (data->e != 0) { 289 | napi_value error; 290 | assert(create_xattr_error(env, data->e, &error) == napi_ok); 291 | assert(napi_reject_deferred(env, data->deferred, error) == napi_ok); 292 | return; 293 | } 294 | 295 | napi_value undefined; 296 | assert(napi_get_undefined(env, &undefined) == napi_ok); 297 | assert(napi_resolve_deferred(env, data->deferred, undefined) == napi_ok); 298 | 299 | free(_data); 300 | } 301 | 302 | napi_value xattr_remove(napi_env env, napi_callback_info info) { 303 | size_t argc = 2; 304 | napi_value args[2]; 305 | assert(napi_get_cb_info(env, info, &argc, args, NULL, NULL) == napi_ok); 306 | 307 | XattrRemoveData* data = malloc(sizeof(XattrRemoveData)); 308 | 309 | size_t filename_length; 310 | assert(napi_get_value_string_utf8(env, args[0], NULL, 0, &filename_length) == napi_ok); 311 | data->filename = malloc(filename_length + 1); 312 | assert(napi_get_value_string_utf8(env, args[0], data->filename, filename_length + 1, NULL) == napi_ok); 313 | 314 | size_t attribute_length; 315 | assert(napi_get_value_string_utf8(env, args[1], NULL, 0, &attribute_length) == napi_ok); 316 | data->attribute = malloc(attribute_length + 1); 317 | assert(napi_get_value_string_utf8(env, args[1], data->attribute, attribute_length + 1, NULL) == napi_ok); 318 | 319 | napi_value promise; 320 | assert(napi_create_promise(env, &data->deferred, &promise) == napi_ok); 321 | 322 | napi_value work_name; 323 | assert(napi_create_string_utf8(env, "fs-xattr:remove", NAPI_AUTO_LENGTH, &work_name) == napi_ok); 324 | 325 | napi_async_work work; 326 | assert(napi_create_async_work(env, NULL, work_name, xattr_remove_execute, xattr_remove_complete, (void*) data, &work) == napi_ok); 327 | 328 | assert(napi_queue_async_work(env, work) == napi_ok); 329 | 330 | return promise; 331 | } 332 | -------------------------------------------------------------------------------- /src/async.h: -------------------------------------------------------------------------------- 1 | #ifndef LD_ASYNC_H 2 | #define LD_ASYNC_H 3 | 4 | #define NAPI_VERSION 1 5 | #include 6 | 7 | napi_value xattr_get(napi_env env, napi_callback_info info); 8 | napi_value xattr_set(napi_env env, napi_callback_info info); 9 | napi_value xattr_list(napi_env env, napi_callback_info info); 10 | napi_value xattr_remove(napi_env env, napi_callback_info info); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/error.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | 4 | #include "error.h" 5 | 6 | #ifdef __APPLE__ 7 | #define E_ENOATTR ENOATTR 8 | #define S_ENOATTR "ENOATTR" 9 | #else 10 | #define E_ENOATTR ENODATA 11 | #define S_ENOATTR "ENODATA" 12 | #endif 13 | 14 | const char* _error_description(int e) { 15 | switch (e) { 16 | case E_ENOATTR: return "The extended attribute does not exist."; 17 | case ENOTSUP: return "The file system does not support extended attributes or has the feature disabled."; 18 | case ERANGE: return "value (as indicated by size) is too small to hold the extended attribute data."; 19 | case EPERM: return "The named attribute is not permitted for this type of object."; 20 | case EINVAL: return "name is invalid or options has an unsupported bit set."; 21 | case EISDIR: return "path or fd do not refer to a regular file and the attribute in question is only applicable to files. Similar to EPERM."; 22 | case ENOTDIR: return "A component of path's prefix is not a directory."; 23 | case ENAMETOOLONG: return "The length of name exceeds XATTR_MAXNAMELEN UTF-8 bytes, or a component of path exceeds NAME_MAX characters, or the entire path exceeds PATH_MAX characters."; 24 | case EACCES: return "Search permission is denied for a component of path or the attribute is not allowed to be read (e.g. an ACL prohibits reading the attributes of this file)."; 25 | case ELOOP: return "Too many symbolic links were encountered in translating the pathname."; 26 | case EFAULT: return "path or name points to an invalid address."; 27 | case EIO: return "An I/O error occurred while reading from or writing to the file system."; 28 | default: return strerror(e); 29 | } 30 | } 31 | 32 | const char* errorDescriptionForSet(int e) { 33 | switch (e) { 34 | case EEXIST: return "options contains XATTR_CREATE and the named attribute already exists."; 35 | case E_ENOATTR: return "options is set to XATTR_REPLACE and the named attribute does not exist."; 36 | case ENOTSUP: return "The file system does not support extended attributes or has the feature disabled."; 37 | case EROFS: return "The file system is mounted read-only."; 38 | case ERANGE: return "The data size of the attribute is out of range (some attributes have size restrictions)."; 39 | case EPERM: return "Attributes cannot be associated with this type of object. For example, attributes are not allowed for resource forks."; 40 | case EINVAL: return "name or options is invalid. name must be valid UTF-8 and options must make sense."; 41 | case ENOTDIR: return "A component of path is not a directory."; 42 | case ENAMETOOLONG: return "name exceeded XATTR_MAXNAMELEN UTF-8 bytes, or a component of path exceeded NAME_MAX characters, or the entire path exceeded PATH_MAX characters."; 43 | case EACCES: return "Search permission is denied for a component of path or permission to set the attribute is denied."; 44 | case ELOOP: return "Too many symbolic links were encountered resolving path."; 45 | case EFAULT: return "path or name points to an invalid address."; 46 | case EIO: return "An I/O error occurred while reading from or writing to the file system."; 47 | case E2BIG: return "The data size of the extended attribute is too large."; 48 | case ENOSPC: return "Not enough space left on the file system."; 49 | default: return strerror(e); 50 | } 51 | } 52 | 53 | const char* _error_code(int e) { 54 | switch (e) { 55 | /* Standard */ 56 | case EPERM: return "EPERM"; 57 | case ENOENT: return "ENOENT"; 58 | case ESRCH: return "ESRCH"; 59 | case EINTR: return "EINTR"; 60 | case EIO: return "EIO"; 61 | case ENXIO: return "ENXIO"; 62 | case E2BIG: return "E2BIG"; 63 | case ENOEXEC: return "ENOEXEC"; 64 | case EBADF: return "EBADF"; 65 | case ECHILD: return "ECHILD"; 66 | case EAGAIN: return "EAGAIN"; 67 | case ENOMEM: return "ENOMEM"; 68 | case EACCES: return "EACCES"; 69 | case EFAULT: return "EFAULT"; 70 | case ENOTBLK: return "ENOTBLK"; 71 | case EBUSY: return "EBUSY"; 72 | case EEXIST: return "EEXIST"; 73 | case EXDEV: return "EXDEV"; 74 | case ENODEV: return "ENODEV"; 75 | case ENOTDIR: return "ENOTDIR"; 76 | case EISDIR: return "EISDIR"; 77 | case EINVAL: return "EINVAL"; 78 | case ENFILE: return "ENFILE"; 79 | case EMFILE: return "EMFILE"; 80 | case ENOTTY: return "ENOTTY"; 81 | case ETXTBSY: return "ETXTBSY"; 82 | case EFBIG: return "EFBIG"; 83 | case ENOSPC: return "ENOSPC"; 84 | case ESPIPE: return "ESPIPE"; 85 | case EROFS: return "EROFS"; 86 | case EMLINK: return "EMLINK"; 87 | case EPIPE: return "EPIPE"; 88 | case EDOM: return "EDOM"; 89 | case ERANGE: return "ERANGE"; 90 | /* Special */ 91 | case E_ENOATTR: return S_ENOATTR; 92 | case ENOTSUP: return "ENOTSUP"; 93 | case ENAMETOOLONG: return "ENAMETOOLONG"; 94 | case ELOOP: return "ELOOP"; 95 | /* Unknown */ 96 | default: return ""; 97 | } 98 | } 99 | 100 | napi_status create_xattr_error(napi_env env, int e, napi_value* result) { 101 | napi_status status; 102 | 103 | napi_value code; 104 | status = napi_create_string_utf8(env, _error_code(e), NAPI_AUTO_LENGTH, &code); 105 | if (status != napi_ok) return status; 106 | 107 | napi_value msg; 108 | status = napi_create_string_utf8(env, _error_description(e), NAPI_AUTO_LENGTH, &msg); 109 | 110 | napi_value error; 111 | status = napi_create_error(env, code, msg, &error); 112 | if (status != napi_ok) return status; 113 | 114 | napi_value error_number; 115 | status = napi_create_int32(env, e, &error_number); 116 | if (status != napi_ok) return status; 117 | 118 | status = napi_set_named_property(env, error, "errno", error_number); 119 | if (status != napi_ok) return status; 120 | 121 | *result = error; 122 | return napi_ok; 123 | } 124 | 125 | napi_status throw_xattr_error(napi_env env, int e) { 126 | napi_status status; 127 | 128 | napi_value error; 129 | status = create_xattr_error(env, e, &error); 130 | if (status != napi_ok) return status; 131 | 132 | status = napi_throw(env, error); 133 | if (status != napi_ok) return status; 134 | 135 | return napi_ok; 136 | } 137 | -------------------------------------------------------------------------------- /src/error.h: -------------------------------------------------------------------------------- 1 | #ifndef LD_ERROR_H 2 | #define LD_ERROR_H 3 | 4 | #define NAPI_VERSION 1 5 | #include 6 | 7 | napi_status create_xattr_error(napi_env env, int e, napi_value* result); 8 | napi_status throw_xattr_error(napi_env env, int e); 9 | 10 | #endif 11 | -------------------------------------------------------------------------------- /src/sync.c: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | 6 | #include "error.h" 7 | #include "util.h" 8 | 9 | #include "sync.h" 10 | 11 | napi_value xattr_get_sync(napi_env env, napi_callback_info info) { 12 | size_t argc = 2; 13 | napi_value args[2]; 14 | assert(napi_get_cb_info(env, info, &argc, args, NULL, NULL) == napi_ok); 15 | 16 | size_t filename_length; 17 | assert(napi_get_value_string_utf8(env, args[0], NULL, 0, &filename_length) == napi_ok); 18 | char *filename = malloc(filename_length + 1); 19 | assert(napi_get_value_string_utf8(env, args[0], filename, filename_length + 1, NULL) == napi_ok); 20 | 21 | size_t attribute_length; 22 | assert(napi_get_value_string_utf8(env, args[1], NULL, 0, &attribute_length) == napi_ok); 23 | char *attribute = malloc(attribute_length + 1); 24 | assert(napi_get_value_string_utf8(env, args[1], attribute, attribute_length + 1, NULL) == napi_ok); 25 | 26 | ssize_t value_length; 27 | 28 | #ifdef __APPLE__ 29 | value_length = getxattr(filename, attribute, NULL, 0, 0, 0); 30 | #else 31 | value_length = getxattr(filename, attribute, NULL, 0); 32 | #endif 33 | 34 | if (value_length == -1) { 35 | free(filename); 36 | free(attribute); 37 | assert(throw_xattr_error(env, errno) == napi_ok); 38 | return NULL; 39 | } 40 | 41 | napi_value buffer; 42 | void* buffer_data; 43 | assert(napi_create_buffer(env, (size_t) value_length, &buffer_data, &buffer) == napi_ok); 44 | 45 | #ifdef __APPLE__ 46 | value_length = getxattr(filename, attribute, buffer_data, (size_t) value_length, 0, 0); 47 | #else 48 | value_length = getxattr(filename, attribute, buffer_data, (size_t) value_length); 49 | #endif 50 | 51 | free(filename); 52 | free(attribute); 53 | 54 | if (value_length == -1) { 55 | assert(throw_xattr_error(env, errno) == napi_ok); 56 | return NULL; 57 | } 58 | 59 | return buffer; 60 | } 61 | 62 | napi_value xattr_set_sync(napi_env env, napi_callback_info info) { 63 | size_t argc = 3; 64 | napi_value args[3]; 65 | assert(napi_get_cb_info(env, info, &argc, args, NULL, NULL) == napi_ok); 66 | 67 | size_t filename_length; 68 | assert(napi_get_value_string_utf8(env, args[0], NULL, 0, &filename_length) == napi_ok); 69 | char *filename = malloc(filename_length + 1); 70 | assert(napi_get_value_string_utf8(env, args[0], filename, filename_length + 1, NULL) == napi_ok); 71 | 72 | size_t attribute_length; 73 | assert(napi_get_value_string_utf8(env, args[1], NULL, 0, &attribute_length) == napi_ok); 74 | char *attribute = malloc(attribute_length + 1); 75 | assert(napi_get_value_string_utf8(env, args[1], attribute, attribute_length + 1, NULL) == napi_ok); 76 | 77 | void *value; 78 | size_t value_length; 79 | assert(napi_get_buffer_info(env, args[2], &value, &value_length) == napi_ok); 80 | 81 | #ifdef __APPLE__ 82 | int res = setxattr(filename, attribute, value, value_length, 0, 0); 83 | #else 84 | int res = setxattr(filename, attribute, value, value_length, 0); 85 | #endif 86 | 87 | free(filename); 88 | free(attribute); 89 | 90 | if (res == -1) { 91 | assert(throw_xattr_error(env, errno) == napi_ok); 92 | return NULL; 93 | } 94 | 95 | return NULL; 96 | } 97 | 98 | napi_value xattr_list_sync(napi_env env, napi_callback_info info) { 99 | size_t argc = 1; 100 | napi_value args[1]; 101 | assert(napi_get_cb_info(env, info, &argc, args, NULL, NULL) == napi_ok); 102 | 103 | size_t filename_length; 104 | assert(napi_get_value_string_utf8(env, args[0], NULL, 0, &filename_length) == napi_ok); 105 | char *filename = malloc(filename_length + 1); 106 | assert(napi_get_value_string_utf8(env, args[0], filename, filename_length + 1, NULL) == napi_ok); 107 | 108 | ssize_t result_length; 109 | 110 | #ifdef __APPLE__ 111 | result_length = listxattr(filename, NULL, 0, 0); 112 | #else 113 | result_length = listxattr(filename, NULL, 0); 114 | #endif 115 | 116 | if (result_length == -1) { 117 | free(filename); 118 | assert(throw_xattr_error(env, errno) == napi_ok); 119 | return NULL; 120 | } 121 | 122 | char *result = (char *) malloc((size_t) result_length); 123 | 124 | #ifdef __APPLE__ 125 | result_length = listxattr(filename, result, (size_t) result_length, 0); 126 | #else 127 | result_length = listxattr(filename, result, (size_t) result_length); 128 | #endif 129 | 130 | free(filename); 131 | 132 | if (result_length == -1) { 133 | free(result); 134 | assert(throw_xattr_error(env, errno) == napi_ok); 135 | return NULL; 136 | } 137 | 138 | napi_value array; 139 | assert(split_string_array(env, result, (size_t) result_length, &array) == napi_ok); 140 | 141 | free(result); 142 | 143 | return array; 144 | } 145 | 146 | napi_value xattr_remove_sync(napi_env env, napi_callback_info info) { 147 | size_t argc = 2; 148 | napi_value args[2]; 149 | assert(napi_get_cb_info(env, info, &argc, args, NULL, NULL) == napi_ok); 150 | 151 | size_t filename_length; 152 | assert(napi_get_value_string_utf8(env, args[0], NULL, 0, &filename_length) == napi_ok); 153 | char *filename = malloc(filename_length + 1); 154 | assert(napi_get_value_string_utf8(env, args[0], filename, filename_length + 1, NULL) == napi_ok); 155 | 156 | size_t attribute_length; 157 | assert(napi_get_value_string_utf8(env, args[1], NULL, 0, &attribute_length) == napi_ok); 158 | char *attribute = malloc(attribute_length + 1); 159 | assert(napi_get_value_string_utf8(env, args[1], attribute, attribute_length + 1, NULL) == napi_ok); 160 | 161 | #ifdef __APPLE__ 162 | int res = removexattr(filename, attribute, 0); 163 | #else 164 | int res = removexattr(filename, attribute); 165 | #endif 166 | 167 | free(filename); 168 | free(attribute); 169 | 170 | if (res == -1) { 171 | assert(throw_xattr_error(env, errno) == napi_ok); 172 | return NULL; 173 | } 174 | 175 | return NULL; 176 | } 177 | -------------------------------------------------------------------------------- /src/sync.h: -------------------------------------------------------------------------------- 1 | #ifndef LD_SYNC_H 2 | #define LD_SYNC_H 3 | 4 | #define NAPI_VERSION 1 5 | #include 6 | 7 | napi_value xattr_get_sync(napi_env env, napi_callback_info info); 8 | napi_value xattr_set_sync(napi_env env, napi_callback_info info); 9 | napi_value xattr_list_sync(napi_env env, napi_callback_info info); 10 | napi_value xattr_remove_sync(napi_env env, napi_callback_info info); 11 | 12 | #endif 13 | -------------------------------------------------------------------------------- /src/util.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #include "util.h" 4 | 5 | napi_status split_string_array(napi_env env, const char *data, size_t length, napi_value* result) { 6 | napi_status status; 7 | 8 | napi_value array; 9 | status = napi_create_array(env, &array); 10 | if (status != napi_ok) return status; 11 | 12 | uint32_t array_position = 0; 13 | size_t value_position = 0; 14 | 15 | while (value_position < length) { 16 | size_t item_length = strlen(data + value_position); 17 | 18 | napi_value item_string; 19 | status = napi_create_string_utf8(env, data + value_position, item_length, &item_string); 20 | if (status != napi_ok) return status; 21 | 22 | status = napi_set_element(env, array, array_position, item_string); 23 | if (status != napi_ok) return status; 24 | 25 | value_position += item_length + 1; 26 | array_position += 1; 27 | } 28 | 29 | napi_value length_number; 30 | status = napi_create_uint32(env, array_position, &length_number); 31 | if (status != napi_ok) return status; 32 | 33 | status = napi_set_named_property(env, array, "length", length_number); 34 | if (status != napi_ok) return status; 35 | 36 | *result = array; 37 | return napi_ok; 38 | } 39 | -------------------------------------------------------------------------------- /src/util.h: -------------------------------------------------------------------------------- 1 | #ifndef LD_UTIL_H 2 | #define LD_UTIL_H 3 | 4 | #include 5 | 6 | #define NAPI_VERSION 1 7 | #include 8 | 9 | napi_status split_string_array(napi_env env, const char *data, size_t length, napi_value* result); 10 | 11 | #endif 12 | -------------------------------------------------------------------------------- /src/xattr.c: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #define NAPI_VERSION 1 4 | #include 5 | 6 | #include "async.h" 7 | #include "sync.h" 8 | 9 | static napi_value Init(napi_env env, napi_value exports) { 10 | napi_value result; 11 | assert(napi_create_object(env, &result) == napi_ok); 12 | 13 | napi_value get_fn; 14 | assert(napi_create_function(env, "get", NAPI_AUTO_LENGTH, xattr_get, NULL, &get_fn) == napi_ok); 15 | assert(napi_set_named_property(env, result, "get", get_fn) == napi_ok); 16 | napi_value set_fn; 17 | assert(napi_create_function(env, "set", NAPI_AUTO_LENGTH, xattr_set, NULL, &set_fn) == napi_ok); 18 | assert(napi_set_named_property(env, result, "set", set_fn) == napi_ok); 19 | napi_value list_fn; 20 | assert(napi_create_function(env, "list", NAPI_AUTO_LENGTH, xattr_list, NULL, &list_fn) == napi_ok); 21 | assert(napi_set_named_property(env, result, "list", list_fn) == napi_ok); 22 | napi_value remove_fn; 23 | assert(napi_create_function(env, "remove", NAPI_AUTO_LENGTH, xattr_remove, NULL, &remove_fn) == napi_ok); 24 | assert(napi_set_named_property(env, result, "remove", remove_fn) == napi_ok); 25 | 26 | napi_value get_sync_fn; 27 | assert(napi_create_function(env, "getSync", NAPI_AUTO_LENGTH, xattr_get_sync, NULL, &get_sync_fn) == napi_ok); 28 | assert(napi_set_named_property(env, result, "getSync", get_sync_fn) == napi_ok); 29 | napi_value set_sync_fn; 30 | assert(napi_create_function(env, "setSync", NAPI_AUTO_LENGTH, xattr_set_sync, NULL, &set_sync_fn) == napi_ok); 31 | assert(napi_set_named_property(env, result, "setSync", set_sync_fn) == napi_ok); 32 | napi_value list_sync_fn; 33 | assert(napi_create_function(env, "listSync", NAPI_AUTO_LENGTH, xattr_list_sync, NULL, &list_sync_fn) == napi_ok); 34 | assert(napi_set_named_property(env, result, "listSync", list_sync_fn) == napi_ok); 35 | napi_value remove_sync_fn; 36 | assert(napi_create_function(env, "removeSync", NAPI_AUTO_LENGTH, xattr_remove_sync, NULL, &remove_sync_fn) == napi_ok); 37 | assert(napi_set_named_property(env, result, "removeSync", remove_sync_fn) == napi_ok); 38 | 39 | return result; 40 | } 41 | 42 | NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) 43 | -------------------------------------------------------------------------------- /test/xattr.js: -------------------------------------------------------------------------------- 1 | /* eslint-env mocha */ 2 | 3 | import * as xattr from '../index.js' 4 | 5 | import assert from 'node:assert' 6 | import crypto from 'node:crypto' 7 | import fs from 'node:fs' 8 | import os from 'node:os' 9 | 10 | import temp from 'fs-temp' 11 | 12 | const attribute0 = 'user.linusu.test' 13 | const attribute1 = 'user.linusu.secondary' 14 | const payload0 = crypto.randomBytes(24).toString('hex') 15 | const payload1 = crypto.randomBytes(24).toString('hex') 16 | 17 | describe('xattr#sync', function () { 18 | let path 19 | 20 | before(function () { 21 | path = temp.writeFileSync('') 22 | }) 23 | 24 | it('should set an attribute', function () { 25 | xattr.setAttributeSync(path, attribute0, payload0) 26 | xattr.setAttributeSync(path, attribute1, payload1) 27 | }) 28 | 29 | it('should get an attribute', function () { 30 | const val = xattr.getAttributeSync(path, attribute0) 31 | assert(Buffer.isBuffer(val)) 32 | assert.strictEqual(val.toString(), payload0) 33 | }) 34 | 35 | it('should list the attributes', function () { 36 | const val = xattr.listAttributesSync(path) 37 | assert.ok(val.includes(attribute0)) 38 | assert.ok(val.includes(attribute1)) 39 | }) 40 | 41 | it('should remove the attribute', function () { 42 | xattr.removeAttributeSync(path, attribute0) 43 | xattr.removeAttributeSync(path, attribute1) 44 | }) 45 | 46 | it('should give useful errors', function () { 47 | assert.throws(function () { 48 | xattr.getAttributeSync(path, attribute0) 49 | }, function (err) { 50 | assert.strictEqual(err.errno, os.platform() === 'darwin' ? 93 : 61) 51 | assert.strictEqual(err.code, os.platform() === 'darwin' ? 'ENOATTR' : 'ENODATA') 52 | return true 53 | }) 54 | }) 55 | 56 | after(function (done) { 57 | fs.unlink(path, done) 58 | }) 59 | }) 60 | 61 | describe('xattr#async', function () { 62 | let path 63 | 64 | before(function () { 65 | path = temp.writeFileSync('') 66 | }) 67 | 68 | it('should set an attribute', async function () { 69 | await xattr.setAttribute(path, attribute0, payload0) 70 | await xattr.setAttribute(path, attribute1, payload1) 71 | }) 72 | 73 | it('should get an attribute', async function () { 74 | const val = await xattr.getAttribute(path, attribute0) 75 | 76 | assert(Buffer.isBuffer(val)) 77 | assert.strictEqual(val.toString(), payload0) 78 | }) 79 | 80 | it('should list the attributes', async function () { 81 | const list = await xattr.listAttributes(path) 82 | 83 | assert.ok(list.includes(attribute0)) 84 | assert.ok(list.includes(attribute1)) 85 | }) 86 | 87 | it('should remove the attribute', async function () { 88 | await xattr.removeAttribute(path, attribute0) 89 | await xattr.removeAttribute(path, attribute1) 90 | }) 91 | 92 | it('should give useful errors', async function () { 93 | let err 94 | try { 95 | await xattr.getAttribute(path, attribute0) 96 | } catch (_err) { 97 | err = _err 98 | } 99 | 100 | assert(err) 101 | assert.strictEqual(err.errno, os.platform() === 'darwin' ? 93 : 61) 102 | assert.strictEqual(err.code, os.platform() === 'darwin' ? 'ENOATTR' : 'ENODATA') 103 | }) 104 | 105 | after(function (done) { 106 | fs.unlink(path, done) 107 | }) 108 | }) 109 | 110 | describe('xattr#utf8', function () { 111 | let path 112 | 113 | before(function () { 114 | path = temp.template('∞ %s').writeFileSync('') 115 | }) 116 | 117 | it('should set an attribute', async function () { 118 | await xattr.setAttribute(path, attribute0, payload0) 119 | }) 120 | 121 | it('should get an attribute', async function () { 122 | const val = await xattr.getAttribute(path, attribute0) 123 | 124 | assert(Buffer.isBuffer(val)) 125 | assert.strictEqual(val.toString(), payload0) 126 | }) 127 | 128 | it('should list the attributes', async function () { 129 | const list = await xattr.listAttributes(path) 130 | 131 | assert.ok(list.includes(attribute0)) 132 | }) 133 | 134 | it('should remove the attribute', async function () { 135 | await xattr.removeAttribute(path, attribute0) 136 | }) 137 | 138 | it('should give useful errors', async function () { 139 | let err 140 | try { 141 | await xattr.getAttribute(path, attribute0) 142 | } catch (_err) { 143 | err = _err 144 | } 145 | 146 | assert(err) 147 | assert.strictEqual(err.errno, os.platform() === 'darwin' ? 93 : 61) 148 | assert.strictEqual(err.code, os.platform() === 'darwin' ? 'ENOATTR' : 'ENODATA') 149 | }) 150 | 151 | after(function (done) { 152 | fs.unlink(path, done) 153 | }) 154 | }) 155 | --------------------------------------------------------------------------------