|
| 1 | +const { dirname, resolve } = require('path') |
| 2 | +const { fileURLToPath } = require('url') |
| 3 | + |
| 4 | +const fs = require('../fs.js') |
| 5 | + |
| 6 | +// given a path, find the owner of the nearest parent |
| 7 | +const find = async (path) => { |
| 8 | + // if we have no getuid, permissions are irrelevant on this platform |
| 9 | + if (!process.getuid) { |
| 10 | + return {} |
| 11 | + } |
| 12 | + |
| 13 | + // fs methods accept URL objects with a scheme of file: so we need to unwrap |
| 14 | + // those into an actual path string before we can resolve it |
| 15 | + const resolved = path != null && path.href && path.origin |
| 16 | + ? resolve(fileURLToPath(path)) |
| 17 | + : resolve(path) |
| 18 | + |
| 19 | + let stat |
| 20 | + |
| 21 | + try { |
| 22 | + stat = await fs.lstat(resolved) |
| 23 | + } finally { |
| 24 | + // if we got a stat, return its contents |
| 25 | + if (stat) { |
| 26 | + return { uid: stat.uid, gid: stat.gid } |
| 27 | + } |
| 28 | + |
| 29 | + // try the parent directory |
| 30 | + if (resolved !== dirname(resolved)) { |
| 31 | + return find(dirname(resolved)) |
| 32 | + } |
| 33 | + |
| 34 | + // no more parents, never got a stat, just return an empty object |
| 35 | + return {} |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +// given a path, uid, and gid update the ownership of the path if necessary |
| 40 | +const update = async (path, uid, gid) => { |
| 41 | + // nothing to update, just exit |
| 42 | + if (uid === undefined && gid === undefined) { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + try { |
| 47 | + // see if the permissions are already the same, if they are we don't |
| 48 | + // need to do anything, so return early |
| 49 | + const stat = await fs.stat(path) |
| 50 | + if (uid === stat.uid && gid === stat.gid) { |
| 51 | + return |
| 52 | + } |
| 53 | + } catch (err) {} |
| 54 | + |
| 55 | + try { |
| 56 | + await fs.chown(path, uid, gid) |
| 57 | + } catch (err) {} |
| 58 | +} |
| 59 | + |
| 60 | +// accepts a `path` and the `owner` property of an options object and normalizes |
| 61 | +// it into an object with numerical `uid` and `gid` |
| 62 | +const validate = async (path, input) => { |
| 63 | + let uid |
| 64 | + let gid |
| 65 | + |
| 66 | + if (typeof input === 'string' || typeof input === 'number') { |
| 67 | + uid = input |
| 68 | + gid = input |
| 69 | + } else if (input && typeof input === 'object') { |
| 70 | + uid = input.uid |
| 71 | + gid = input.gid |
| 72 | + } |
| 73 | + |
| 74 | + if (uid === 'inherit' || gid === 'inherit') { |
| 75 | + const owner = await find(path) |
| 76 | + if (uid === 'inherit') { |
| 77 | + uid = owner.uid |
| 78 | + } |
| 79 | + |
| 80 | + if (gid === 'inherit') { |
| 81 | + gid = owner.gid |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + return { uid, gid } |
| 86 | +} |
| 87 | + |
| 88 | +module.exports = { |
| 89 | + find, |
| 90 | + update, |
| 91 | + validate, |
| 92 | +} |
0 commit comments