Solved

Corrupted file at system level (cross-linked)

Posted on 2008-10-23
8
545 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Several part series to implement Internet Explorer 11 Enterprise Mode
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …

708 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

16 Experts available now in Live!

Get 1:1 Help Now