Solved

Corrupted file at system level (cross-linked)

Posted on 2008-10-23
8
553 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 
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
 
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

SharePoint Admin?

Enable Your Employees To Focus On The Core With Intuitive Onscreen Guidance That is With You At The Moment of Need.

Question has a verified solution.

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

Configuring Remote Assistance for use with SCCM
Sometimes clients can lose connectivity with the Lotus Notes Domino Server, but there's not always an obvious answer as to why it happens.   Read this article to follow one of the first experiences I had with Lotus Notes on a client's machine, my…
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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