blocxx

PosixFile.cpp

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