Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Wait for file removal after close

I'm working on a quota manager implemented in C/C++ under OS X that limit the maximum size of a specific folder. In my project there are generic thread workers that may perform either writing new data to the folder or moving data from the folder to server.

This way, sometimes, a removing task may delete a file that is still in the process of being written into the disk by another thread.

can I somehow prevent the file from being remove during writing process. I can implement mechanism of my own (using flags, locks, etc...) but i believe it should be filesystem property.

I've tried the following code to check if remove fails of the file is still opened, but it actually succeed.

#include <stdio.h> 
int main() {
    FILE *fp;
    fp=fopen("./x", "w");
    fprintf(fp, "Testing...\n");
    if( remove( "./x" ) != 0 )
        perror( "Error deleting file" );
    else
        puts( "File successfully deleted" );
    return 0;
}

how can i enfoce file closed before deleting ?

like image 313
Zohar81 Avatar asked Feb 12 '26 09:02

Zohar81


1 Answers

Use the lsof command to check which are the files open. (lsof - list open files)

For example you can invoke lsof with -F p and it will output the pid of the processes prefixed with 'p':

$ lsof -F p /some/file
p1234
p4567

Refer : lsof man page

like image 138
Ani Menon Avatar answered Feb 14 '26 00:02

Ani Menon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!