Solved

Corrupted file at system level (cross-linked)

Posted on 2008-10-23
8
546 Views
Last Modified: 2013-11-14
We have two programs that run on a Windows XP Embedded Kiosk machine.  One program creates a file by  opening it, writing a character, and then closing it.  The other program simply checks for the
existence of the file like so:

if(PathFileExistsA( "C:\\FOLDER\FILENAME.TXT" ))

This file is written to ONLY when the program launches and is removed ONLY when the program closes (i.e. not continuosly).  It is created like so:

m_pStartUpStream = fopen(strStartupFileName, "w");
if(m_pStartUpStream)
{
   fprintf(m_pStartUpStream, "1");
   fclose(m_pStartUpStream);
}

Shutting the machine off periodically corrupts this file (ie. crosslinks) to the point where chkdsk has to be used to clean it up.

Any thoughts?
0
Comment
Question by:CAnuszczyk
  • 4
  • 4
8 Comments
 
LVL 12

Expert Comment

by:hfraser
ID: 22785970
Cross linked files are usually created when a system is shut down without going through the normal shutdown process, such as a crash, powering off abruptly, etc.. The OS doesn't have the chance to properly flush queued writes, and cross links can result.

If the problem is due to power loss, whether it's flaky power, users shutting it down by turning off a switch, etc., a UPS can be used to give the system a chance to shut down gracefully by detecting the switch to battery.

You might also consider a screen that provides an adminstrator with an option to do a clean shutdown.

If none of these works, you'll be stuck doing a disk recovery as part of the system startup.
0
 
LVL 2

Author Comment

by:CAnuszczyk
ID: 22786378
So, opening and closing files may or may not flush the buffers to disk?  
The particular files I'm talking about get corrupted even though they've been closed for some time (i.e. not being written to during the inadvertant shutdown).
0
 
LVL 12

Expert Comment

by:hfraser
ID: 22788197
Not immediately, but the windows pretty small if the code you posted is all this program does. But it depends upon how often the file is created/deleted. Is this being used like a semaphore between two programs, or to serialize the running of two programs at boot, or ...?

If it's once to start up the second app at boot time, I'd say my analysis is pretty unlikely. If it's every second, my odds get better.

Depending upon what you're trying to do, there may be a better way if you have the flexibility to make changes.
0
 
LVL 2

Author Comment

by:CAnuszczyk
ID: 22790215
It's funny.  I've installed SQL Server express 2005 and I've created a program that just inserts 20K records at a time.  I ran 10 instances of it and waited until over 100K records should have been added.  I then turned off the machine - restarted - no corruption at all. I did this a series of times with the same results.  Either they turn off disk write caching (software and hardware levels) or I'm doing something wrong with my writes.  I'm guessing that the corrupted file originally reported above was corrupted by some other file getting corrupted - thoughts?
0
Do email signature updates give you a headache?

Constantly trying to correctly format email signatures? Spending all of your time at every user’s desk to make updates? Want high-quality HTML signatures on all devices, including on mobiles and Macs? Then, let Exclaimer solve all your email signature problems today!

 
LVL 12

Expert Comment

by:hfraser
ID: 22792327
Databases usually handle the filesystem differently in to provide the ability to rollback multi-step transactions if the something (including the OS) fails. Inserting thousands of records isn't doing an apples-apples comparison.


I can't offer any other explanation for the corrupted filesystem. Cross-linked files mean that blocks on the filesystem are linked to more than one file in the filesystem. Access to physical media is fairly well protected in XP, so you're looking for an scenario where the filesystem has actually corrupted basic filesystem information. While it's not impossible, the most common reason is blocks not being flushed to disk because the OS did no shut down cleanly.
0
 
LVL 2

Accepted Solution

by:
CAnuszczyk earned 0 total points
ID: 22813407
I found the problem.  When opening a file, you must use the 'c' flag - which is the commit flag.  This affects any flush() or _flushall() calls.  From the docs:

"c
  Enable the commit flag for the associated filename so that the contents of the file buffer are written directly to disk if either fflush or _flushall is called."

So basically, the fopen needs to look like so (notice the addition of the fflush()):

m_pStartUpStream = fopen(strStartupFileName, "wc");
if(m_pStartUpStream)
{
   fprintf(m_pStartUpStream, "1");
   fflush(m_pStartUpStream);
   fclose(m_pStartUpStream);
}

With this change made throughout our software, corruption seems to have been eliminated.
0
 
LVL 12

Expert Comment

by:hfraser
ID: 22814218
I'd like to add this to my knowledgebase, but none of the docs I've looked at show the "c" option on the mode doing waht you've suggested.  Where are you reading this?

BTW, glad you found the problem. Is this, then, implicitly doing an fsync? Do you know why the flush made a difference so long after the file had been closed?
0
 
LVL 2

Author Comment

by:CAnuszczyk
ID: 22823733
check out

http://msdn.microsoft.com/en-us/library/yeby3zcb.aspx
The 'c" option is a Windows only command.  This is not standard C.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A Bare Metal Image backup allows for the restore of an entire system to a similar or dissimilar hardware. They are highly useful for migrations and disaster recovery. Bare Metal Image backups support Full and Incremental backups. Differential backup…
If you get continual lockouts after changing your Active Directory password, there are several possible reasons.  Two of the most common are using other devices to access your email and stored passwords in the credential manager of windows.
The viewer will learn how to successfully create a multiboot device using the SARDU utility on Windows 7. Start the SARDU utility: Change the image directory to wherever you store your ISOs, this will prevent you from having 2 copies of an ISO wit…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now