UNPKG

nv-file-basic

Version:

nv-file-basic ============== - simple async wrap of node fs-module - add some symbol-link functions

352 lines (289 loc) 9.79 kB
nv-file-basic ============== - simple async wrap of node fs-module - add some symbol-link functions install ======= - npm install nv-file-basic usage ===== const fbsc = require("nv-file-basic") example ------- ### char\_dev "/dev/ttyS0" await fbsc.is_char_dev("/dev/ttyS0") >true ### blk\_dev "/dev/sda1" await fbsc.is_blk_dev("/dev/sda1") >true ### sock var srv = http.createServer(); srv.listen("sock") await fbsc.is_sock("./sock") >true ### fifo mkfifo fifo await fbsc.is_fifo("./fifo") ### file AND dir nv-file-basic# ls -l total 20 -rw-r--r-- 1 root root 1539 Feb 27 09:56 consts.js -rw-r--r-- 1 root root 4454 Feb 27 10:03 index.js drwxr-xr-x 3 root root 4096 Feb 26 01:58 node_modules -rw-r--r-- 1 root root 58 Feb 26 01:58 package.json lrwxrwxrwx 1 root root 8 Feb 27 10:04 s0 -> index.js lrwxrwxrwx 1 root root 2 Feb 27 10:05 s1-to-s0 -> s0 lrwxrwxrwx 1 root root 26 Feb 27 10:05 s2 -> ../nv-file-reader/index.js ..... > await fbsc.is_file("./consts.js") true > > await fbsc.is_dir("./node_modules") true > > await fbsc.is_slink("./s0") true > await fbsc.readlink("./s0") '/opt/JS/NV5_/nv-file-/pkgs/nv-file-basic/index.js' > > await fbsc.readlink("./s1-to-s0") '/opt/JS/NV5_/nv-file-/pkgs/nv-file-basic/s0' > await fbsc.readlink_r("./s1-to-s0") '/opt/JS/NV5_/nv-file-/pkgs/nv-file-basic/index.js' > ### invalid symbol-link #### nv-file-basic# ls -l /usr/bin/ | egrep ghci lrwxrwxrwx 1 root root 35 Sep 10 2019 ghci -> /mnt/sdb/HASKELL/installed/bin/ghci nv-file-basic# nv-file-basic# cat /mnt/sdb/HASKELL/installed/bin/ghci cat: /mnt/sdb/HASKELL/installed/bin/ghci: No such file or directory nv-file-basic# > await fbsc.readlink("/usr/bin/ghci") '/mnt/sdb/HASKELL/installed/bin/ghci' > > await fbsc.is_valid_slink("/usr/bin/ghci") false > #### # touch x # ln -s x s3 # ln -s s3 s4 # ln -s s4 s5 # rm x > await fbsc.readlink_r("./s5") '/opt/JS/NV5_/nv-file-/pkgs/nv-file-basic/x' > > await fbsc.is_valid_slink("./s5") true //different behavior with fs.exist > await fbsc.is_valid_slink("./s4") true //different behavior with fs.exist > await fbsc.is_valid_slink("./s3") false > > await fbsc.is_valid_slink_r("./s5") false > await fbsc.get_tname("./x") 'noexist' > ### children > await fbsc.get_children("./node_modules") [ '/opt/JS/NV5_/nv-file-/pkgs/nv-file-basic/node_modules/.package-lock.json', '/opt/JS/NV5_/nv-file-/pkgs/nv-file-basic/node_modules/nv-path-basic' ] > ### lstat > await fbsc.lstat("./index.js") { dev: 2050, nlink: 1, uid: 0, gid: 0, rdev: 0, blksize: 4096, ino: 691999, size: 5445, blocks: 16, atimeMs: 1645972913639.7522, mtimeMs: 1645972908643.701, ctimeMs: 1645972908655.7012, birthtimeMs: 1645972908643.701, atime: 2022-02-27T14:41:53.640Z, mtime: 2022-02-27T14:41:48.644Z, ctime: 2022-02-27T14:41:48.656Z, birthtime: 2022-02-27T14:41:48.644Z, permission: { owner_can_read: true, owner_can_write: true, owner_can_exec: false, group_can_read: true, group_can_write: false, group_can_exec: false, other_can_read: true, other_can_write: false, other_can_exec: false }, type: 'file' } > ### CONSTS CONSTS: { TNAMES: { slink: 'slink', valid_slink: 'valid_slink', invalid_slink: 'invalid_slink', file: 'file', dir: 'dir', blk_dev: 'blk_dev', char_dev: 'char_dev', fifo: 'fifo', sock: 'sock', noexist: 'noexist' }, CONST_TNAME_TO_NUM: { S_IFMT: 61440, S_IFREG: 32768, S_IFDIR: 16384, S_IFCHR: 8192, S_IFBLK: 24576, S_Ififo: 4096, S_IFLNK: 40960, S_IFSOCK: 49152 }, NUM_TO_CONST_TNAME: { '4096': 'S_Ififo', '8192': 'S_IFCHR', '16384': 'S_IFDIR', '24576': 'S_IFBLK', '32768': 'S_IFREG', '40960': 'S_IFLNK', '49152': 'S_IFSOCK', '61440': 'S_IFMT' }, CONST_TNAME_TO_TNAME: { S_IFREG: 'file', S_IFDIR: 'dir', S_IFCHR: 'char_dev', S_IFBLK: 'blk_dev', S_Ififo: 'fifo', S_IFLNK: 'slink', S_IFSOCK: 'sock' }, TNAME_TO_CONST_TNAME: { file: 'S_IFREG', dir: 'S_IFDIR', char_dev: 'S_IFCHR', blk_dev: 'S_IFBLK', fifo: 'S_Ififo', slink: 'S_IFLNK', sock: 'S_IFSOCK' }, num_to_tname: [Function: num_to_tname], tname_to_num: [Function: tname_to_num], PNAMES: { owner_can_read: 'owner_can_read', owner_can_write: 'owner_can_write', owner_can_exec: 'owner_can_exec', group_can_read: 'group_can_read', group_can_write: 'group_can_write', group_can_exec: 'group_can_exec', other_can_read: 'other_can_read', other_can_write: 'other_can_write', other_can_exec: 'other_can_exec' }, CONST_PNAME_TO_PNAME: { S_IRUSR: 'owner_can_read', S_IWUSR: 'owner_can_write', S_IXUSR: 'owner_can_exec', S_IRGRP: 'group_can_read', S_IWGRP: 'group_can_write', S_IXGRP: 'group_can_exec', S_IROTH: 'other_can_read', S_IWOTH: 'other_can_write', S_IXOTH: 'other_can_exec' }, PNAME_TO_CONST_PNAME: { owner_can_read: 'S_IRUSR', owner_can_write: 'S_IWUSR', owner_can_exec: 'S_IXUSR', group_can_read: 'S_IRGRP', group_can_write: 'S_IWGRP', group_can_exec: 'S_IXGRP', other_can_read: 'S_IROTH', other_can_write: 'S_IWOTH', other_can_exec: 'S_IXOTH' }, CONST_PNAME_TO_NUM: { S_IRUSR: 256, S_IWUSR: 128, S_IXUSR: 64, S_IRGRP: 32, S_IWGRP: 16, S_IXGRP: 8, S_IROTH: 4, S_IWOTH: 2, S_IXOTH: 1 }, NUM_TO_CONST_PNAME: { '1': 'S_IXOTH', '2': 'S_IWOTH', '4': 'S_IROTH', '8': 'S_IXGRP', '16': 'S_IWGRP', '32': 'S_IRGRP', '64': 'S_IXUSR', '128': 'S_IWUSR', '256': 'S_IRUSR' }, num_to_pname: [Function: num_to_pname], pname_to_num: [Function: pname_to_num] } ### this is a getter, return a promise > await fbsc.FH_SYMS_ { kCapture: Symbol(kCapture), kHandle: Symbol(kHandle), kFd: Symbol(kFd), kRefs: Symbol(kRefs), kClosePromise: Symbol(kClosePromise) } > APIS ==== const _readdir = _promisify(fs.readdir); const _readlink = _promisify(fs.readlink); const _lstat = _promisify(fs.lstat); const _exist = _promisify(fs.exists); const exist = async (p) => { const is_slink = async (p) => { const is_file = async (p) => { const is_dir = async (p) => { const is_blk_dev = async (p) => { const is_char_dev = async (p) => { const is_fifo = async (p) => { const is_sock = async (p) => { const _get_tname_with_stat = (stat) => { const get_tname = async (p) => { const _get_children_with_stat = async (p,stat) => { const get_children = async (p) => { const _is_leaf_with_stat = async (p,stat) => { const is_leaf = async(p) => { const _readlink_without_resolve = async(p) => { const readlink = async (p)=> { const readlink_r = async (p) => { const is_valid_slink = async (p) => { const is_invalid_slink = async (p) => { const is_valid_slink_r = async (p) => { const is_invalid_slink_r = async (p) => { const size = async (p) => { const _permission_with_stat = (stat) => { const permission = async (p)=> { const lstat = async (p)=> { LICENSE ======= - ISC