X-Git-Url: http://vcs.maemo.org/git/?a=blobdiff_plain;f=src%2Ffiles.c;fp=src%2Ffiles.c;h=fc42a278192e813c9a039a04b6d2fe268eba6407;hb=9dffc9de96014e24d1fd1030a79317ba34c504e8;hp=ce595ab85ac44ec55a6f68256353e2e0a8820ac9;hpb=42c9226fc71c19af4d755c6900120bfa07f7e99c;p=routino diff --git a/src/files.c b/src/files.c index ce595ab..fc42a27 100644 --- a/src/files.c +++ b/src/files.c @@ -1,5 +1,5 @@ /*************************************** - $Header: /home/amb/routino/src/RCS/files.c,v 1.18 2010/03/29 18:20:06 amb Exp $ + $Header: /home/amb/routino/src/RCS/files.c,v 1.25 2010/10/31 17:52:40 amb Exp $ Functions to handle files. @@ -21,9 +21,11 @@ along with this program. If not, see . ***************************************/ +#include #include #include +#include #include #include #include @@ -31,7 +33,7 @@ #include #include -#include "functions.h" +#include "files.h" /*+ A structure to contain the list of memory mapped files. +*/ @@ -64,7 +66,7 @@ static int nmappedfiles=0; char *FileName(const char *dirname,const char *prefix, const char *name) { - char *filename=(char*)malloc((dirname?strlen(dirname):0)+1+(prefix?strlen(prefix):0)+1+strlen(name)); + char *filename=(char*)malloc((dirname?strlen(dirname):0)+1+(prefix?strlen(prefix):0)+1+strlen(name)+1); sprintf(filename,"%s%s%s%s%s",dirname?dirname:"",dirname?"/":"",prefix?prefix:"",prefix?"-":"",name); @@ -100,7 +102,54 @@ void *MapFile(const char *filename) { close(fd); - fprintf(stderr,"Cannot mmap file '%s' [%s].\n",filename,strerror(errno)); + assert(0); + + fprintf(stderr,"Cannot mmap file '%s' for reading [%s].\n",filename,strerror(errno)); + exit(EXIT_FAILURE); + } + + mappedfiles=(struct mmapinfo*)realloc((void*)mappedfiles,(nmappedfiles+1)*sizeof(struct mmapinfo)); + + mappedfiles[nmappedfiles].filename=filename; + mappedfiles[nmappedfiles].fd=fd; + mappedfiles[nmappedfiles].address=address; + mappedfiles[nmappedfiles].length=size; + + nmappedfiles++; + + return(address); +} + + +/*++++++++++++++++++++++++++++++++++++++ + Open a file and map it into memory. + + void *MapFileWriteable Returns the address of the file or exits in case of an error. + + const char *filename The name of the file to open. + ++++++++++++++++++++++++++++++++++++++*/ + +void *MapFileWriteable(const char *filename) +{ + int fd; + off_t size; + void *address; + + /* Open the file and get its size */ + + fd=ReOpenFileWriteable(filename); + + size=SizeFile(filename); + + /* Map the file */ + + address=mmap(NULL,size,PROT_READ|PROT_WRITE,MAP_SHARED,fd,0); + + if(address==MAP_FAILED) + { + close(fd); + + fprintf(stderr,"Cannot mmap file '%s' for reading and writing [%s].\n",filename,strerror(errno)); exit(EXIT_FAILURE); } @@ -161,18 +210,18 @@ void *UnmapFile(const char *filename) /*++++++++++++++++++++++++++++++++++++++ Open a new file on disk for writing to. - int OpenFile Returns the file descriptor if OK or exits in case of an error. + int OpenFileNew Returns the file descriptor if OK or exits in case of an error. const char *filename The name of the file to create. ++++++++++++++++++++++++++++++++++++++*/ -int OpenFile(const char *filename) +int OpenFileNew(const char *filename) { int fd; /* Open the file */ - fd=open(filename,O_WRONLY|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); + fd=open(filename,O_RDWR|O_CREAT|O_TRUNC,S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH); if(fd<0) { @@ -185,14 +234,14 @@ int OpenFile(const char *filename) /*++++++++++++++++++++++++++++++++++++++ - Open a new file on disk for reading and appending. + Open a new file on disk for reading from and appending. - int AppendFile Returns the file descriptor if OK or exits in case of an error. + int OpenFileAppend Returns the file descriptor if OK or exits in case of an error. const char *filename The name of the file to create. ++++++++++++++++++++++++++++++++++++++*/ -int AppendFile(const char *filename) +int OpenFileAppend(const char *filename) { int fd; @@ -211,7 +260,7 @@ int AppendFile(const char *filename) /*++++++++++++++++++++++++++++++++++++++ - Open an existing file on disk for reading from. + Open an existing file on disk for reading. int ReOpenFile Returns the file descriptor if OK or exits in case of an error. @@ -237,48 +286,28 @@ int ReOpenFile(const char *filename) /*++++++++++++++++++++++++++++++++++++++ - Write data to a file on disk. - - int WriteFile Returns 0 if OK or something else in case of an error. + Open an existing file on disk for reading from or writing to. - int fd The file descriptor to write to. + int ReOpenFileWriteable Returns the file descriptor if OK or exits in case of an error. - const void *address The address of the data to be written from. - - size_t length The length of data to write. + const char *filename The name of the file to open. ++++++++++++++++++++++++++++++++++++++*/ -int WriteFile(int fd,const void *address,size_t length) +int ReOpenFileWriteable(const char *filename) { - /* Write the data */ - - if(write(fd,address,length)!=length) - return(-1); - - return(0); -} - - -/*++++++++++++++++++++++++++++++++++++++ - Read data from a file on disk. - - int ReadFile Returns 0 if OK or something else in case of an error. - - int fd The file descriptor to read from. - - void *address The address of the data to be read into. + int fd; - size_t length The length of data to read. - ++++++++++++++++++++++++++++++++++++++*/ + /* Open the file */ -int ReadFile(int fd,void *address,size_t length) -{ - /* Read the data */ + fd=open(filename,O_RDWR); - if(read(fd,address,length)!=length) - return(-1); + if(fd<0) + { + fprintf(stderr,"Cannot open file '%s' for reading and writing [%s].\n",filename,strerror(errno)); + exit(EXIT_FAILURE); + } - return(0); + return(fd); } @@ -324,27 +353,6 @@ int ExistsFile(const char *filename) /*++++++++++++++++++++++++++++++++++++++ - Seek to a position in a file on disk. - - int SeekFile Returns 0 if OK or something else in case of an error. - - int fd The file descriptor to seek within. - - off_t position The position to seek to. - ++++++++++++++++++++++++++++++++++++++*/ - -int SeekFile(int fd,off_t position) -{ - /* Seek the data */ - - if(lseek(fd,position,SEEK_SET)!=position) - return(-1); - - return(0); -} - - -/*++++++++++++++++++++++++++++++++++++++ Close a file on disk. int fd The file descriptor to close.