Main Page | Modules | Data Structures | File List | Data Fields | Related Pages

mntd_file.c

00001 /***************************************************************************
00002  * CVSID: $Id: mntd_file.c,v 1.1 2004/04/01 19:30:23 stefanb Exp $
00003  *
00004  * mntd_file.c : file handler for MNT daemon
00005  *
00006  * Copyright (C) 2004 Stefan Bambach, <stefan@bambach.biz>
00007  *
00008  * Licensed under the GNU General Public License 2.0
00009  *
00010  * This program is free software; you can redistribute it and/or modify
00011  * it under the terms of the GNU General Public License as published by
00012  * the Free Software Foundation; either version 2 of the License, or
00013  * (at your option) any later version.
00014  *
00015  * This program is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00018  * GNU General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU General Public License
00021  * along with this program; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  *
00024  **************************************************************************/
00025  
00026 #ifdef HAVE_CONFIG_H
00027 #  include <config.h>
00028 #endif
00029 
00030 #include <stdlib.h>
00031 #include <stdio.h>
00032 #include <sys/stat.h>
00033 #include <sys/types.h>
00034 #include <fcntl.h>
00035 #include <unistd.h>
00036 #include <string.h>
00037 #include <stdio.h>
00038 
00039 #include "mntd_file.h"
00040 #include "errmanager.h"
00041 
00042 
00058 int
00059 mntd_file_is_fifo(char *filepath)
00060 {
00061     struct stat st;
00062     
00063     if (stat(filepath, &st) != 0) {
00064         return 0;
00065     }
00066 
00067     if (!S_ISFIFO(st.st_mode)) {
00068         return 0;
00069     }
00070     
00071     return 1;
00072 }
00073 
00074 
00082 int
00083 mntd_file_is_file(char *filepath)
00084 {
00085     struct stat st;
00086     
00087     if (stat(filepath, &st) != 0) {
00088         return 0;
00089     }
00090 
00091     if (!S_ISREG(st.st_mode)) {
00092         return 0;
00093     }
00094     
00095     return 1;
00096 }
00097 
00098 
00106 int
00107 mntd_file_get_stamp(char *filepath)
00108 {
00109     struct stat st;
00110     
00111     if (stat(filepath, &st) != 0) {
00112         return -1;
00113     }
00114 
00115     if (!S_ISREG(st.st_mode)) {
00116         return -1;
00117     }
00118     
00119     return st.st_mtime;
00120 }
00121 
00122 
00130 int
00131 mntd_file_get_size(char *filepath)
00132 {
00133     struct stat st;
00134     
00135     if (stat(filepath, &st) != 0) {
00136         return -1;
00137     }
00138 
00139     if (!S_ISREG(st.st_mode)) {
00140         return -1;
00141     }
00142     
00143     return st.st_size;
00144 }
00145 
00146 
00154 char *
00155 mntd_file_read(char *filepath)
00156 {
00157     char *data = NULL;
00158     FILE *fp = NULL;
00159     int size = 0;
00160     int rsize = 0;
00161     int retval = 0;
00162     
00163     // opening file for reading
00164     fp = fopen(filepath, "r");
00165     if (fp != NULL) {
00166         // getting filesize
00167         size = mntd_file_get_size(filepath);
00168         if (size != -1) {
00169             data = (char *) malloc(sizeof(char)*(size+1));
00170             if (data != NULL) {
00171                 // reading file
00172                 if ((rsize = fread(data, sizeof(char), size, fp)) >= 0) {
00173                     // EOF reached ?
00174                     if (rsize == size) {
00175                         retval = 1; // file successfully read
00176                     }
00177                 }
00178                 data[size]='\0';
00179             }
00180         }
00181 
00182         // closing file
00183         fclose(fp);
00184     }
00185     
00186 
00187     // freeing memory, if error
00188     if (retval == 0) {
00189         if (data != NULL) {
00190             free(data);
00191             data = NULL;
00192         }
00193         
00194         return NULL;
00195     }
00196     
00197     return data;
00198 }
00199 
00200 
00209 int
00210 mntd_file_write(char *filepath, char *data)
00211 {
00212     FILE *fp = NULL;
00213     int retval = 0;
00214     int size = strlen(data);
00215     int wsize = 0;
00216     
00217     // opening file for writing
00218     fp = fopen(filepath, "w");
00219     if (fp != NULL) {
00220         // writing to file
00221         if ((wsize = fwrite(data, sizeof(char), size, fp)) >= 0) {
00222             // EOF reached ?
00223             if (wsize == size) {
00224                 retval = 1; // file successfully written
00225             }
00226         }
00227 
00228         // closing file
00229         fclose(fp);
00230     }
00231     
00232     return retval;
00233 }
00234 
00235 
00243 int
00244 mntd_file_remove(char *filepath)
00245 {
00246     if(mntd_file_is_file(filepath)) {
00247         if(unlink(filepath)==0)
00248             return 1;
00249     }
00250     
00251     return 0;
00252 }
00253 
00254 

Generated on Thu May 27 23:27:28 2004 for Mntd by doxygen 1.3.5