472,958 Members | 2,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

How to detect if file is deleted

Hi,

I have program, which opens file at the startup and logs error
messages to the file, file handle is closed at the end of the program.
However if file is deleted in-between, program do not report any error
while writing to the open file handle. On Windows, file shows-up again
in explorer and automatically deleted finally when program ends. On
Unix, same thing happens if file is on nfs mounted drive, but in this
case, actual file is deleted and some dummy file is showsup and logs
goes to that file, which again automatically removed once program
terminates.

If file is deleted, I want to re-create the file and log to the new
file, which should not disappear when program terminates.

Any thoughts?

Thanks,
Kiran
Nov 14 '05 #1
6 5133
On 16 Feb 2005 00:52:07 -0800, in comp.lang.c , ki**********@gmail.com
(Kiran) wrote:
Hi,

I have program, which opens file at the startup and logs error
messages to the file, file handle is closed at the end of the program.
However if file is deleted in-between, program do not report any error
while writing to the open file handle. ....
If file is deleted, I want to re-create the file and log to the new
file, which should not disappear when program terminates.


Open the file for each write, close it afterwards, and handle the error
that arises if the file disappears between writes. This may be
time-expensive due to all the file-open/closes but oughtnt to be on a
decent OS.

writedata()
{
open file for append
if failure, open for create/write
if still failure, gracefully notify user - file cannot be created
write data
close file
}

--
Mark McIntyre
CLC FAQ <http://www.eskimo.com/~scs/C-faq/top.html>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>

----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Nov 14 '05 #2

Kiran wrote:
Hi,

I have program, which opens file at the startup and logs error
messages to the file, file handle is closed at the end of the program. However if file is deleted in-between, program do not report any error while writing to the open file handle.

What you really need is file-locking. However, that is beyond the scope
of this newsgroup, as there is no way to do that in ISO standard C.
There probably is a way offered by your platform.

Brian

Nov 14 '05 #3
Mark McIntyre <ma**********@spamcop.net> writes:
On 16 Feb 2005 00:52:07 -0800, in comp.lang.c , ki**********@gmail.com
(Kiran) wrote:
I have program, which opens file at the startup and logs error
messages to the file, file handle is closed at the end of the program.
However if file is deleted in-between, program do not report any error
while writing to the open file handle.

...

If file is deleted, I want to re-create the file and log to the new
file, which should not disappear when program terminates.


Open the file for each write, close it afterwards, and handle the error
that arises if the file disappears between writes. This may be
time-expensive due to all the file-open/closes but oughtnt to be on a
decent OS.

writedata()
{
open file for append
if failure, open for create/write
if still failure, gracefully notify user - file cannot be created
write data
close file
}


If you're worried about the file being deleted while the program is
running (presumably by something external to the program), you might
also need to worry about the file being deleted and recreated behind
your program's back. There's a chance that the file could be deleted,
and another file of the same name created, between calls to
writedata().

There may be some system-specific things you can do to prevent the
file from being deleted while the program is running.

Another option might be to save all the error messages internally, and
write them out all at once just before the program terminates. This
may be unacceptable if you want to see the messages before the program
finishes, or if the program might be killed before it can write the
log.

How you want to handle this may depend on whether you're concerned
about the file being deleted accidentally, or being deleted
deliberately and maliciously (e.g., by an attacker trying to cover his
tracks). In the former case, it may suffice to create the file with
an obscure name (perhaps using tmpnam()) and rename it when you're
finished.

Since standard C, and therefore this newsgroup, only barely
acknowledges the existence of anything external to the program, you
should probably try a newsgroup that's specific to your system (e.g.,
comp.unix.programmer or comp.os.ms-windows.programmer.win32).

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Nov 14 '05 #4
In article <34*************************@posting.google.com> ,
Kiran <ki**********@gmail.com> wrote:
:I have program, which opens file at the startup and logs error
:messages to the file, file handle is closed at the end of the program.
:However if file is deleted in-between, program do not report any error
:while writing to the open file handle.

It's not supposed to in POSIX. POSIX.1 says specifically that
the removal of the file contents shall be postponed until
all references to the file have been closed. Until that time,
you can work with the file.

The standard C function tmpfile() is pretty much based upon this
assumption that the file will stick around until it is closed.
[It could be implimented a different way, though, such as by setting
an attribute that marked it for closure upon deletion.]
:On Windows, file shows-up again
:in explorer and automatically deleted finally when program ends. On
:Unix, same thing happens if file is on nfs mounted drive, but in this
:case, actual file is deleted and some dummy file is showsup and logs
:goes to that file, which again automatically removed once program
:terminates.

Ummm, nfs v1 and v2 are theoretically stateless, which makes it difficult
to refer to deleted files, so you should expect kind of odd results...
:If file is deleted, I want to re-create the file and log to the new
:file, which should not disappear when program terminates.

I can't think of anything within Standard C, but chances are you
can use one of the common extensions to fstat() the file and examine
the link count. You don't want to use stat() for this because stat()
relies on the pathname, and if the file was deleted and another
file with that name was created, stat() would show the file as existing.
Some operating systems have extensions that allow you to monitor
the state of a file and get notified when the file gets deleted.
I'm thinking in particular of SGI's IRIX 'fam' (file alteration monitor).
That wouldn't be portable to Windows though, or even to many other
unix.
--
Will you ask your master if he wants to join my court at Camelot?!
Nov 14 '05 #5

