Home > Net >  Best approach to check folder/file exist before read and write to the file
Best approach to check folder/file exist before read and write to the file

Time:12-22

How can I check if the folder/file exist before read and write to the file? I did some search and it shows that using "fs.stat() or fs.access() to check if a folder/file exists" but I also read some related questions (related question one, related question two) and it appears to be that I might get some kind of "race condition".

I am thinking of doing below approach to avoid "race condition", am I still going to get some kind of "race condition" with below approach?:

const checkIfFolderOrFileExist = () => {
  return new Promise((resolve, reject) => {
    fs.access(filePath, constants.F_OK, (err) => {
      if (err) {
        reject(err);
      } else {
        resolve("exist");
      }
    });
  });
};

const createFolder = () => {
  return new Promise((resolve, reject) => {
    fs.mkdir(folderPath, { recursive: false }, (err) => {
      if (err) {
        reject(err);
      } else {
        resolve("done");
      }
    });
  });
};

const writeToTheFile = (data) => {
  return new Promise((resolve, reject) => {
    fs.writeFile(filePath, data, (err) => {
      if (err) {
        reject(err);
      } else {
        resolve("Done");
      }
    });
  });
};

const callBothPromiseFunction = async () => {
  /* Does below code has a possibility that I might get some kind of "race condition"? (I am using fs.access and fs.writeFile) */
  const folderOrFileExist = await checkIfFolderOrFileExist(); //<---- using fs.access
  if (folderOrFileExist === "exist") {
    const response = await writeToTheFile(someRandomData);
    /* some other js codes */
  } else {
    await createFolder();
    const response = await writeToTheFile(someRandomData);
    /* some other js codes */
  }
};

CodePudding user response:

There is a really easy way to test that in javascript using the fs library

const fs = require('fs');

const filePath = '/path/to/file.txt';

fs.access(filePath, fs.constants.F_OK, (err) => {
  if (err) {
    console.error(`${filePath} does not exist`);
  } else {
    // file exists, you can read or write to it here
  }
});

You just have to add your reading / writing code in the "else"

CodePudding user response:

I personally never had issues with accessing a file/folder before reading/writing to it, but if you want to be sure to prevent this, you can leverage two mechanisms.

Failing promise

Instead of first checking if the folder exists, just create the folder and ignore the error when the folder already exists.

const ensureFolderExists = () => {
  return new Promise((resolve, reject) => {
    fs.mkdir(folderPath, { recursive: false }, (err) => {
      resolve('done');
    });
  });
};

const callBothPromiseFunction = async () => {
  await ensureFolderExists();
  await writeToTheFile(someRandomData);
};

The only downside is that you can't detect any other errors. For example, when you don't have the permissions to create the folder.

Recursive: true

The second option is to set the recursive flag to true. This will automatically "swallow" errors generated by already existing folders.

  • Related