How to determine if an SFTP file is a directory in Node.js? - node.js

The ssh2 library's SFTP readdir method gives me back all the files in the remote directory. How can I tell if any of of them are directories?
Here's some example output from the library:
{ filename:
'myfile',
longname:
'-rwxr-x--- 1 myuser mygroup 19036227 Nov 21 11:05 myfile',
attrs:
Stats {
mode: 33256,
permissions: 33256,
uid: 603,
gid: 1014,
size: 19036227,
atime: 1542859216,
mtime: 1542816340 } }

The file's mode contains bits indicating its type. You can check it like this:
const fs = require('fs');
function isDir(mode) {
return (mode & fs.constants.S_IFMT) == fs.constants.S_IFDIR;
}
isDir(myfile.attrs.mode);

Related

Kubernetes gitconfig mounted as directory instead of file

I have some helm charts in which I would like to mount the git config globally on each container.
In this case the home directory for each container is / path. When I would like to do it manually on the container I am getting following
git config --global --add safe.directory "*" error: could not lock config file //.gitconfig: Permission denied
Now I want to map my config map to the global .gitconfig file.
set {
name = "git.sync.extraVolumeMounts[0].name"
value = "git-config"
}
set {
name = "git.sync.extraVolumeMounts[0].mountPath"
value = "/.gitconfig"
}
set {
name = "git.sync.extraVolumeMounts[0].subPath"
value = ".gitconfig"
}
With such config I am getting the .gitconfig as folder not the file
bitnami#airflow-web-7cdb6f5d6f-48mzh:/$ ls -la
total 84
drwxr-xr-x 1 root root 4096 Dec 2 12:20 .
drwxr-xr-x 1 root root 4096 Dec 2 12:20 ..
drwxrwsrwx 2 root bitnami 4096 Dec 2 12:20 .gitconfig
drwxr-xr-x 1 root root 4096 Jul 30 11:21 bin
Any idea what I am doing wrong? Is there any environment variable instead I can set?
I tried to use system config but it does not work either as some folder structure is missing.
Just for future, I was able to achieve that with the usage of the proper escaping
{
"name": "git.sync.extraVolumeMounts[0].mountPath",
"type": null,
"value": "/\\.gitconfig"
},
{
"name": "git.sync.extraVolumeMounts[0].name",
"type": null,
"value": "git-config"
},
{
"name": "git.sync.extraVolumeMounts[0].subPath",
"type": null,
"value": "gitconfig"
},
This basically, creates a .gitconfig file and not a folder.
Setup is working fine.

Sharp fails on resize with zsh: killed

