Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to intentionally make a file be "EBUSY: resource busy or locked"

Tags:

file

locking

In all my search attempts, I could only find people trying to fix an "EBUSY: resource busy or locked" error.

What I want is to intentionally put a file in this state (temporarily). How can this be done?

I tried using fs.open, fs.createReadStream and fs.createWriteStream from NodeJS, but for my surprise this doesn't make the file busy.

How can I do this? Preferably in NodeJS, but it can be in any language.

like image 704
Pedro A Avatar asked Dec 05 '25 08:12

Pedro A


1 Answers

On .NET Core, you can use FileStream.Lock().

Here's the API documentation from .NET Core 3.1 which includes a sample .NET program also that does locking a file exclusively, which should trigger the 'EBUSY: resource busy or locked' error in node JS programs trying to access the same file.

The FileStream.Lock() API call signature is:

public virtual void Lock (long position, long length);

You can call it with position set to 0 and length set to the length of the file. FileStream.Lock() on Windows allows locking file ranges.

like image 80
vvg Avatar answered Dec 07 '25 23:12

vvg