2

I am using NSFileHandle's fileHandleForWritingAtPath to open a handle to a file and write into it (append to the end of it). Since I always need to write into it I never close the handle or check that it's there when writing into it.

I need to support a case were for some reason a third party came and deleted the file, that is I have a valid handle that is going into a file that is no longer there (the kernel should know the handle is bad). Anticipating this I wrap the call to writeData: with a try catch, I expect the method to throw an exception (why does it return void and not written bytes is beyond me).

It does not. Why?

Cœur
  • 37,241
  • 25
  • 195
  • 267
user1730969
  • 462
  • 3
  • 10
  • Are you checking the return value of fileHandleForWritingAtPath for nil. – D25 Oct 09 '12 at 07:59
  • yes of course, the flow is , open a handle write into it, after some time has passed someone deletes said file. – user1730969 Oct 09 '12 at 08:03
  • What exactly do you mean by "someone deletes said file?" I'm asking this because without any more information my guess is that the file is not actually deleted. iOS is - among other things - a Unix-based system, so just removing an entry from a directory does not "delete" the file, and an open handle would keep the file in the file system. – Monolo Oct 09 '12 at 08:10
  • You are correct, didn't consider that. the file does continue to exist somewhere (I guess the inode has a reference count on it), I can see this by checking the file offsetInFile and see that it grows. Ok then I'll refine my question, can I lock the file? – user1730969 Oct 09 '12 at 08:24

0 Answers0