Kiran wrote:
Hi,

I have program, which opens file at the startup and logs error
messages to the file, file handle is closed at the end of the program. However if file is deleted in-between, program do not report any error while writing to the open file handle. On Windows, file shows-up again in explorer and automatically deleted finally when program ends. On
Unix, same thing happens if file is on nfs mounted drive, but in this
case, actual file is deleted and some dummy file is showsup and logs
goes to that file, which again automatically removed once program
terminates.

If file is deleted, I want to re-create the file and log to the new
file, which should not disappear when program terminates.

Any thoughts?

Thanks,
Kiran


On a Unix System, if a file has been opened by one processs and some
other process deletes the same file, only the directory entry for that
file is removed whereas the data blocks remain allocated. The process
that has opened the file is allowed to do read/write operations on
that file. The disk blocks will be freed only after the last close on
this file.
So, what you can do is before closing the file, check whether that file
is deleted (use fstat for that). If the file is deleted, create the
file
again (with the same name if you want) and then read the contents from
the deleted file and write to the this new created file.

Nov 14 '05 #6
On Wed, 16 Feb 2005 22:55:33 +0000, Walter Roberson wrote:
In article <34*************************@posting.google.com> ,
Kiran <ki**********@gmail.com> wrote:
:I have program, which opens file at the startup and logs error
:messages to the file, file handle is closed at the end of the program.
:However if file is deleted in-between, program do not report any error
:while writing to the open file handle.

It's not supposed to in POSIX. POSIX.1 says specifically that
the removal of the file contents shall be postponed until
all references to the file have been closed. Until that time,
you can work with the file.
Perhaps not even then if there other hard links to the file. In a
desperate bid for topicality I would point out that the standard says:

"The remove function causes the file whose name is the string pointed to
by filename to be no longer accessible by that name."

So the remove() function isn't required in any sense to "delete" a file.
The standard C function tmpfile() is pretty much based upon this
assumption that the file will stick around until it is closed.
[It could be implimented a different way, though, such as by setting
an attribute that marked it for closure upon deletion.]
Deletion upon closure? removge() upon closure perhaps if it is implemented
using an actual filename.
:On Windows, file shows-up again
:in explorer and automatically deleted finally when program ends. On
:Unix, same thing happens if file is on nfs mounted drive, but in this
:case, actual file is deleted and some dummy file is showsup and logs
:goes to that file, which again automatically removed once program
:terminates.

Ummm, nfs v1 and v2 are theoretically stateless, which makes it
difficult to refer to deleted files, so you should expect kind of odd
results...
Depends on whether the files are referred to by name or by a token/handle.
:If file is deleted, I want to re-create the file and log to the new
:file, which should not disappear when program terminates.

I can't think of anything within Standard C, but chances are you can use
one of the common extensions to fstat() the file and examine the link
count. You don't want to use stat() for this because stat() relies on
the pathname, and if the file was deleted and another file with that
name was created, stat() would show the file as existing.


[OT]
fstat() alone is no good, e.g. a rename won't change the link count, but
you typically do want to detect this. The normal approach is to use both
stat() and fstat() and compare the filesystem and inode numbers to verify
that the filename refers to the same file that you have open. [/OT]

Lawrence
Nov 14 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: ajikoe | last post by:
Hello, If I use os.remove(fileName), does it always assure that the code will move to the next code only if the fileName is deleted completely? Pujo
5
by: Brad Wood | last post by:
The delete method of the FileSystemObject.FileObject does not return a result. If permissions disallow deletion, it will not raise an error. Conversely, if the delete method does succeed, a call...
6
by: Jonathan | last post by:
I am trying to upload a new version of a file that already exists on my web site. It asks me to overwrite and I choose yes. But when I go to the web page, even a half hour after the upload, the...
3
by: Bill Tepe | last post by:
I have a need to insert rows into an Audit type table when values change in certain fields in a table. I thought I could do this via a trigger. However, on requirement is to include in the audit...
9
by: Hannu | last post by:
Hi. i have hundreds of queries and forms. I know there are a lot of forms and also queries that are not in use and could be deleted, but problem is how i know which are not in use? it is very...
6
by: Ana | last post by:
Hi! I have problems with the following scenario: My application is developed using C# under .NET. It must run on all Windows versions starting from Windows 98. The user must open different...
6
by: shyam | last post by:
Hi All I had raised a simillar query in an earlier post ...
6
by: Zytan | last post by:
I ran through the VB Guided Tour some time ago. In particular, the "Managing Your Records: Using Data in Your Program" section: http://msdn2.microsoft.com/en-us/library/t25kbx0s(VS.80).aspx ...
5
by: Doug Bell | last post by:
Hi I have a DataGrid that has a DataView as its DataSource. I need to detect when a New Row is added or when a Row is Deleted so that I change data in the underlying DataTable. I found that...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Aliciasmith | last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
1
by: Teri B | last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course. 0ne-to-many. One course many roles. Then I created a report based on the Course form and...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
2
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.