473,583 Members | 3,134 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5279
On 16 Feb 2005 00:52:07 -0800, in comp.lang.c , ki**********@gm ail.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**********@s pamcop.net> writes:
On 16 Feb 2005 00:52:07 -0800, in comp.lang.c , ki**********@gm ail.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.progr ammer or comp.os.ms-windows.program mer.win32).

--
Keith Thompson (The_Other_Keit h) 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************ *************@p osting.google.c om>,
Kiran <ki**********@g mail.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************ *************@p osting.google.c om>,
Kiran <ki**********@g mail.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
2191
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
7332
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 to FileExists directly afterward may return true because the system hasn't updated yet (or whatever actually goes on). Is there any way to...
6
2041
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 orginal content of the page is still there. I have tried uplaoding several times in IE and FTP voyager. There are no errors reported during the upload....
3
9294
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 both the old and new value. Is there a "simple" way to do this? I know I could query the table before the update and compare to what the new...
9
1411
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 hard job to find it out one by one, so is there any better ways? hannu
6
5146
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 documents (txt, MS Office files, pdf, pictures,…) from inside my app. It must start the file with the adequate external program (Notepad, MS Office...
6
2046
by: shyam | last post by:
Hi All I had raised a simillar query in an earlier post http://groups.google.com/group/comp.lang.c++/browse_thread/thread/f371af248d90ead6/1d054627402539e1?lnk=gst&q=ofstream+write+failure&rnum=1#1d054627402539e1 Basically I have a file which is opened through ofstream for writing. Now I want to check if the file has been deleted or...
6
3165
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 This explains how to create a SQL database, which is stored as an .mdf file: http://msdn2.microsoft.com/en-us/library/ms172599(VS.80).aspx It is just...
5
2318
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 MyDataGrid_CurrentCellChanged doesn't fire on a deletion or on a New Row if there was no previous rows. Can someone advised the simplest way to...
0
7827
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7936
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
8195
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5701
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5375
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3845
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2334
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
0
1158
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.