1

This code works great on windows:

https://stackoverflow.com/a/11318745/637142

I am creating an application targeting netcoreapp2.1. When I run that code under Linux I am able to delete the file. If my application is running I want to prevent deletions of a specific file. How can I do that?


Further explanation

I have this code:

// this code only runs on Linux 

var fs = File.Open(@"/tmp/foo.txt", FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None);

fs.Lock(0, 0);

// Wait for 1 minute
for (var i = 0; i < 60; i++)
{
         Console.WriteLine("This is a test.");
         System.Threading.Thread.Sleep(1000);
         fs.Lock(0, 0); // this is not needed I just have it
}

// Do your work here
fs.Close();

When I run that code I am able to execute the command "rm /tmp/foo.txt" and that command successfully deletes the file. Note when I login to my Linux computer I am root.


What am I trying to solve:

I try to make my logic of my Windows application similar to the logic of my Linux application. In this case they differ.

If I place a lock like on the first example I am able to have something like:

           bool isProcessExecuting;
            try
            {
                System.IO.File.Delete(someFileContainingALock);
                isProcessExecuting = false;
            }
            catch
            {
                isProcessExecuting = true;
            }

            if (isProcessExecuting)
            {
                // prevent starting another instance of the same program
                return;
            }

            // else continue execution

For example this code enables me to prevent running multiple instances of the same application. I know there are alternative ways of doing the same thing. It will be nice if I can use the same code on Windows and Linux. And in this case my linux code will have to be different

Tono Nam
  • 34,064
  • 78
  • 298
  • 470
  • 1
    You will need to explain your scenario better. On Linux, while it's true that some othe process can issue a delete on a file your process has open, it is _not_ true that the file will actually be deleted at that moment. Your process will still have the file handle, and as long as it keeps the file open, the file will still be there. Normally, this is sufficient. If it is not sufficient in your scenario, you need to explain why, so that your _actual_ problem can be addressed, since there's no way to literally do what you're asking for, short of modifying the file access flags themselves. – Peter Duniho Aug 29 '19 at 04:40
  • 1
    In the meantime, there are a lot of informative Q&A in these two searches: https://stackoverflow.com/search?q=linux+exclusive+file+access and https://stackoverflow.com/search?q=lock+file+on+linux – Peter Duniho Aug 29 '19 at 04:42
  • _"that command successfully deletes the file"_ -- your explanation is still lacking. As I already noted, while the command will succeed, the file is not actually deleted until your process is done with it. The deletion should not affect your own process. So, why do you care if someone issues a delete command for the file? Why does it matter to you? What _actual_ problem are you trying to solve? You seem to have an [XY Problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem) here...you've asked about Y, now tell us what X is. – Peter Duniho Aug 29 '19 at 05:01

0 Answers0