mirror of
https://github.com/cirosantilli/linux-kernel-module-cheat.git
synced 2026-01-23 02:05:57 +01:00
nodejs: file io
This commit is contained in:
@@ -13188,8 +13188,10 @@ ERROR: package host-nodejs installs executables without proper RPATH
|
|||||||
|
|
||||||
Examples:
|
Examples:
|
||||||
|
|
||||||
* process
|
* `process`
|
||||||
** link:rootfs_overlay/lkmc/nodejs/command_line_arguments.js[]
|
** link:rootfs_overlay/lkmc/nodejs/command_line_arguments.js[]
|
||||||
|
* `fs`
|
||||||
|
** link:rootfs_overlay/lkmc/nodejs/file_write_read.js[]
|
||||||
|
|
||||||
=== Userland content bibliography
|
=== Userland content bibliography
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
#!/usr/bin/env node
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
// https://cirosantilli.com/linux-kernel-module-cheat#node-js
|
||||||
|
|
||||||
for (const arg of process.argv) {
|
for (const arg of process.argv) {
|
||||||
console.log(arg);
|
console.log(arg);
|
||||||
}
|
}
|
||||||
|
|||||||
20
rootfs_overlay/lkmc/nodejs/file_write_read.js
Executable file
20
rootfs_overlay/lkmc/nodejs/file_write_read.js
Executable file
@@ -0,0 +1,20 @@
|
|||||||
|
#!/usr/bin/env node
|
||||||
|
|
||||||
|
// https://cirosantilli.com/linux-kernel-module-cheat#node-js
|
||||||
|
|
||||||
|
const assert = require('assert');
|
||||||
|
const fs = require('fs');
|
||||||
|
const path = require('path');
|
||||||
|
|
||||||
|
const file_path = path.basename(__filename) + '.tmp';
|
||||||
|
const data = 'asdf\nqwer\n';
|
||||||
|
const encoding = 'utf8';
|
||||||
|
|
||||||
|
// Write to file.
|
||||||
|
//
|
||||||
|
// Error handling through exceptions:
|
||||||
|
// https://stackoverflow.com/questions/15543235/checking-if-writefilesync-successfully-wrote-the-file
|
||||||
|
fs.writeFileSync(file_path, data, encoding);
|
||||||
|
|
||||||
|
// Read from file.
|
||||||
|
assert.strictEqual(fs.readFileSync(file_path, encoding), data);
|
||||||
@@ -108,10 +108,10 @@ int file_write(char *path, char *write_string) {
|
|||||||
|
|
||||||
int main(void) {
|
int main(void) {
|
||||||
char *path = LKMC_TMP_FILE;
|
char *path = LKMC_TMP_FILE;
|
||||||
char *input = "asdf\nqwer\n";
|
char *data = "asdf\nqwer\n";
|
||||||
|
|
||||||
/* Write entire string to file at once. */
|
/* Write entire string to file at once. */
|
||||||
if (file_write(path, input) == -1) {
|
if (file_write(path, data) == -1) {
|
||||||
LKMC_IO_ERROR("file_write", path);
|
LKMC_IO_ERROR("file_write", path);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -121,7 +121,7 @@ int main(void) {
|
|||||||
if (output == NULL) {
|
if (output == NULL) {
|
||||||
LKMC_IO_ERROR("file_read", path);
|
LKMC_IO_ERROR("file_read", path);
|
||||||
}
|
}
|
||||||
assert(strcmp(input, output) == 0);
|
assert(strcmp(data, output) == 0);
|
||||||
free(output);
|
free(output);
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -131,7 +131,7 @@ int main(void) {
|
|||||||
if (size == -1) {
|
if (size == -1) {
|
||||||
LKMC_IO_ERROR("file_size", path);
|
LKMC_IO_ERROR("file_size", path);
|
||||||
}
|
}
|
||||||
assert((size_t)size == strlen(input));
|
assert((size_t)size == strlen(data));
|
||||||
}
|
}
|
||||||
|
|
||||||
return EXIT_SUCCESS;
|
return EXIT_SUCCESS;
|
||||||
|
|||||||
Reference in New Issue
Block a user