Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to check if any files are open in a directory?

I am trying to delete all files in a folder, but if a file is left open, it will not delete. I need to check the folder for any open files, write their names to a text document, and then close the open files. As I don't have much experience, I am first trying to check one file in the same folder, then all in the same folder, then all in a different folder. I don't want to be to needy or demanding, so just some help with the first step would be nice.

I don't have a ton of experience coding, but I have tried using fstream and (name.is_open). I may be using them wrong, so I have not yet ruled them out.

// ifstream::is_open
#include <iostream>     // std::cout
#include <fstream>      // std::ifstream

void checkFiles() {
int done = 0;
while(done != 1){
    std::cout << "Enter 0 for continue or 1 for done: ";
    std::cin >> done;

    std::ifstream ifs ("test.txt");

    if (ifs.is_open()) {
        // Print that file is open, then close
        std::cout << "File is open\n";
        std::ifstream.close():
    }
    else {
        std::cout << "File not open\n";
    }
}

For this bit of code, if the file is open, it should say "File is open." If not, it should say "File not open"

Even if I force quit the .txt file, it still says that it is open.

Eventually, I want to have a new file that displays what files were open, as well as closing all the open files.

like image 643
Jackson148 Avatar asked Dec 23 '22 22:12

Jackson148


1 Answers

Standard C++ offers us the filesystem library to handle files and directories (standardized in C++17). However, checking which files are open is not - as far as I can tell - a feature of that library.

The is_open() method for std::fstream's is something completely different than what you're trying to check for: It tells you whether the particular stream object is in an open state (which would mean association with an open file) - and it doesn't use the OS to check which files are open. Typically, it's just a way to check whether you've closed it someplace else in your own program; at most, it might ensure that the OS has not unilaterally closed the OS-side file access handle. So, you won't get anywhere in that direction.

I also believe, though I'm not 100% certain, that Boost doesn't have a library which offers this capability, either. Boost's filesystem library is almost identical to std::filesystem, as the latter was based on it.

So, to the best of my knowledge, you either need to directly use operating-system-specific calls to do this, or look for a library offering this functionality, elsewhere.

If you haven't found anything else, you could track how this is currently done with what's available in userspace. There's a utility called lsof. It's available on some operating systems based on Linux, Darwin, FreeBSD and Solaris (e.g. available on MacOS). It's maintained here. The source code seems to be rather atrocious C. An intrepid developer could parse that mess, figure out what it does, extract the parts relevant for your specific use case, and refactor it into a reasonable, readable and short(ish) C++ function. I realize you (OP) might not be up for it at this point, but - maybe someone else reading this answer will get inspired to do it.

like image 189
einpoklum Avatar answered Jan 02 '23 05:01

einpoklum