Monday, 9 March 2015

Synchrolock FileLock

This creates a file placement to prevent a new file being created with the same name. 


#include <windows.h> #include <stdio.h> /* All Variables are here! */ FILE *readFile; // Read from file FILE *logFile; // Log File SynchroLock.log FILE *heartbeat; // File to output process heartbeat CHAR lockName[512]; // !Prone to Overflow! INT countFile; // Number of Files CHAR *removeNewLine; // Removes NewLine Char at end of gets() INT createPass; SYSTEMTIME HeartBeatTime; // For LocalTime information for logging heartbeat. BOOL LogExists=FALSE; //SECURITY_ATTRIBUTES MutexAttributes; // Global var used by RegisterMyMutex functn // Function LockMyFile int LockMyFile(CHAR *currentFile[]) { HANDLE *fileHandle; fileHandle = CreateFile(currentFile, // open MYFILE.TXT SYNCHRONIZE, // open for reading 0, // no share until closed NULL, // no security CREATE_NEW, // Create New File FILE_FLAG_DELETE_ON_CLOSE, // Delete on Close NULL); // no attr. template if (GetLastError() == NULL) return 1; else if (GetLastError() == 183) return -1; else if (GetLastError() == 80) return -1; else if (GetLastError() == 3) return -2; return 0; } // Entry Point void main() { countFile = 0; // Initialise counter mutex loaded readFile = fopen("c:\\SynchroLock\\File.list","r"); logFile = fopen("c:\\SynchroLock\\SynchroLock.log","a+"); if (logFile==NULL){ MessageBox(0,"No results will be written.","Error: Cannot open LogFile", MB_OK); // TO DO - FILE already created and running, QUIT! fclose(readFile); ExitProcess(0); } else LogExists=TRUE; if (readFile==NULL) { fprintf(logFile, "ERROR: No INPUT file. QUITTING\n", lockName); ExitProcess(0); } GetLocalTime(&HeartBeatTime); fprintf(logFile, "\n+Log file appended on %02d/%02d/%d %02d:%02d:%02d by FileLock \n", HeartBeatTime.wDay, HeartBeatTime.wMonth, HeartBeatTime.wYear, HeartBeatTime.wHour, HeartBeatTime.wMinute, HeartBeatTime.wSecond); while(fgets(lockName, 512, readFile)!=NULL) { if ((removeNewLine=strchr(lockName,'\n'))!=NULL) *removeNewLine='\0'; // Remove NewLine char from eos, replace with NULL countFile++; createPass = LockMyFile(lockName); if(createPass==1) fprintf(logFile, "%s has been synchronized succesfully.\n", lockName); else if (createPass==-1) fprintf(logFile, "ATTENTION - %s already exists. %d\n", lockName, GetLastError()); else if (createPass==-2) fprintf(logFile, "ATTENTION - %s was Denied Access. %d\n", lockName, GetLastError()); else if (createPass==0) fprintf(logFile, "WARNING - unknown error when creating %s - %d.\n", lockName, GetLastError()); } // End While Loop fclose(readFile); fclose(logFile); // Create a hearbeat and circulate while (LogExists){ heartbeat = fopen("\\SynchroLock\\Heartbeat.log","a+"); GetLocalTime(&HeartBeatTime); fprintf(heartbeat, "FileLock %02d/%02d/%d %02d:%02d:%02d \n", HeartBeatTime.wDay, HeartBeatTime.wMonth, HeartBeatTime.wYear, HeartBeatTime.wHour, HeartBeatTime.wMinute, HeartBeatTime.wSecond); fclose(heartbeat); Sleep(360000); // sleep 60 minutes } }

No comments:

Post a Comment