Пример #1
0
int flock (int fd,int op)
{
  HANDLE hdl = (HANDLE) _get_osfhandle (fd);
  DWORD flags = (op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0;
  OVERLAPPED offset = {NIL,NIL,0,0,NIL};
  int ret = -1;
  blocknotify_t bn = (blocknotify_t) 
    ((op & LOCK_NB) ? NIL : mail_parameters (NIL,GET_BLOCKNOTIFY,NIL));
  if (hdl < 0) errno = EBADF;	/* error in file descriptor */
  else switch (op & ~LOCK_NB) {	/* translate to LockFileEx() op */
  case LOCK_EX:			/* exclusive */
    flags |= LOCKFILE_EXCLUSIVE_LOCK;
  case LOCK_SH:			/* shared */
    if (!check_nt ()) return 0;	/* always succeeds if not NT */
    if (bn) (*bn) (BLOCK_FILELOCK,NIL);
				/* bug for bug compatible with Unix */
    UnlockFileEx (hdl,NIL,1,0,&offset);
				/* lock the file as requested */
    if (LockFileEx (hdl,flags,NIL,1,0,&offset)) ret = 0;
    if (bn) (*bn) (BLOCK_NONE,NIL);
				/* if failed */
    if (ret) errno = (op & LOCK_NB) ? EAGAIN : EBADF;
    break;
  case LOCK_UN:			/* unlock */
    if (check_nt ()) UnlockFileEx (hdl,NIL,1,0,&offset);
    ret = 0;			/* always succeeds */
  default:			/* default */
    errno = EINVAL;		/* bad call */
    break;
  }
  return ret;
}
Пример #2
0
int flock(int fd, int op)
{
	HANDLE hdl = (HANDLE) _get_osfhandle(fd);
	DWORD low = 1, high = 0;
	OVERLAPPED offset =
	{0, 0, 0, 0, NULL};
	if (hdl < 0)
		return -1;				/* error in file descriptor */
	/* bug for bug compatible with Unix */
	UnlockFileEx(hdl, 0, low, high, &offset);
	switch (op & ~LOCK_NB) {	/* translate to LockFileEx() op */
		case LOCK_EX:			/* exclusive */
			if (LockFileEx(hdl, LOCKFILE_EXCLUSIVE_LOCK +
						((op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
						   0, low, high, &offset))
				return 0;
			break;
		case LOCK_SH:			/* shared */
			if (LockFileEx(hdl, ((op & LOCK_NB) ? LOCKFILE_FAIL_IMMEDIATELY : 0),
						   0, low, high, &offset))
				return 0;
			break;
		case LOCK_UN:			/* unlock */
			return 0;			/* always succeeds */
		default:				/* default */
			break;
	}
	errno = EINVAL;				/* bad call */
	return -1;
}
Пример #3
0
static int
lock_out(HANDLE hFile , int op)
{
	DWORD dwNumBytesWritten;
	BOOL fSuccess;
	OVERLAPPED sOverlapped;
	char buf[PBS_MAXHOSTNAME + 10];

	if (op == F_WRLCK) {
		sOverlapped.Offset = 0;
		sOverlapped.OffsetHigh = 100;
		sOverlapped.hEvent = 0;

		fSuccess = LockFileEx(hFile,
			LOCKFILE_EXCLUSIVE_LOCK |
			LOCKFILE_FAIL_IMMEDIATELY,
			0, 0, 1000, &sOverlapped);
		if (fSuccess) {
			/* if write-lock, record hostname and pid in file */
			(void) sprintf(buf, "%s:%d\n", thishost, getpid());
			fSuccess = WriteFile(hFile,
				buf,
				strlen(buf),
				&dwNumBytesWritten,
				NULL);
			return 0;
		}
	} else {
		/* unlock and return */
		fSuccess = UnlockFileEx(hFile, 0, 0, 1000, &sOverlapped);
		if (fSuccess)
			return 0;
	}
	return 1;
}
Пример #4
0
int flock(int fd, int oper) {
    OVERLAPPED o;
    int i = -1;
    HANDLE fh;

    fh = (HANDLE)_get_osfhandle(fd);
    memset(&o, 0, sizeof(o));

    switch(oper) {
		case LOCK_SH:		/* shared lock */
			LK_ERR(LockFileEx(fh, 0, 0, LK_LEN, 0, &o),i);
		break;
		case LOCK_EX:		/* exclusive lock */
			LK_ERR(LockFileEx(fh, LOCKFILE_EXCLUSIVE_LOCK, 0, LK_LEN, 0, &o),i);
		break;
		case LOCK_SH|LOCK_NB:	/* non-blocking shared lock */
			LK_ERR(LockFileEx(fh, LOCKFILE_FAIL_IMMEDIATELY, 0, LK_LEN, 0, &o),i);
		break;
		case LOCK_EX|LOCK_NB:	/* non-blocking exclusive lock */
			LK_ERR(LockFileEx(fh,LOCKFILE_EXCLUSIVE_LOCK|LOCKFILE_FAIL_IMMEDIATELY,0, LK_LEN, 0, &o),i);
		break;
		case LOCK_UN:		/* unlock lock */
			LK_ERR(UnlockFileEx(fh, 0, LK_LEN, 0, &o),i);
		break;
		default:			/* unknown */
			//errno = EINVAL; // i heard that on some versions errno is a function (win32 MT lib?)
		break;
    }
    return i;
}
Пример #5
0
void CJaxerLog::LogStr(eLogLevel eLevel, const char* pszStr, int len)
{
    if (eLevel < m_eDefault)
        return;

    if (!m_fp)
        return;

    time_t rawtime;
	struct tm * timeinfo;

    char szTimeBuf[0x64];

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );

    sprintf(szTimeBuf,
        "%02d:%02d:%02d %02d/%02d/%04d [%s] ",
        timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
        timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_year+1900,
        sLogLevel[eLevel]);

    
    OVERLAPPED ovlp;
    memset(&ovlp, 0, sizeof(ovlp));
    LockFileEx(m_fp, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &ovlp);
    DWORD nBytes;
    SetFilePointer(m_fp, 0, 0, FILE_END);
    WriteFile(m_fp, szTimeBuf, (DWORD)strlen(szTimeBuf), &nBytes, NULL);
    WriteFile(m_fp, pszStr, (len==-1)? (DWORD)strlen(pszStr) : len, &nBytes, NULL);
    WriteFile(m_fp, "\r\n", 2, &nBytes, NULL);
    UnlockFileEx(m_fp, 0, 1, 0, &ovlp);
}
Пример #6
0
int
pgut_flock(int fd, int operation)
{
	BOOL	ret;
	HANDLE	handle = (HANDLE) _get_osfhandle(fd);
	DWORD	lo = 0;
	DWORD	hi = 0;

	if (operation & LOCK_UN)
	{
		ret = UnlockFileEx(handle, 0, lo, hi, NULL);
	}
	else
	{
		DWORD	flags = 0;
		if (operation & LOCK_EX)
			flags |= LOCKFILE_EXCLUSIVE_LOCK;
		if (operation & LOCK_NB)
			flags |= LOCKFILE_FAIL_IMMEDIATELY;
		ret = LockFileEx(handle, flags, 0, lo, hi, NULL);
	}

	if (!ret)
	{
		_dosmaperr(GetLastError());
		return -1;
	}

	return 0;
}
Пример #7
0
static int file_lock(lua_State *L,
                     FILE *f, const char *mode, long offset, long length)
{
  static const ULARGE_INTEGER zero_len;
  static const OVERLAPPED zero_ov;
  HANDLE h = file_handle(f);
  ULARGE_INTEGER len = zero_len;
  OVERLAPPED ov = zero_ov;
  DWORD flags = 0;
  BOOL ret;
  if (length) len.LowPart = length;
  else len.LowPart = len.HighPart = -1;
  ov.Offset = offset;
  switch (*mode) {
    case 'w':
      flags = LOCKFILE_EXCLUSIVE_LOCK;
      /*FALLTHRU*/
    case 'r':
      flags |= LOCKFILE_FAIL_IMMEDIATELY;
      ret = LockFileEx(h, flags, 0, len.LowPart, len.HighPart, &ov);
      break;
    case 'u':
      ret = UnlockFileEx(h, 0, len.LowPart, len.HighPart, &ov);
      break;
    default:
      return luaL_error(L, "invalid mode");
  }
  if (!ret)
    return push_error(L);
  /* return the file */
  lua_settop(L, 1);
  return 1;
}
Пример #8
0
xi_file_re xi_file_unlock(xint32 fd, xint64 spos, xint64 len) {
	OVERLAPPED overlapped;
	const DWORD offsetLow = (DWORD)(spos & 0xFFFFFFFF);
	const DWORD offsetHigh = (DWORD)((spos >> 0x20) & 0x7FFFFFFF);
	const DWORD nNumberOfBytesToUnlockLow = (DWORD)(len & 0xFFFFFFFF);
	const DWORD nNumberOfBytesToUnlockHigh =
			(DWORD)((len >> 0x20) & 0x7FFFFFFF);
	xg_fd_t *fdesc;

	if (fd < 0) {
		return XI_FILE_RV_ERR_ARGS;
	}

	fdesc = xg_fd_get(fd);

	xi_mem_set(&overlapped, 0, sizeof(overlapped));
	overlapped.Offset = offsetLow;
	overlapped.OffsetHigh = offsetHigh;

	if (!UnlockFileEx(fdesc->desc.f.fd, /* [in] file handle to lock */
	(DWORD) 0, /* [in] reserved */
	nNumberOfBytesToUnlockLow, /* [in] number of bytes to lock (low) */
	nNumberOfBytesToUnlockHigh, /* [in] number of bytes to lock (high) */
	(LPOVERLAPPED) & overlapped)) { /* [in] contains file offset to lock start */
		return XI_FILE_RV_ERR_ARGS;
	}

	return XI_FILE_RV_OK;
}
Пример #9
0
bool Log::LogFile::unlock(void)
{
    OVERLAPPED ov;

    memset(&ov, 0, sizeof (ov));
    return UnlockFileEx(fd, 0, 1, 0, &ov) == TRUE;
}
Пример #10
0
/* use LockFileEx/UnlockFileEx */
int acl_myflock(ACL_FILE_HANDLE fd, int lock_style acl_unused, int operation)
{
	const char *myname = "acl_myflock";
	DWORD size = 0xFFFFFFFF;
	char  ebuf[256];
	unsigned char lock_op;
	OVERLAPPED ovlp;
	DWORD flags;

	if ((operation & (ACL_FLOCK_OP_BITS)) != operation)
		acl_msg_panic("myflock: improper operation type: 0x%x", operation);

	memset(&ovlp, 0, sizeof(ovlp));

	ovlp.Offset = 0;
	lock_op = operation & ~ACL_FLOCK_OP_NOWAIT;
	if (lock_op == ACL_FLOCK_OP_NONE) {
		if(UnlockFileEx(fd, 0, 1, 0, &ovlp))
			return (0);
		acl_msg_error("%s(%d): unlock error(%s)",
			myname, __LINE__, acl_last_strerror(ebuf,sizeof(ebuf)));
		return (-1);
	} else if (lock_op == ACL_FLOCK_OP_SHARED) {
		while (1) {
			flags = 0;
			if ((operation & ACL_FLOCK_OP_NOWAIT))
				flags |= LOCKFILE_FAIL_IMMEDIATELY;

			if(LockFileEx(fd, flags, 0, 1, 0, &ovlp))
				return (0);
			if ((operation & ACL_FLOCK_OP_NOWAIT)) {
				acl_msg_error("%s(%d): lock error(%s)", myname, __LINE__,
					acl_last_strerror(ebuf,sizeof(ebuf)));
				return (-1);
			}
			sleep(1);
		}
	} else if (lock_op == ACL_FLOCK_OP_EXCLUSIVE) {
		while (1) {
			flags = LOCKFILE_EXCLUSIVE_LOCK;
			if ((operation & ACL_FLOCK_OP_NOWAIT))
				flags |= LOCKFILE_FAIL_IMMEDIATELY;

			if(LockFileEx(fd, flags, 0, 1, 0, &ovlp))
				return (0);

			if ((operation & ACL_FLOCK_OP_NOWAIT)) {
				acl_msg_error("%s(%d): lock error(%s)", myname, __LINE__,
					acl_last_strerror(ebuf,sizeof(ebuf)));
				return (-1);
			}
			sleep(1);
		}
	}

	acl_msg_error("%s(%d): invalid lock_op(%d)", myname, __LINE__, lock_op);
	return (-1);
}
Пример #11
0
DWORD SpecialCases::Sp_UnlockFileEx()
	{
	 BOOL returnVal_Real = 0;
    BOOL returnVal_Intercepted = 0;
	OVERLAPPED ovValue;
	ZeroMemory( &ovValue, sizeof( OVERLAPPED ) );
    DWORD error = 0;
	return (UnlockFileEx( NULL, NULL, NULL, NULL, &ovValue ));
	}
Пример #12
0
bool tr_sys_file_lock(tr_sys_file_t handle, int operation, tr_error** error)
{
    TR_ASSERT(handle != TR_BAD_SYS_FILE);
    TR_ASSERT((operation & ~(TR_SYS_FILE_LOCK_SH | TR_SYS_FILE_LOCK_EX | TR_SYS_FILE_LOCK_NB | TR_SYS_FILE_LOCK_UN)) == 0);
    TR_ASSERT(!!(operation & TR_SYS_FILE_LOCK_SH) + !!(operation & TR_SYS_FILE_LOCK_EX) +
        !!(operation & TR_SYS_FILE_LOCK_UN) == 1);

    bool ret;
    OVERLAPPED overlapped = { .Pointer = 0, .hEvent = NULL };

    if ((operation & TR_SYS_FILE_LOCK_UN) == 0)
    {
        DWORD native_flags = 0;

        if ((operation & TR_SYS_FILE_LOCK_EX) != 0)
        {
            native_flags |= LOCKFILE_EXCLUSIVE_LOCK;
        }

        if ((operation & TR_SYS_FILE_LOCK_NB) != 0)
        {
            native_flags |= LOCKFILE_FAIL_IMMEDIATELY;
        }

        ret = LockFileEx(handle, native_flags, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE;
    }
    else
    {
        ret = UnlockFileEx(handle, 0, MAXDWORD, MAXDWORD, &overlapped) != FALSE;
    }

    if (!ret)
    {
        set_system_error(error, GetLastError());
    }

    return ret;
}

char* tr_sys_dir_get_current(tr_error** error)
{
    char* ret = NULL;
    wchar_t* wide_ret = NULL;
    DWORD size;

    size = GetCurrentDirectoryW(0, NULL);

    if (size != 0)
    {
        wide_ret = tr_new(wchar_t, size);

        if (GetCurrentDirectoryW(size, wide_ret) != 0)
        {
            ret = tr_win32_native_to_utf8(wide_ret, size);
        }
    }
Пример #13
0
static_inline void
_VUnlockFd(FD_t handle, afs_uint32 offset)
{
    OVERLAPPED lap;

    memset(&lap, 0, sizeof(lap));
    lap.Offset = offset;

    UnlockFileEx(handle, 0, 1, 0, &lap);
}
Пример #14
0
static_inline void
_VUnlockFd(struct VLockFile *lf, afs_uint32 offset)
{
    OVERLAPPED lap;

    memset(&lap, 0, sizeof(lap));
    lap.Offset = offset;

    UnlockFileEx(lf->fd, 0, 1, 0, &lap);
}
Пример #15
0
static inline int dolock(xc_fcntl_mutex_t *fcntl_mutex, int type)
{
	static OVERLAPPED offset = {0, 0, 0, 0, NULL};

	if (type == LCK_UN) {
		return UnlockFileEx(fcntl_mutex->fd, 0, 1, 0, &offset);
	}
	else {
		return LockFileEx(fcntl_mutex->fd, type, 0, 1, 0, &offset);
	}
}
Пример #16
0
int mmc_unlock_page(mmap_cache* cache) {
    OVERLAPPED lock;
    memset(&lock, 0, sizeof(lock));
    lock.Offset = cache->p_offset;
    lock.hEvent = 0;
  
    UnlockFileEx(cache->fh, 0, cache->c_page_size, 0, &lock);
    
    /* Set to bad value while page not locked */
    cache->p_cur = ~0; /* ~0 = -1, but unsigned */    
}
Пример #17
0
/* Windows specific code of destroy_dotlock.  */
static void
dotlock_destroy_w32 (dotlock_t h)
{
  if (h->locked)
    {
      OVERLAPPED ovl;

      memset (&ovl, 0, sizeof ovl);
      UnlockFileEx (h->lockhd, 0, 1, 0, &ovl);
    }
  CloseHandle (h->lockhd);
}
Пример #18
0
axStatus axFile::unlock() {
	if( ! isValid() ) { assert(false);	return axStatus_Std::not_initialized; }

    DWORD len = 0xffffffff;
	OVERLAPPED offset;
	ax_memset( offset, 0 );

	if (!UnlockFileEx( h_, 0, len, len, &offset)) {
		DEBUG_ax_log_win32_error("File::unlock");
		assert(false);
		return axStatus_Std::File_lock_error;
	}
	return 0;
}
Пример #19
0
void
VUnlockPartition_r(char *name)
{
    register struct DiskPartition64 *dp = VGetPartition_r(name, 0);
    OVERLAPPED lap;

    if (!dp)
	return;			/* no partition, will fail later */
    memset(&lap, 0, sizeof(lap));

    UnlockFileEx((HANDLE) dp->lock_fd, 0, 1, 0, &lap);
    CloseHandle((HANDLE) dp->lock_fd);
    dp->lock_fd = INVALID_FD;
}
Пример #20
0
AutoFileLock::~AutoFileLock()
{
    if(_hFile != INVALID_HANDLE_VALUE)
    {
        OVERLAPPED l={0,0,0,0,0};
        if(UnlockFileEx (_hFile, 0, 0, 0, &l) == 0)
        {
           PEG_TRACE((TRC_DISCARDED_DATA, Tracer::LEVEL2,
               "AutoFileLock: Failed to unlock file, error code %d.",
                GetLastError()));
        }
        CloseHandle(_hFile);
    }
}
Пример #21
0
/* Windows specific code of release_dotlock.  */
static int
dotlock_release_w32 (dotlock_t h)
{
  OVERLAPPED ovl;

  memset (&ovl, 0, sizeof ovl);
  if (!UnlockFileEx (h->lockhd, 0, 1, 0, &ovl))
    {
      my_error_2 ("release_dotlock: error removing lockfile `%s': %s\n",
                  h->lockname, w32_strerror (-1));
      return -1;
    }

  return 0;
}
Пример #22
0
static int flock(int fd, int op)
{
    HANDLE h = (HANDLE) _get_osfhandle(fd);
    OVERLAPPED offset;
    if (h < 0)
	return -1;
    memset(&offset, 0, sizeof(offset));
    switch (op) {
    case LOCK_EX:
	return (LockFileEx(h, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &offset)) ? 0 : -1;
    case LOCK_UN:
	UnlockFileEx(h, 0, 1, 0, &offset);
	return 0;
    }
    return -1;
}
Пример #23
0
int
File::unlock()
{
	if (m_hdl == INVALID_HANDLE_VALUE)
	{
		return -1;
	}

	OVERLAPPED ov;
	memset(&ov, 0, sizeof(ov));
	if (!UnlockFileEx(m_hdl, 0, 0xffffffff, 0xffffffff, &ov))
	{
		return -1;
	}

	return 0;
}
Пример #24
0
int main(int argc, char *argv[]){
    char buff[255] = "";
    HANDLE file_handle;
    DWORD err_code;
    OVERLAPPED overlapped;
    HANDLE output_handle = get_output_handle();

    memset(&overlapped, 0, sizeof(overlapped));

    file_handle = CreateFile(argv[1],
                       GENERIC_READ,
                       FILE_SHARE_READ,
                       NULL,
                       OPEN_EXISTING,
                       FILE_ATTRIBUTE_NORMAL,
                       NULL);

    if(file_handle == INVALID_HANDLE_VALUE)
    {
        print_error_alert(output_handle, "Error on opening");
        exit(1);
    }

    if(!LockFile(file_handle, 0, 0, 10, 10))
    {
      print_warning_alert(output_handle, "File is locked! Waiting...");
      if (!LockFileEx(file_handle, LOCKFILE_EXCLUSIVE_LOCK, 0, 10000, 0, &overlapped))
      {
          err_code = GetLastError();
          print_error_alert(output_handle, "Error on locking");
          exit(1);
      }
    }
    print_success_alert(output_handle, "File successfuly locked");
    Sleep(500);
    printf("\nFile contains:\n");
    read_write(file_handle, output_handle, buff, sizeof(buff));

    Sleep(7000);

    UnlockFileEx(file_handle, 0, 10000, 0, &overlapped);
    print_success_alert(output_handle, "File successfuly unlocked");

    return 0;
}
Пример #25
0
/* Unlocks a file. */
static void unlock(MVMThreadContext *tc, MVMOSHandle *h) {
    MVMIOFileData *data = (MVMIOFileData *)h->body.data;

#ifdef _WIN32

    const DWORD len = 0xffffffff;
    const HANDLE hf = (HANDLE)_get_osfhandle(data->fd);
    OVERLAPPED offset;

    if (hf == INVALID_HANDLE_VALUE) {
        MVM_exception_throw_adhoc(tc, "Failed to seek in filehandle: bad file descriptor");
    }

    memset (&offset, 0, sizeof(offset));
    MVM_gc_mark_thread_blocked(tc);
    if (UnlockFileEx(hf, 0, len, len, &offset)) {
        MVM_gc_mark_thread_unblocked(tc);
        return;
    }
    MVM_gc_mark_thread_unblocked(tc);

    MVM_exception_throw_adhoc(tc, "Failed to unlock filehandle: %d", GetLastError());
#else

    struct flock l;
    ssize_t r;
    const int fd = data->fd;

    l.l_whence = SEEK_SET;
    l.l_start = 0;
    l.l_len = 0;
    l.l_type = F_UNLCK;

    do {
        MVM_gc_mark_thread_blocked(tc);
        r = fcntl(fd, F_SETLKW, &l);
        MVM_gc_mark_thread_unblocked(tc);
    } while (r == -1 && errno == EINTR);

    if (r == -1) {
        MVM_exception_throw_adhoc(tc, "Failed to unlock filehandle: %d", errno);
    }
#endif
}
Пример #26
0
static HRESULT WINAPI FileLockBytesImpl_UnlockRegion(ILockBytes* iface,
    ULARGE_INTEGER libOffset, ULARGE_INTEGER cb, DWORD dwLockType)
{
    FileLockBytesImpl* This = impl_from_ILockBytes(iface);
    OVERLAPPED ol;

    TRACE("ofs %u count %u flags %x\n", libOffset.u.LowPart, cb.u.LowPart, dwLockType);

    if (dwLockType & LOCK_WRITE)
        return STG_E_INVALIDFUNCTION;

    ol.hEvent = 0;
    ol.u.s.Offset = libOffset.u.LowPart;
    ol.u.s.OffsetHigh = libOffset.u.HighPart;

    if (UnlockFileEx(This->hfile, 0, cb.u.LowPart, cb.u.HighPart, &ol))
        return S_OK;
    return get_lock_error();
}
Пример #27
0
static void
enchant_unlock_file (FILE * f)
{
#if defined(HAVE_FLOCK)
	flock (fileno (f), LOCK_UN);
#elif defined(HAVE_LOCKF)
	lockf (fileno (f), F_ULOCK, 0);
#elif defined(_WIN32)
	OVERLAPPED overlapped;

	overlapped.Offset = 0;
	overlapped.OffsetHigh = 0;
	overlapped.hEvent = NULL;
	if (!UnlockFileEx ((HANDLE) _get_osfhandle (fileno (f)), 0, 0, 0x80000000, &overlapped))
		g_warning ("Could not unlock file\n");
#else
	/* TODO: UNIX fcntl. This race condition probably isn't too bad. */
#endif /* HAVE_FLOCK */
}
Пример #28
0
DWORD FindProcessId (DWORD jobNumber)

/* Obtain the process ID of the specified job number. */
{
	HANDLE hJobData;
	JM_JOB jobRecord;
	DWORD nXfer, fileSizeLow;
	TCHAR jobMgtFileName[MAX_PATH+1];
	OVERLAPPED regionStart;
	LARGE_INTEGER fileSize;

	if ( !GetJobMgtFileName (jobMgtFileName) ) return 0;
	hJobData = CreateFile (jobMgtFileName, GENERIC_READ,
			FILE_SHARE_READ | FILE_SHARE_WRITE,
			NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
	if (hJobData == INVALID_HANDLE_VALUE) return 0;
	/* Position to the correct record, but not past the end of file */
	/* As a variation, use GetFileSize to demonstrate its operation. */
	if (!GetFileSizeEx (hJobData, &fileSize) ||
		(fileSize.HighPart != 0 || SJM_JOB * (jobNumber - 1) > fileSize.LowPart 
		 || fileSize.LowPart > SJM_JOB * MAX_JOBS_ALLOWED))
			return 0;
	fileSizeLow = fileSize.LowPart;
	/* SetFilePoiner is more convenient here than SetFilePointerEx since the the file is known to be "short" ( < 4 GB). */
	SetFilePointer (hJobData, SJM_JOB * (jobNumber - 1), NULL, FILE_BEGIN);
	
	/* Get a shared lock on the record. */
	
	regionStart.Offset = SJM_JOB * (jobNumber - 1);
	regionStart.OffsetHigh = 0; /* Assume a "short" file. */
	regionStart.hEvent = (HANDLE)0;
	LockFileEx (hJobData, 0, 0, SJM_JOB, 0, &regionStart);
	
	if (!ReadFile (hJobData, &jobRecord, SJM_JOB, &nXfer, NULL))
		ReportError (_T ("JobData file error"), 0, TRUE);

	UnlockFileEx (hJobData, 0, SJM_JOB, 0, &regionStart);
	CloseHandle (hJobData);

	return jobRecord.ProcessId;
}
Пример #29
0
static int zend_file_cache_flock(int fd, int op)
{
	OVERLAPPED offset = {0,0,0,0,NULL};
	if (op == LOCK_EX) {
		if (LockFileEx((HANDLE)_get_osfhandle(fd),
		               LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &offset) == TRUE) {
			return 0;
		}
	} else if (op == LOCK_SH) {
		if (LockFileEx((HANDLE)_get_osfhandle(fd),
		               0, 0, 1, 0, &offset) == TRUE) {
			return 0;
		}
	} else if (op == LOCK_UN) {
		if (UnlockFileEx((HANDLE)_get_osfhandle(fd),
		                 0, 1, 0, &offset) == TRUE) {
			return 0;
		}
	}
	return -1;
}
Пример #30
0
void CJaxerLog::Log(eLogLevel eLevel, char* pszFormat, ...)
{
    if (eLevel < m_eDefault)
        return;

    if (!m_fp)
        return;

    char szBuffer[0x4000];
    char szTimeBuf[0x64];

    va_list arg_ptr;
    va_start(arg_ptr, pszFormat);
    vsprintf(szBuffer, pszFormat, arg_ptr);
    va_end(arg_ptr);

    time_t rawtime;
	struct tm * timeinfo;

	time ( &rawtime );
	timeinfo = localtime ( &rawtime );

    sprintf(szTimeBuf,
        "%02d:%02d:%02d %02d/%02d/%04d [%s] ",
        timeinfo->tm_hour, timeinfo->tm_min, timeinfo->tm_sec,
        timeinfo->tm_mon+1, timeinfo->tm_mday, timeinfo->tm_year+1900,
        sLogLevel[eLevel]);

    
    OVERLAPPED ovlp;
    memset(&ovlp, 0, sizeof(ovlp));
    LockFileEx(m_fp, LOCKFILE_EXCLUSIVE_LOCK, 0, 1, 0, &ovlp);
    DWORD nBytes;
    SetFilePointer(m_fp, 0, 0, FILE_END);
    WriteFile(m_fp, szTimeBuf, (DWORD)strlen(szTimeBuf), &nBytes, NULL);
    WriteFile(m_fp, szBuffer, (DWORD)strlen(szBuffer), &nBytes, NULL);
    WriteFile(m_fp, "\r\n", 2, &nBytes, NULL);
    UnlockFileEx(m_fp, 0, 1, 0, &ovlp);
}