I have a small node script to resize images for a website:
const sharp = require('sharp')
const fsp = require('fs/promises')
const path = require('path')
const FILES = [
'/home/xx/68/ea/68ea8c14c2b655c50cbc560b9f3a5af620882670.jpeg',
'/home/xx/4e/7f/4e7f94103480d8ff231b17310c598470dc10489c.jpeg',
]
const SIZES = [ 300, 500, 800, 1024, 2048 ]
const FORMATS = [ 'jpeg' ]
const dir = '/home/xx/media/temp/'
async function generatePreviews(pathName, name) {
const image = sharp(pathName)
const metadatas = await image.metadata()
console.log('format:', metadatas.format)
console.log('width:', metadatas.width)
console.log('height:', metadatas.height)
console.log('weight:', await fsp.stat(pathName))
for (const size of SIZES) {
for (const format of FORMATS) {
console.log('start create preview', name, format, size)
try {
await image.clone()
.resize(size, size, {fit: 'inside'})
.toFile(path.join(dir, `preview_${name}_${size}.${format}`))
console.log('Preview created', name, format, size)
} catch (e) {
console.log('An error occured')
console.log(e)
}
}
}
}
async function testPreviews() {
console.log('start generating previews')
for (let i = 0; i < FILES.length; i++) {
await generatePreviews(FILES[i], i)
}
console.log('All previews generated')
}
testPreviews()
It works perfectly on my desktop computer (old i5 760 from 2010), but on my server, it fails almost silently when generating the largest previews for the largest file…
Here is the output on the server:
% node ./src/scripts/test_sharp.js
start generating previews
format: jpeg
width: 683
height: 1024
weight: Stats {
dev: 51713,
mode: 33188,
nlink: 1,
uid: 1001,
gid: 1001,
rdev: 0,
blksize: 4096,
ino: 658937,
size: 473209,
blocks: 928,
atimeMs: 1654021875500.3074,
mtimeMs: 1467568028343.9807,
ctimeMs: 1645954831869.6965,
birthtimeMs: 1645954825541.6052,
atime: 2022-05-31T18:31:15.500Z,
mtime: 2016-07-03T17:47:08.344Z,
ctime: 2022-02-27T09:40:31.870Z,
birthtime: 2022-02-27T09:40:25.542Z
}
start create preview 0 jpeg 300
Preview created 0 jpeg 300
start create preview 0 jpeg 500
Preview created 0 jpeg 500
start create preview 0 jpeg 800
Preview created 0 jpeg 800
start create preview 0 jpeg 1024
Preview created 0 jpeg 1024
start create preview 0 jpeg 2048
Preview created 0 jpeg 2048
format: jpeg
width: 3840
height: 5760
weight: Stats {
dev: 51713,
mode: 33188,
nlink: 1,
uid: 1001,
gid: 1001,
rdev: 0,
blksize: 4096,
ino: 658752,
size: 2272667,
blocks: 4440,
atimeMs: 1654021876468.3223,
mtimeMs: 1468007124640.3523,
ctimeMs: 1645954169924.1448,
birthtimeMs: 1645954151475.8787,
atime: 2022-05-31T18:31:16.468Z,
mtime: 2016-07-08T19:45:24.640Z,
ctime: 2022-02-27T09:29:29.924Z,
birthtime: 2022-02-27T09:29:11.476Z
}
start create preview 1 jpeg 300
Preview created 1 jpeg 300
start create preview 1 jpeg 500
Preview created 1 jpeg 500
start create preview 1 jpeg 800
zsh: killed node ./src/scripts/test_sharp.js
It fails at 800px side preview generation for the largest file (5760 x 3840px).
The catch block doesn't trigger…
Server is a 1 CPU VPS with 1GB of RAM (hosted at gandi.net).
% node --version
v16.15.0
% yarn list --pattern sharp 1 Jun 04:06:40
yarn list v1.22.18
└─ sharp#0.30.6
Done in 1.49s.
Any help would be appreciated :)

Impossible to delete file in debugfs

I'm playing with debugfs. In a module, I've created a directory 'test_debugfs' in the debugfs filesystem (mounted at /sys/kernel/debug) and a file 'demo_file'.
// Create the test_debufs in /sys/kernel/debug
struct dentry * my_dirent;
static int __init my_module_init_module(void) {
my_dirent = debugfs_create_dir("test_debugfs", NULL);
debugfs_create_file("demo_file", 0666, my_dirent, NULL, &fops_debugfs);
}
Unfortunately, I forgot to remove the directory on module unload, and now I cannot remove the demo_file anymore.
# rmmod my_module
# cd /sys/kernel/debug/test_debugfs
# ls
demo_file
# rm -rf demo_file
rm: cannot remove 'demo_file': Operation not permitted
# sstat
File: demo_file
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 6h/6d Inode: 16426 Links: 1
Access: (0666/-rw-rw-rw-) Uid: ( 0/ root) Gid: ( 0/ root)
Access: 2021-04-28 10:20:14.807999989 +0200
Modify: 2021-04-28 10:20:14.807999989 +0200
Change: 2021-04-28 10:20:14.807999989 +0200
Birth: -
After rebooting my machine, the demo_file is still there.
Do you know how I could remove it ?
Answer:
Thanks to Varun, I managed to remove the file directly in the module with this code:
struct dentry * my_dirent;
static int __init my_module_init_module(void) {
struct path path;
ret = kern_path("/sys/kernel/debug/test_debugfs", LOOKUP_DIRECTORY, &path);
if (ret)
pr_err("Failed to lookup /sys/kernel/debug/test_debugfs err %d\n", ret);
else
debugfs_remove_recursive(path.dentry);
}
You cannot use rm command to remove file from debug_fs ..
The debugfs filesystem does not support unlink function in the directory inode operations. Hence rm command will fail
You have to use debugfs function void debugfs_remove(struct dentry *dentry) where dentry parameter is the return value from debugfs_create_file function call

