创建文件时,Node.js文件许可权设置为000如何创建具有读写权限或更改权限的文件?

问题描述

我正在尝试通过Node.js创建文件,并且如果可能的话,想专门使用FS.openSync()方法。当前,我有一个非常简单的类来创建文件。该课程将得到进一步发展,但只有解决了我的问题后,我才能继续上课。如何设置节点通过“ FS.writeSync()方法”创建的文件的文件用户权限,以便可以访问该文件。创建文件后,我认为权限设置为001或000,这是一些荒谬且不需要的用户权限设置。 我在下面复制并粘贴了我的小类,下面是我用来创建该类实例并运行创建该文件的方法的文件。

  • 我的文件类别
/** @format */

/* eslint-disable */
const fs = require('fs');
const {O_RDONLY,O_WRONLY,O_RDWR,O_CREAT,O_TRUNC,O_APPEND} = require('fs').constants;
const {Buffer} = require('buffer');
/* eslint-enable */

class TextBasedFile {
    constructor(path) {
        this.path = path;
        this.buf = Buffer.allocUnsafe(400000000); // 40MB (Should be more than enough)
    }

    create() {
        const fd = fs.openSync(this.path,O_WRONLY);
        console.log('\n\n\n\nRESULT: ' + fd);
        return fd;
    }

    truncate() {
        const fd = fs.openSync(this.path,O_TRUNC);
        console.log('\n\n\n\nRESULT: ' + fd);
        return fd;
    }
}

module.exports.TextBasedFile = TextBasedFile;
  • 我用于创建新文本文件的代码

/** @format */

const {TextBasedFile} = require('./unity/iof');

const sumfile = new TextBasedFile('./someFile.txt');

sumfile.create();



如您所见,我的设置非常简单,唯一的问题是,如果没有sudo命令,我将无法访问创建的文件。我什至无法从Ubuntu用户访问该文件。以下是我的应用目录中所有文件的打印输出,以及每个文件的用户权限。您会看到“ someFile.txt”的用户权限对我来说不起作用。

drwxrwxr-x   7 ajay ajay  4096 Oct  1 10:43 .
drwxrwxr-x   4 ajay ajay  4096 Sep 30 20:03 ..
-rwxrwxr-x   1 ajay ajay   533 Sep 30 22:16 app.js
-rwxrwxr-x   1 ajay ajay   858 Sep 30 22:52 .developerNotes.txt
-rwxrwxr-x   1 ajay ajay  1005 Sep 30 22:43 .eslintrc.json
drwxrwxr-x   8 ajay ajay  4096 Sep 30 23:38 .git
-rwxrwxr-x   1 ajay ajay   551 Oct  1 09:26 .gitignore
-rwxrwxr-x   1 ajay ajay  1075 Sep 30 20:03 LICENSE
-rwxrwxr-x   1 ajay ajay  1115 Sep 26 13:32 localhost-cert.pem
-rwxrwxr-x   1 ajay ajay  1704 Sep 26 13:32 localhost-privkey.pem
drwxrwxr-x 107 ajay ajay  4096 Sep 30 20:32 node_modules
-rwxrwxr-x   1 ajay ajay   980 Sep 30 20:31 package.json
-rwxrwxr-x   1 ajay ajay 36991 Sep 30 20:31 package-lock.json
-rwxrwxr-x   1 ajay ajay   466 Sep 30 22:11 .prettierrc
drwxrwxr-x   7 ajay ajay  4096 Sep 30 19:36 public
-rwxrwxr-x   1 ajay ajay   335 Sep 30 20:03 README.md
---------x   1 ajay ajay     0 Oct  1 10:43 someFile.txt
drwxrwxr-x   2 ajay ajay  4096 Sep 30 19:33 unity
drwxrwxr-x   2 ajay ajay  4096 Sep 30 20:05 .vscode

如果有人知道如何创建文件,其权限设置允许我从节点内部访问文件并使用它,那么....

...那太棒了。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)