-2

In my code I am trying to delete a directory having files and subdirectories using rm -rf .

While deleting I am getting the error message , "Directory not empty". This is happening because rm -rf recursively deletes the files inside the directory and comes out to delete the directory . Meanwhile, other thread creates new file in the same directory.

Is there a way to solve this issue. I want to force delete the directory and if possible lock the directory for writing and then delete it. I am using linux and C++.

Nancy Garg
  • 63
  • 8
  • 2
    *Besides* the obvious answer of stopping the process spamming the directory with new files before the delete? Also, this problem appears agnostic to C++. That it was the language you chose to punt the invoke of `rm -rf` from seems irrelevant, though I expect the impending sense of doom of the mystery process that has no home in which to write its files will be another problem regardless. Or is that the real question: how to kill all processes that have open file descriptors on your directory tree so you can eradicate it without remorse? – WhozCraig Feb 24 '22 at 12:50
  • If the writing process needs the directory structure to write in, how about changing the deleting process to only delete files and not directories? – Mark Setchell Feb 24 '22 at 12:59
  • Your question leaves too much unspecified. Why do you want to delete a directory while a program/thread is writing files to it? (Or, conversely, why do you want to write files to a directory that is being deleted?) How is the program that writes files affected if the directory does not exist? (Splitting hairs) Is it the "Directory not empty" message that concerns you, or the fact that the directory itself cannot be deleted? – Peter Feb 24 '22 at 14:40
  • @Peter: I am not sure which process/thread is writing in it. I am not even concerned about that. My concern is to delete the directory and this "Directory not empty" message is also my concern. If the program got affected which is writing , I will look in to that later. – Nancy Garg Feb 24 '22 at 18:01
  • @MarkSetchell: Yeah that will work, but that is not my requirement. I want to delete the whole directory . – Nancy Garg Feb 24 '22 at 18:01
  • @WhozCraig: idont want to kill the process. just want to remove my directory. – Nancy Garg Feb 24 '22 at 18:03
  • 1
    @NancyGarg I suggest your goal is backward. If you want to delete a directory, you need to be clear on why it's there in the first place and why a process continues to write to it. Understanding what the writing process is doing, and why, is important to that. For example, if the writing process explicitly recreates all or part of the directory/file structure (which is a fair bet, since it's writing to it) there is no viable solution to your question as asked since it won't matter if the removal of the directory succeeds - the directory tree will still be recreated. – Peter Feb 24 '22 at 21:25

1 Answers1

1

If I have understood your question correctly, you want to stop the processes writing to a particular directory. To proceed with rm

First, we are going to identify the process writing to a directory with fuser --mount <PATH>.

Then we are going to pause those processes:

  • kill -STOP <PID>
  • kill -CONT <PID>