?
Solved

Corrupted file at system level (cross-linked)

Posted on 2008-10-23
8
Medium Priority
?
556 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
Turn your laptop into a mobile console!

The CV211 Laptop USB Console Adapter provides a direct Laptop-to-Computer connection for fast and easy remote desktop access with no software to install.

 
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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

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

This article is a collection of issues that people face from time to time and possible solutions to those issues. I hope you enjoy reading it.
A small collection of useful tips and tricks for Windows 10 users that I decided to write as a result of recent questions that were asked and answered at Experts Exchange. Two short video tutorials included. Enjoy..
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
Suggested Courses

801 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