Main Page   Modules   Alphabetical List   Data Structures   File List   Data Fields   Globals  

Files.c

Go to the documentation of this file.
00001 /******************************************************************
00002         ANet_Daemon/Linux/Files.c
00003         Code for file management in the ANet deamon.
00004         This code is specific to Linux.
00005 
00006         Part of the run-time wrapper of the ANet project.
00007 
00008         Distributed under GPL: http://www.gnu.org/copyleft/gpl.html
00009 ******************************************************************/
00010 
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <stdlib.h>
00014 #include <sys/types.h>
00015 #include <sys/stat.h>
00016 #include <fcntl.h>
00017 #include <unistd.h>
00018 #include <errno.h>
00019 #include "Files.h"
00020 
00032 ANetFile ANetInitFile(void)
00033 {
00034   ANetFile f;
00035 
00036   f.busy = FALSE;
00037   f.open = FALSE;
00038   f.async = TRUE;
00039   f.path = NULL;
00040 
00041   return f;
00042 }
00043 
00044 UInt32 ANetGetSetFilePath(ANetFile *f, ANetMemoryTag path, UInt8 set)
00045 {
00046   int len;
00047 
00048   if (!f)
00049     return 2; // ?
00050 
00051   if (f->busy)
00052     return 60;
00053 
00054   if (f->open)
00055     return 4;
00056 
00057   if (set)
00058   {
00059     len = strlen(path);
00060     f->path = malloc(len + 1);
00061     if (!f->path)
00062       return 5;
00063     else
00064     {
00065       FILE * fp;
00066       strncpy(f->path, path, len);
00067       fp = fopen(f->path, "w"); // is there any better way to create a file?
00068       fclose(fp);
00069       return 0;
00070     }
00071   }
00072   else
00073   {
00074     //doubts
00075   }
00076 
00077   return 0; // ??
00078 }
00079 
00080 UInt32 ANetGetSetFileAsync(ANetFile *f, UInt8 *async, UInt8 set)
00081 {
00082   if (!async)
00083     return 1;
00084 
00085   if (f->busy)
00086     return 60;
00087 
00088   if (set)
00089   {
00090     if (f->busy)
00091       return 60;
00092 
00093     if (f->async && *async)
00094       return 0;
00095 
00096     if (!(f->async || *async))
00097       return 0;
00098 
00099     close(f->fd);
00100 
00101     if (*async)
00102       f->fd = open(f->path, O_RDWR | O_APPEND);
00103     else
00104       f->fd = open(f->path, O_RDWR | O_APPEND | O_SYNC);
00105 
00106     if (f->fd < 0)
00107       return -1; // open failed
00108   }
00109   else
00110     *async = f->async;
00111 
00112   return 0;
00113 }
00114 
00115 UInt8 ANetIsFileBusy(ANetFile * f)
00116 {
00117   return f->busy;
00118 }
00119 
00120 UInt32 ANetGetFileSize(ANetFile *f, UInt32 *size)
00121 {
00122   struct stat statbuf;
00123   extern int errno;
00124 
00125   if (f->busy)
00126     return 60;
00127 
00128   if (!size)
00129     return 3;
00130 
00131   if (stat(f->path, &statbuf) < 0)
00132   {
00133     switch(errno)
00134     {
00135       case ENOENT: return 2;
00136       default: return 1;
00137     }
00138   }
00139 
00140   *size = statbuf.st_size;
00141 
00142   return 0;
00143 }
00144 
00145 UInt32 ANetOpenFile(ANetFile *f)
00146 {
00147   if (f->busy)
00148     return 60;
00149 
00150   if (f->open)
00151     return 2; // this is not part of the doc
00152 
00153   if (f->async)
00154     f->fd = open(f->path, O_RDWR | O_APPEND);
00155   else
00156     f->fd = open(f->path, O_RDWR | O_APPEND | O_SYNC);
00157 
00158   if (f->fd < 0)
00159     return 1;
00160 
00161   return 0;
00162 }
00163 
00164 UInt32 ANetCloseFile(ANetFile *f)
00165 {
00166   if (f->busy)
00167     return 60;
00168 
00169   if (close(f->fd) < 0)
00170     return 1;
00171 
00172   return 0;
00173 }
00174 
00175 UInt32 ANetReadFile(ANetFile *f, UInt32 startPos, UInt32 length,
00176         ANetMemoryTag buffer, UInt32 *read)
00177 {
00178   return 0;
00179 }
00180 
00181 UInt32 ANetWriteFile(ANetFile *f, ANetMemoryTag buffer);
00182 
00183 UInt32 ANetWriteFileAt(ANetFile *f, ANetMemoryTag buffer, UInt32 pos);
00184 
00185 UInt32 ANetDeleteFile(ANetFile *f);
00186 
00187 UInt32 ANetCopyFile(ANetFile *source, ANetFile *dest);
00188 

Generated on Sun Dec 23 15:20:37 2001 for ANet by doxygen 1.2.12 written by Dimitri van Heesch, © 1997-2001

SourceForge
Logo