abyss_file.c

Go to the documentation of this file.
00001 /******************************************************************************
00002 **
00003 ** file.c
00004 **
00005 ** This file is part of the ABYSS Web server project.
00006 **
00007 ** Copyright (C) 2000 by Moez Mahfoudh <mmoez@bigfoot.com>.
00008 ** All rights reserved.
00009 **
00010 ** Redistribution and use in source and binary forms, with or without
00011 ** modification, are permitted provided that the following conditions
00012 ** are met:
00013 ** 1. Redistributions of source code must retain the above copyright
00014 **    notice, this list of conditions and the following disclaimer.
00015 ** 2. Redistributions in binary form must reproduce the above copyright
00016 **    notice, this list of conditions and the following disclaimer in the
00017 **    documentation and/or other materials provided with the distribution.
00018 ** 3. The name of the author may not be used to endorse or promote products
00019 **    derived from this software without specific prior written permission.
00020 ** 
00021 ** THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
00022 ** ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00023 ** IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00024 ** ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
00025 ** FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00026 ** DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00027 ** OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00028 ** HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029 ** LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00030 ** OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00031 ** SUCH DAMAGE.
00032 **
00033 ******************************************************************************/
00034 
00035 #include <string.h>
00036 
00037 #ifdef WIN32
00038 #include <io.h>
00039 #endif
00040 
00041 #ifndef WIN32
00042 #include <dirent.h>
00043 #include <sys/stat.h>
00044 #endif
00045 
00046 #include <xmlrpc-c/abyss.h>
00047 #include "abyss_file.h"
00048 
00049 /*********************************************************************
00050 ** File
00051 *********************************************************************/
00052 
00053 abyss_bool FileOpen(TFile *f, const char *name,uint32_t attrib)
00054 {
00055 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00056     return ((*f=_open(name,attrib))!=(-1));
00057 #else
00058     return ((*f=open(name,attrib))!=(-1));
00059 #endif
00060 }
00061 
00062 abyss_bool FileOpenCreate(TFile *f, const char *name, uint32_t attrib)
00063 {
00064 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00065     return ((*f=_open(name,attrib | O_CREAT,_S_IWRITE | _S_IREAD))!=(-1));
00066 #else
00067     return ((*f=open(name,attrib | O_CREAT,S_IWRITE | S_IREAD))!=(-1));
00068 #endif
00069 }
00070 
00071 abyss_bool
00072 FileWrite(TFile *      const f,
00073           const void * const buffer,
00074           uint32_t     const len) {
00075 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00076     return (_write(*f,buffer,len)==(int32_t)len);
00077 #else
00078     return (write(*f,buffer,len)==(int32_t)len);
00079 #endif
00080 }
00081 
00082 int32_t FileRead(TFile *f, void *buffer, uint32_t len)
00083 {
00084 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00085     return (_read(*f,buffer,len));
00086 #else
00087     return (read(*f,buffer,len));
00088 #endif
00089 }
00090 
00091 abyss_bool FileSeek(TFile *f, uint64_t pos, uint32_t attrib)
00092 {
00093 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00094     return (_lseek(*f,pos,attrib)!=(-1));
00095 #else
00096     return (lseek(*f,pos,attrib)!=(-1));
00097 #endif
00098 }
00099 
00100 uint64_t FileSize(TFile *f)
00101 {
00102 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00103     return (_filelength(*f));
00104 #else
00105     struct stat fs;
00106 
00107     fstat(*f,&fs);
00108     return (fs.st_size);
00109 #endif  
00110 }
00111 
00112 abyss_bool FileClose(TFile *f)
00113 {
00114 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00115     return (_close(*f)!=(-1));
00116 #else
00117     return (close(*f)!=(-1));
00118 #endif
00119 }
00120 
00121 
00122 
00123 abyss_bool
00124 FileStat(const char * const filename,
00125          TFileStat *  const filestat) {
00126 #if defined( WIN32 ) && !defined( __BORLANDC__ )
00127     return (_stati64(filename,filestat)!=(-1));
00128 #else
00129     return (stat(filename,filestat)!=(-1));
00130 #endif
00131 }
00132 
00133 
00134 
00135 abyss_bool
00136 FileFindFirst(TFileFind *  const filefind,
00137               const char * const path,
00138               TFileInfo *  const fileinfo) {
00139 #ifdef WIN32
00140     abyss_bool ret;
00141     char *p=path+strlen(path);
00142 
00143     *p='\\';
00144     *(p+1)='*';
00145     *(p+2)='\0';
00146 #ifndef __BORLANDC__
00147     ret=(((*filefind)=_findfirst(path,fileinfo))!=(-1));
00148 #else
00149     *filefind = FindFirstFile( path, &fileinfo->data );
00150    ret = *filefind != NULL;
00151    if( ret )
00152    {
00153       LARGE_INTEGER li;
00154       li.LowPart = fileinfo->data.nFileSizeLow;
00155       li.HighPart = fileinfo->data.nFileSizeHigh;
00156       strcpy( fileinfo->name, fileinfo->data.cFileName );
00157        fileinfo->attrib = fileinfo->data.dwFileAttributes;
00158        fileinfo->size   = li.QuadPart;
00159       fileinfo->time_write = fileinfo->data.ftLastWriteTime.dwLowDateTime;
00160    }
00161 #endif
00162     *p='\0';
00163     return ret;
00164 #else  /* WIN32 */
00165     strncpy(filefind->path,path,NAME_MAX);
00166     filefind->path[NAME_MAX]='\0';
00167     filefind->handle=opendir(path);
00168     if (filefind->handle)
00169         return FileFindNext(filefind,fileinfo);
00170 
00171     return FALSE;
00172 #endif /* WIN32 */
00173 }
00174 
00175 
00176 
00177 abyss_bool FileFindNext(TFileFind *filefind,TFileInfo *fileinfo)
00178 {
00179 #ifdef WIN32
00180 
00181 #ifndef __BORLANDC__
00182     return (_findnext(*filefind,fileinfo)!=(-1));
00183 #else
00184    abyss_bool ret = FindNextFile( *filefind, &fileinfo->data );
00185    if( ret )
00186    {
00187       LARGE_INTEGER li;
00188       li.LowPart = fileinfo->data.nFileSizeLow;
00189       li.HighPart = fileinfo->data.nFileSizeHigh;
00190       strcpy( fileinfo->name, fileinfo->data.cFileName );
00191        fileinfo->attrib = fileinfo->data.dwFileAttributes;
00192        fileinfo->size   = li.QuadPart;
00193       fileinfo->time_write = fileinfo->data.ftLastWriteTime.dwLowDateTime;
00194    }
00195     return ret;
00196 #endif
00197 
00198 #else  /* WIN32 */
00199     struct dirent *de;
00200     /****** Must be changed ***/
00201     char z[NAME_MAX+1];
00202 
00203     de=readdir(filefind->handle);
00204     if (de)
00205     {
00206         struct stat fs;
00207 
00208         strcpy(fileinfo->name,de->d_name);
00209         strcpy(z,filefind->path);
00210         strncat(z,"/",NAME_MAX);
00211         strncat(z,fileinfo->name,NAME_MAX);
00212         z[NAME_MAX]='\0';
00213         
00214         stat(z,&fs);
00215 
00216         if (fs.st_mode & S_IFDIR)
00217             fileinfo->attrib=A_SUBDIR;
00218         else
00219             fileinfo->attrib=0;
00220 
00221         fileinfo->size=fs.st_size;
00222         fileinfo->time_write=fs.st_mtime;
00223         
00224         return TRUE;
00225     };
00226 
00227     return FALSE;
00228 #endif /* WIN32 */
00229 }
00230 
00231 void FileFindClose(TFileFind *filefind)
00232 {
00233 #ifdef WIN32
00234 
00235 #ifndef __BORLANDC__
00236     _findclose(*filefind);
00237 #else
00238    FindClose( *filefind );
00239 #endif
00240 
00241 #else  /* WIN32 */
00242     closedir(filefind->handle);
00243 #endif /* WIN32 */
00244 }

Generated on Thu May 17 10:00:23 2012 for Kamailio - The Open Source SIP Server by  doxygen 1.5.6