00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
00164 fp = fopen(filepath, "r");
00165 if (fp != NULL) {
00166
00167 size = mntd_file_get_size(filepath);
00168 if (size != -1) {
00169 data = (char *) malloc(sizeof(char)*(size+1));
00170 if (data != NULL) {
00171
00172 if ((rsize = fread(data, sizeof(char), size, fp)) >= 0) {
00173
00174 if (rsize == size) {
00175 retval = 1;
00176 }
00177 }
00178 data[size]='\0';
00179 }
00180 }
00181
00182
00183 fclose(fp);
00184 }
00185
00186
00187
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
00218 fp = fopen(filepath, "w");
00219 if (fp != NULL) {
00220
00221 if ((wsize = fwrite(data, sizeof(char), size, fp)) >= 0) {
00222
00223 if (wsize == size) {
00224 retval = 1;
00225 }
00226 }
00227
00228
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