A killer app!
This is a story about an app I wrote a couple of months ago. A killer app! Yes, sir. Read on:
At some moment, the app needs to create a bunch of related temporary files. They are created in the TEMP directory using a GUID as the files base name. Each file uses the same GUID and has its own extension. When the operation is over, the app deletes the files (
The GUID trick is a nice one to generate a unique file name but it's not debugger-friendly: Good luck to find a file named after a random GUID in order to check its contents! So I added a secret registry value that allows to override the file name with a given (constnat) name instead of a random GUID. Cool!
Er... No, not cool!
Earlier this morning, I was modifiying the app. The modification involved the contents of the temp files. In order to check the result, I used this hidden registry feature to give the files a name in the source directory.
Pretty soon after the first test, I noticed that some source files had disappeared! WTF!? Oh no... I gave the temp file the same name as some 6 source files which I had just spent one hour modifying.
Result of the test: The del
Boy! I invented a program that deletes its own source code...
Yes, I have back-ups and yes, I have Source Control. So the disaster was pretty limited, thank you. But somehow, I believe I should fix this design or I'll be bitten again some time...


4 Comments:
Life is a b... sometimes.
BTW I am currently coding something in php calling a function called:
function RecursiveRemoveDirectory($directory, $empty=FALSE)
The function works so well that I am sometimes affraid of what could happen if made a mistaken in the calling argument!
Salut Serge,
Je suppose que tu sais que l'API Win32 contient différentes fonctions qui vont bien pour gérer et créer des noms de fichiers temporaires uniques ?
Un bout de code copié/collé d'un source chez moi:
// The full path returned is guaranteed to correspond to
// a writable place. The file name is guaranteed to be unique
// if unique = true.
__declspec(dllexport) CString GetDocuLiveTempFileName(CString extension, CString prefix, bool unique)
{
char path[_MAX_PATH], fileName[_MAX_FNAME];
DWORD pid = unique ? 0 : GetCurrentProcessId();
::GetTempPath(_MAX_PATH, path);
::GetTempFileName(path, LPCTSTR(prefix), (UINT) pid, fileName);
CString strFileName = CString(fileName);
strFileName.MakeUpper();
strFileName.Replace(".TMP", "." + extension.MakeUpper());
return strFileName;
}
Salut David,
Oui, je connais cette fonction. Mais elle m'a toujours paru stupidement compliquée, surtout pour obtenir un nom garanti unique: Incrémenter un compteur en boucle et tester si le fichier existe !? Quelle horreur!
Et de toute façon, ça n'aurait pas changé grand-chose à mon problème.
Mais bon, j'admet que je cherche peut-être la petite bête.
Mr Crookies, comme le dit la sagesse populaire: Qui fait le malin tombe dans le ravin!
Post a Comment
<< Home