13 #include <sys/locking.h>
14 #define access _access
16 #define locking _locking
23 #include <sys/procfs.h>
28 int CheckIfDir(
const char *name)
33 return ((stat(name, &s_buf) != -1) && ((s_buf.st_mode & S_IFMT) == S_IFDIR));
38 int CheckIfFile(
const char *name)
43 return ((stat(name, &s_buf) != -1) && ((s_buf.st_mode & S_IFMT) == S_IFREG));
48 int CheckForWrite(
const char *filepath)
53 return ((stat(filepath, &s_buf) != -1) && s_buf.st_mode && (access(filepath, W_OK) != -1));
58 int CheckForRead(
const char *filepath)
63 return ((stat(filepath, &s_buf) != -1) && s_buf.st_mode && (access(filepath, R_OK) != -1));
68 int CheckForExe(
const char *filepath)
73 return ((stat(filepath, &s_buf) != -1) && s_buf.st_mode && (access(filepath, X_OK) != -1));
78 int GetFileSize(
const char *filepath)
84 if ( stat( filepath, &s_buf) == -1) {
87 return (s_buf.st_size);
99 int IsFileSectionLocked(
const int iFileNo,
102 const int bExclusive)
107 lseek(iFileNo, nPosition, SEEK_SET);
109 if (locking(iFileNo, LK_NBLCK, nSize)!=0) {
112 locking(iFileNo, LK_UNLCK, nSize);
117 fl.l_start = nPosition;
125 fl.l_type = bExclusive ? F_WRLCK : F_RDLCK;
127 if (fcntl(fno, F_GETLK, &fl)!=0) {
128 printf(
"IsFileSectionLocked: fcntl(F_GETLK,...) failed "
129 "with error %d (%s)\n", errno, strerror(errno));
132 return (fl.l_type != F_UNLCK);
138 int LockFileSection(
const int iFileNo,
142 const int bExclusive,
143 const int bNonBlocking)
149 lseek(iFileNo, nPosition, SEEK_SET);
153 HANDLE hFile = (
void *) _get_osfhandle(iFileNo);
154 if (hFile == INVALID_HANDLE_VALUE) {
155 printf(
"LockFileSection: get_ofshandle failed for file %d\n", iFileNo);
160 struct _OVERLAPPED ol;
161 ol.Offset = nPosition;
167 int nMode = LOCKFILE_EXCLUSIVE_LOCK;
169 nMode |= LOCKFILE_FAIL_IMMEDIATELY;
174 int nRetries = 0, nMaxRetries = 0, iLastErr = 0;
176 nMaxRetries = 1000000000;
179 bOK = LockFileEx(hFile, nMode, 0, nSize, 0, &ol);
181 iLastErr = GetLastError();
183 if (iLastErr!=ERROR_LOCK_VIOLATION) {
187 }
while (!bOK && (nMaxRetries>nRetries++));
190 bOK = UnlockFileEx(hFile, 0, nSize, 0, &ol);
195 DWORD iLastErr = GetLastError();
197 FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
198 FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS,
200 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
201 (LPTSTR) &lpMsgBuf, 0, NULL);
202 printf(
"%s of file %d returned error %d (%s)\n",
203 (
const char *)(bLock ?
"Locking" :
"Unlocking"),
204 iFileNo, iLastErr, (
const char*)(LPCTSTR) lpMsgBuf);
212 fl.l_start = nPosition;
213 fl.l_len = nSize<0 ? 0 : nSize;
223 fl.l_type = bExclusive ? F_WRLCK : F_RDLCK;
227 retval = (fcntl(fno, F_SETLK, &fl)==0);
229 retval = (fcntl(fno, F_SETLKW, &fl)==0);
231 if (errno == EAGAIN) {
232 printf(
"LockFileSection: "
233 "Lock is being held by process %d\n", fl.l_pid);
235 printf(
"Lock of file %d (proc=%d) returned error %d (%s)\n",
236 fno, fl.l_pid, errno, strerror(errno));
242 retval = (fcntl(fno, F_SETLKW, &fl)==0);
244 printf(
"LockFileSection: Unlock failed with error %d (%s)\n", errno, strerror(errno));