0

There is a folder which is being created within another folder in my home directory, and I want to be able to analyse the contents of this folder, however, I have a problem: as the folder is created, it is then immediately deleted by the same program that creates it. Is there any way that I can allow it to create the folder, but then not to delete it? I am running Ubuntu GNOME 15.10 with GNOME 3.18.

16
  • Just as a suggestion, you might also want to ask this on Unix & Linux
    – Daniel
    Nov 10, 2015 at 20:14
  • @Daniel the "also" is a problem - cross-posting is generally unwelcome.
    – muru
    Nov 10, 2015 at 20:15
  • Sorry, wasn't aware of that ^
    – Daniel
    Nov 10, 2015 at 20:16
  • How soon is "immediately"?
    – muru
    Nov 10, 2015 at 20:18
  • @muru: Less than a second. Just basically a flash.
    – user364819
    Nov 10, 2015 at 20:19

1 Answer 1

2

There are no permission settings that can do this, even using ACLs, but you can have a race with the program, using inotifywait:

In one terminal, I ran:

while sleep 0.5; do if mkdir foo/bar; then echo foo; rmdir foo/bar; fi; done

As you can see, this creates a directory, echoes a message and removes the directory - all of which happens very quickly by human standards.

And in another:

while true
do
    if inotifywait -e create foo
    then
        chmod -w foo
        if [[ -d foo/bar ]]
        then
            break 
        else
            chmod +w foo
        fi
    fi
done

This command waits for creation of anything in foo, then removes write permissions from foo, and tests if it was in time for catching the sub directory. If not, it adds back the write permission, and starts again.

After waiting a bit:

foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
foo
rmdir: failed to remove ‘foo/bar’: Permission denied
mkdir: cannot create directory ‘foo/bar’: File exists
mkdir: cannot create directory ‘foo/bar’: File exists
mkdir: cannot create directory ‘foo/bar’: File exists
mkdir: cannot create directory ‘foo/bar’: File exists

This is relying on a race condition, so whether you can catch it is a matter of luck.


foo/bar is the directory that the program makes. If you don't know the name of it, you can try other ways:

  • ensure that there's nothing else in the directory, so when a subdirectory is created, foo/ is non-empty. Some tests for non-empty directory are available in this U&L post:

    shopt -s nullglob
    if [ -n "$(ls -A foo/)" ]
    then
        break
    

    This worked fast enough.

  • read the name of the created directory from inotifywait's output:

    if dir=$(inotifywait -e create --format '%f' foo)
    then
        chmod -w foo
        if [[ -d foo/$dir ]]
    

    This didn't work (yet) for me; the overhead of creating subshell seems to make it just sufficiently slow enough for the other loop to win each time. Maybe coded in C using the inotify API, this could work.

6
  • The directory won't be empty, and if I empty it I could break the mechanism which creates this mystery folder.
    – user364819
    Nov 10, 2015 at 21:52
  • @ParanoidPanda how would you do that?
    – muru
    Nov 10, 2015 at 21:53
  • How could I do what?
    – user364819
    Nov 10, 2015 at 21:57
  • @ParanoidPanda "break the mechanism which creates this mystery folder"?
    – muru
    Nov 10, 2015 at 22:08
  • This folder is filled with configuration files for the application in question and it can't start to perform any action without them.
    – user364819
    Nov 10, 2015 at 22:17

You must log in to answer this question.