最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Node Js: Test to see if a file is locked for editing by another process - Stack Overflow

programmeradmin0浏览0评论

I am writing some code in NodeJs, and want to check to see if the file is in use by another process, if it is then do nothing, if it isn't in use do something.

fs.stats is kind of a, at this instant what is the file size. And doesn't tell me me if its currently in use from another process.

Not sure what else to try.

What is the best way to tell if a file is currently locked for editing by another process, before trying to access the file using nodejs?

I am writing some code in NodeJs, and want to check to see if the file is in use by another process, if it is then do nothing, if it isn't in use do something.

fs.stats is kind of a, at this instant what is the file size. And doesn't tell me me if its currently in use from another process.

Not sure what else to try.

What is the best way to tell if a file is currently locked for editing by another process, before trying to access the file using nodejs?

Share Improve this question asked Jun 7, 2016 at 22:17 shaunshaun 1,2732 gold badges19 silver badges46 bronze badges 3
  • I don't think fs really locks the file down while editing, it does the edit if it can, and then calls the callback, and if it wasn't successful the callbacks first argument would be an error. – adeneo Commented Jun 7, 2016 at 22:28
  • You are right, node doesn't lock the file, but if a file is currently being uploaded by FTP, then the FTP Server has it locked for editing. At that point in time I would like to ignore the file. If it is no longer locked for editing by an outside process, then and only then do I want to interact with the file. – shaun Commented Jun 7, 2016 at 22:31
  • 1 If the file is truly locked, trying to access it with fs should generate an error in the callback that you can handle. The other option would be to set some sort of flag in a map when a file is uploaded, and then clear the flag when the FTP upload is done, and then use the map to see if the file is finished or not. – adeneo Commented Jun 7, 2016 at 22:33
Add a comment  | 

2 Answers 2

Reset to default 15

Code I ended up using after some instruction from the comments left.

var delInterval = setInterval(del(), 1000);

function del(){
    fs.open(filePath, 'r+', function(err, fd){
        if (err && err.code === 'EBUSY'){
            //do nothing till next loop
        } else if (err && err.code === 'ENOENT'){
            console.log(filePath, 'deleted');
            clearInterval(delInterval);
        } else {
            fs.close(fd, function(){
                fs.unlink(filePath, function(err){
                    if(err){
                    } else {
                    console.log(filePath, 'deleted');
                    clearInterval(delInterval);
                    }
                });
            });
        }
    });
}

I came up with a smaller approach, so I'd like to share it:

function isWritable(filePath) {
  let fileAccess = false
  try {
      fs.closeSync(fs.openSync(filePath, 'r+'))
      fileAccess = true
  } catch (err) {
      console.log('can not open file!')
  }
  return fileAccess
}    
发布评论

评论列表(0)

  1. 暂无评论