Solved

Corrupted file at system level (cross-linked)

Posted on 2008-10-23
8
552 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…

732 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