Grafx2 now disables safety backups (and warns the user on startup) if another instance is running or if its configuration directory is read-only. Implemented separately on Linux and Windows, tested only on Windows

git-svn-id: svn://pulkomandy.tk/GrafX2/trunk@1343 416bcca6-2ee7-4201-b75f-2eb2f807beb1
This commit is contained in:
Yves Rizoud
2010-02-14 00:28:42 +00:00
parent 7076e8e06d
commit 4c3a0b6f68
4 changed files with 165 additions and 54 deletions

69
io.c
View File

@@ -284,3 +284,72 @@ void Get_full_filename(char * output_name, char * file_name, char * directory_na
}
strcat(output_name,file_name);
}
/// Lock file used to prevent several instances of grafx2 from harming each others' backups
#ifdef __WIN32__
HANDLE Lock_file_handle = INVALID_HANDLE_VALUE;
#else
int Lock_file_handle = -1;
#endif
byte Create_lock_file(const char *file_directory)
{
char lock_filename[MAX_PATH_CHARACTERS];
strcpy(lock_filename,file_directory);
strcat(lock_filename,"gfx2.lck");
#ifdef __WIN32__
// Windowzy method for creating a lock file
Lock_file_handle = CreateFile(
lock_filename,
GENERIC_WRITE,
0, // No sharing
NULL,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
if (Lock_file_handle == INVALID_HANDLE_VALUE)
{
return -1;
}
#else
// Unixy method for lock file
Lock_file_handle = open(lock_filename,O_WRONLY|O_CREAT);
if (Lock_file_handle == -1)
{
// Usually write-protected media
return -1;
}
if (lockf(Lock_file_handle, F_TLOCK, 0)==-1)
{
close(Lock_file_handle);
// Usually write-protected media
return -1;
}
#endif
return 0;
}
void Release_lock_file(const char *file_directory)
{
char lock_filename[MAX_PATH_CHARACTERS];
#ifdef __WIN32__
if (Lock_file_handle != INVALID_HANDLE_VALUE)
{
CloseHandle(Lock_file_handle);
}
#else
if (Lock_file_handle != -1)
{
close(Lock_file_handle);
Lock_file_handle = -1;
}
#endif
// Actual deletion
strcpy(lock_filename,file_directory);
strcat(lock_filename,"gfx2.lck");
remove(lock_filename);
}