FileIOClient.cc

Go to the documentation of this file.
00001 /*
00002  * IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING. By
00003  * downloading, copying, installing or using the software you agree to
00004  * this license. If you do not agree to this license, do not download,
00005  * install, copy or use the software.
00006  * 
00007  * Intel Open Source License 
00008  * 
00009  * Copyright (c) 2004 Intel Corporation. All rights reserved. 
00010  * 
00011  * Redistribution and use in source and binary forms, with or without
00012  * modification, are permitted provided that the following conditions are
00013  * met:
00014  * 
00015  *   Redistributions of source code must retain the above copyright
00016  *   notice, this list of conditions and the following disclaimer.
00017  * 
00018  *   Redistributions in binary form must reproduce the above copyright
00019  *   notice, this list of conditions and the following disclaimer in the
00020  *   documentation and/or other materials provided with the distribution.
00021  * 
00022  *   Neither the name of the Intel Corporation nor the names of its
00023  *   contributors may be used to endorse or promote products derived from
00024  *   this software without specific prior written permission.
00025  *  
00026  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00027  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00028  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00029  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE INTEL OR
00030  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00031  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00032  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00033  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00034  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00035  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00036  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00037  */
00038 
00039 #include <errno.h>
00040 #include "FileIOClient.h"
00041 #include "IO.h"
00042 
00043 namespace oasys {
00044 
00045 //----------------------------------------------------------------------
00046 FileIOClient::FileIOClient()
00047     : FdIOClient(-1)
00048 {
00049 }
00050 
00051 //----------------------------------------------------------------------
00052 FileIOClient::~FileIOClient()
00053 {
00054     if (fd_ != -1)
00055         close();
00056 }
00057 
00058 //----------------------------------------------------------------------
00059 int
00060 FileIOClient::open(const char* path, int flags, int* errnop)
00061 {
00062     path_.assign(path);
00063     fd_ = IO::open(path, flags, errnop, logpath_);
00064     return fd_;
00065 }
00066 
00067 //----------------------------------------------------------------------
00068 int
00069 FileIOClient::open(const char* path, int flags, mode_t mode, int* errnop)
00070 {
00071     path_.assign(path);
00072     fd_ = IO::open(path, flags, mode, errnop, logpath_);
00073     return fd_;
00074 }
00075 
00076 //----------------------------------------------------------------------
00077 int
00078 FileIOClient::close()
00079 {
00080     int ret = IO::close(fd_, logpath_, path_.c_str());
00081     fd_ = -1;
00082     return ret;
00083 }
00084 
00085 //----------------------------------------------------------------------
00086 int
00087 FileIOClient::reopen(int flags)
00088 {
00089     ASSERT(path_.length() != 0);
00090     fd_ = IO::open(path_.c_str(), flags, NULL, logpath_);
00091     return fd_;
00092 }
00093 
00094 //----------------------------------------------------------------------
00095 int
00096 FileIOClient::unlink()
00097 {
00098     if (path_.length() == 0)
00099         return 0;
00100     
00101     int ret = 0;
00102     ret = IO::unlink(path_.c_str(), logpath_);
00103     path_.assign("");
00104     return ret;
00105 }
00106 
00107 //----------------------------------------------------------------------
00108 int
00109 FileIOClient::lseek(off_t offset, int whence)
00110 {
00111     return IO::lseek(fd_, offset, whence, logpath_);
00112 }
00113 
00114 //----------------------------------------------------------------------
00115 int
00116 FileIOClient::truncate(off_t length)
00117 {
00118     return IO::truncate(fd_, length, logpath_);
00119 }
00120 
00121 //----------------------------------------------------------------------
00122 int
00123 FileIOClient::mkstemp(char* temp)
00124 {
00125     if (fd_ != -1) {
00126         log_err("can't call mkstemp on open file");
00127         return -1;
00128     }
00129 
00130     fd_ = IO::mkstemp(temp, logpath_);
00131 
00132     path_.assign(temp);
00133     return fd_;
00134 }
00135 
00136 //----------------------------------------------------------------------
00137 int
00138 FileIOClient::stat(struct stat* buf)
00139 {
00140     return IO::stat(path_.c_str(), buf, logpath_);
00141 }
00142 
00143 //----------------------------------------------------------------------
00144 int
00145 FileIOClient::lstat(struct stat* buf)
00146 {
00147     return IO::lstat(path_.c_str(), buf, logpath_);
00148 }
00149 
00150 //----------------------------------------------------------------------
00151 int
00152 FileIOClient::copy_contents(size_t len, FileIOClient* dest)
00153 {
00154     char buf[1024];
00155     int cnt;
00156     int origlen = len;
00157 
00158     while (len > 0) {
00159         cnt = (len < sizeof(buf)) ? len : sizeof(buf);
00160 
00161         if (readall(buf, cnt) != cnt) {
00162             log_err("copy_contents: error reading %d bytes: %s",
00163                     cnt, strerror(errno));
00164             return -1;
00165         }
00166 
00167         if (dest->writeall(buf, cnt) != cnt) {
00168             log_err("copy_contents: error writing %d bytes: %s",
00169                     cnt, strerror(errno));
00170             return -1;
00171         }
00172 
00173         len -= cnt;
00174     }
00175     
00176     return origlen;
00177 }
00178 
00179 } // namespace oasys

Generated on Fri Dec 22 14:47:59 2006 for DTN Reference Implementation by  doxygen 1.5.1