nodejs process.setgid, process.setuid behavior with fs module

directory:
drwxrwxr-x 2 alex alex 4096 Aug 3 12:03 ./
drwxr-xr-x 17 alex alex 4096 Aug 3 11:18 ../
-rwx------ 1 root root 19 Aug 3 11:24 privilegedStuff*
-rwxrwx--- 1 root root 28 Aug 3 12:10 privilegedStuff1*
-rwxrwxr-x 1 alex alex 830 Aug 3 12:12 test.js*
test.js:
#!/usr/bin/env node
var fs = require('fs');
console.log(' user id: ', process.getuid());
console.log(' group id: ', process.getgid());
console.log(' user effective id: ', process.getegid());
console.log('group effective id: ', process.getegid());
console.log('\n switching user and group...\n');
process.setgid(1000);
process.setegid(1000);
process.setuid(1000);
process.seteuid(1000);
console.log(' user id: ', process.getuid());
console.log(' group id: ', process.getgid());
console.log(' user effective id: ', process.getegid());
console.log('group effective id: ', process.getegid());
console.log('\n output: \n');
console.log(fs.readFileSync('./privilegedStuff1', 'utf8'))
// this throws error as expected so I commented that
// console.log(fs.readFileSync('./privilegedStuff', 'utf8'))
privilegedStuff1:
content of privilegedStuff1
result:
alex#hp:/apps/test$ sudo ./test.js
user id: 0
group id: 0
user effective id: 0
group effective id: 0
switching user and group...
user id: 1000
group id: 1000
user effective id: 1000
group effective id: 1000
output:
content of privilegedStuff1
so what I don't understand is why node doesn't throw an error as it does nicely with privilegedStuff file? What am I missing?
alex#hp:/apps/test$ groups
alex adm cdrom sudo dip plugdev lpadmin sambashare
alex#hp:/apps/test$ cat privilegedStuff1
cat: privilegedStuff1: Permission denied
alex#hp:/apps/test$ sudo -s
root#hp:/apps/test# groups
root
In my test, I don't have such problem.
Can you enter the following command and show the result:
ls -l privilegedStuff1
id

gruntfile files named patterns, possible?

I'm playing around with Ghost, and I'd like to make the gruntfile compile the sass files from my theme.
So I started by modifying the sass task:
...
sass: {
admin: {
files: {
'<%= paths.adminAssets %>/css/screen.css': '<%= paths.adminAssets %>/sass/screen.scss'
}
},
themes: {
files:{
'content/themes/**/css/ie.css': 'content/themes/**/src/sass/ie.sass',
'content/themes/**/css/print.css': 'content/themes/**/src/sass/print.sass',
'content/themes/**/css/screen.css': 'content/themes/**/src/sass/screen.sass'
}
}
}
...
I realised that I could simplfy this to :
...
sass: {
admin: {
files: {
'<%= paths.adminAssets %>/css/screen.css': '<%= paths.adminAssets %>/sass/screen.scss'
}
},
themes: {
files:{
'content/themes/**/css/*.css': 'content/themes/**/src/sass/*.sass',
}
}
}
...
But then I was thinking, why isn't it replacing the stars in the destination with what it matches from the source?
Ends up it was just creating the following:
$ ls -al ./content/themes/
total 0
drwxrwxr-x 1 zenobius zenobius 50 Nov 18 02:10 .
drwxrwxr-x 1 zenobius zenobius 46 Nov 15 11:02 ..
drwxrwxr-x 1 zenobius zenobius 6 Nov 18 02:10 ** <----- sigh
drwxrwxr-x 1 zenobius zenobius 128 Nov 15 11:02 casper
drwxrwxr-x 1 zenobius zenobius 250 Nov 18 00:08 crycilium
I guess my question is really:
can i use some kind of regex named patterns
could I use a function in the files option to process the output name as the destination?
So the solution was to make use of grunt.file.expandMapping, (thanks to : https://stackoverflow.com/a/16672303/454615):
...
themes: {
files: grunt.file.expandMapping([
"content/themes/**/src/**/*.sass",
"!content/themes/**/src/**/_*.sass",
], '', {
expand: true,
ext: '.css',
rename: function(base, src) {
grunt.log.write(base + " " + src);
return src.replace('/src/', '/../'); // or some variation
}
})
}
...

Resources