OW_PosixFile.cpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2001-2004 Vintela, Inc. All rights reserved.
00003 *
00004 * Redistribution and use in source and binary forms, with or without
00005 * modification, are permitted provided that the following conditions are met:
00006 *
00007 *  - Redistributions of source code must retain the above copyright notice,
00008 *    this list of conditions and the following disclaimer.
00009 *
00010 *  - Redistributions in binary form must reproduce the above copyright notice,
00011 *    this list of conditions and the following disclaimer in the documentation
00012 *    and/or other materials provided with the distribution.
00013 *
00014 *  - Neither the name of Vintela, Inc. nor the names of its
00015 *    contributors may be used to endorse or promote products derived from this
00016 *    software without specific prior written permission.
00017 *
00018 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00019 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00020 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00021 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc. OR THE CONTRIBUTORS
00022 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00023 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00024 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00025 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00026 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00027 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00028 * POSSIBILITY OF SUCH DAMAGE.
00029 *******************************************************************************/
00030 
00036 #include "OW_config.h"
00037 #include "OW_File.hpp"
00038 
00039 #ifdef OW_WIN32
00040    #include <io.h>
00041    #include <stdlib.h>
00042    #include <stdio.h>
00043    #include <memory.h>
00044 #else
00045    #include <fcntl.h>
00046    #ifdef OW_HAVE_UNISTD_H
00047       #include <unistd.h>
00048    #endif
00049 #endif
00050 
00051 
00052 namespace OW_NAMESPACE
00053 {
00054 #ifdef OW_WIN32
00055 namespace
00056 {
00058 // implementation of lock functions
00059 int
00060 doLock(HANDLE hFile, bool doWait)
00061 {
00062    if (hFile == INVALID_HANDLE_VALUE)
00063    {
00064       return -1;
00065    }
00066 
00067    DWORD flags = LOCKFILE_EXCLUSIVE_LOCK;
00068    if (!doWait)
00069    {
00070       flags |= LOCKFILE_FAIL_IMMEDIATELY;
00071    }
00072 
00073    OVERLAPPED ov;
00074    memset(&ov, 0, sizeof(ov));
00075    if (!LockFileEx(hFile, flags, 0, 0xffffffff,
00076       0xffffffff, &ov))
00077    {
00078       return -1;
00079    }
00080 
00081    return 0;
00082 }
00083 
00084 } // end unnamed namespace
00085 
00087 File::File(const File& x) : m_hdl(OW_INVALID_FILEHANDLE)
00088 {
00089    if( x.m_hdl != OW_INVALID_FILEHANDLE )
00090    {
00091       DuplicateHandle(GetCurrentProcess(), x.m_hdl, GetCurrentProcess(),
00092          &m_hdl , 0, FALSE, DUPLICATE_SAME_ACCESS);
00093    }
00094 }
00096 int 
00097 File::getLock()
00098 {
00099    return doLock(m_hdl, true);
00100 }
00102 int
00103 File::tryLock()
00104 {
00105    return doLock(m_hdl, false);
00106 }
00108 int
00109 File::unlock()
00110 {
00111    if (m_hdl == INVALID_HANDLE_VALUE)
00112    {
00113       return -1;
00114    }
00115 
00116    OVERLAPPED ov;
00117    memset(&ov, 0, sizeof(ov));
00118    if (!UnlockFileEx(m_hdl, 0, 0xffffffff, 0xffffffff, &ov))
00119    {
00120       return -1;
00121    }
00122 
00123    return 0;
00124 }
00125 
00126 #else // NOT WIN32
00127 
00129 File::File(const File& x) : m_hdl(dup(x.m_hdl))
00130 {
00131 }
00132 
00133 namespace {
00135 // implementation of lock functions
00136 int
00137 doLock(int hdl, int cmd, short int type)
00138 {
00139    struct flock lck;
00140    ::memset (&lck, '\0', sizeof (lck));
00141    lck.l_type = type;          // type of lock
00142    lck.l_whence = 0;           // 0 offset for l_start
00143    lck.l_start = 0L;           // lock starts at BOF
00144    lck.l_len = 0L;             // extent is entire file
00145    return ::fcntl(hdl, cmd, &lck);
00146 }
00147 } // end unnamed namespace
00149 int 
00150 File::getLock()
00151 {
00152    return doLock(m_hdl, F_SETLKW, F_WRLCK);
00153 }
00155 int
00156 File::tryLock()
00157 {
00158    return doLock(m_hdl, F_SETLK, F_WRLCK);
00159 }
00161 int
00162 File::unlock()
00163 {
00164    return doLock(m_hdl, F_SETLK, F_UNLCK);
00165 }
00166 #endif
00167 
00168 } // end namespace OW_NAMESPACE
00169 

Generated on Thu Feb 9 08:48:08 2006 for openwbem by  doxygen 1.4.6