00001
00007 #include <dirent.h>
00008 #include <errno.h>
00009 #include <limits.h>
00010 #include <stdbool.h>
00011 #include <stdio.h>
00012 #include <string.h>
00013 #include <strings.h>
00014 #include <sys/stat.h>
00015 #include <sys/types.h>
00016 #include <unistd.h>
00017
00018 #include "check.h"
00019 #include "logger.h"
00020
00022 char file_common_buffer[10240];
00023
00024 off_t file_filesize(const char *filename)
00025 {
00026 struct stat s;
00027
00028 check(filename != NULL);
00029
00030 if (stat(filename, &s) != 0)
00031 {
00032 add_log(ADDLOG_FAIL, "[file]filesize", "unable to get stat information: %s\n", strerror(errno));
00033 log_dump("filename", filename);
00034 return 0;
00035 }
00036
00037 return s.st_size;
00038 }
00039
00040 off_t file_filesize_f(FILE *file)
00041 {
00042 struct stat filestat;
00043
00044 check(file != NULL);
00045
00046 if (fstat(fileno(file), &filestat) != 0)
00047 {
00048 add_log(ADDLOG_FAIL, "[file]filesize_f", "unable to get stat information: %s\n", strerror(errno));
00049 return 0;
00050 }
00051
00052 return filestat.st_size;
00053 }
00054
00055 void file_path_explode(char *path, char **dir, char **file, char **ext)
00056 {
00057 check(path != NULL);
00058 check(dir != NULL);
00059 check(file != NULL);
00060 check(ext != NULL);
00061
00062 *file = strrchr(path, '/');
00063 if (*file == NULL)
00064 {
00065 *dir = NULL;
00066 *file = path;
00067 }
00068 else
00069 {
00070 *dir = path;
00071 **file = '\0';
00072 (*file)++;
00073 }
00074
00075 *ext = strrchr(*file, '.');
00076 if (*ext != NULL)
00077 {
00078 **ext = '\0';
00079 (*ext)++;
00080 }
00081 }
00082
00083 bool file_set_context(const char *device)
00084 {
00085 check(device != NULL);
00086
00087 if (chdir(device) != 0)
00088 {
00089 add_log(ADDLOG_FAIL, "[file]set_context", "unable to work in directory\n");
00090 log_dump("device", device);
00091 return false;
00092 }
00093
00094 return true;
00095 }
00096
00097 bool file_exists(const char *path)
00098 {
00099 check(path != NULL);
00100
00101 return (access(path, F_OK) == 0 ? true : false);
00102 }
00103
00104 const char *file_remove_headslash(const char *path)
00105 {
00106 check(path != NULL);
00107
00108 while(*path == '/')
00109 {
00110 path++;
00111 }
00112
00113 return path;
00114 }
00115
00116 const char *file_get_extension(const char *path)
00117 {
00118 const char *result;
00119
00120 check(path != NULL);
00121
00122
00123
00124 result = strrchr(path, '.');
00125 if (result == NULL)
00126 {
00127 result = "";
00128 }
00129
00130 return result;
00131 }
00132
00133 void file_windows_slashes(char *path)
00134 {
00135 check(path != NULL);
00136
00137 while(*path != '\0')
00138 {
00139 if (*path == '/')
00140 {
00141 *path = '\\';
00142 }
00143 path++;
00144 }
00145 }
00146
00147 FILE *file_fcaseopen(const char *directory, const char *filename, const char *mode)
00148 {
00149 DIR *dir;
00150 struct dirent *entry;
00151 char filepath[PATH_MAX];
00152
00153 check(directory != NULL);
00154 check(filename != NULL);
00155
00156 dir = opendir(directory);
00157 if (dir == NULL)
00158 {
00159 add_log(ADDLOG_FAIL, "[file]fcaseopen", "unable to open directory\n");
00160 log_dump("directory", directory);
00161 log_dump("filename", filename);
00162 return NULL;
00163 }
00164
00165 entry = readdir(dir);
00166 while(entry != NULL)
00167 {
00168 if (strcasecmp(filename, entry->d_name) == 0)
00169 {
00170 closedir(dir);
00171 add_log(ADDLOG_DUMP, "[file]fcaseopen", "resolved\n");
00172 log_dump("filename", filename);
00173 log_dump("entry", entry->d_name);
00174
00175 snprintf(filepath, sizeof(filepath), "%s/%s", directory, entry->d_name);
00176 return fopen(filepath, mode);
00177 }
00178 entry = readdir(dir);
00179 }
00180 closedir(dir);
00181
00182 add_log(ADDLOG_RECOVER, "[file]fcaseopen", "unable to find file in directory\n");
00183 log_dump("filename", filename);
00184 log_dump("directory", directory);
00185
00186 snprintf(filepath, sizeof(filepath), "%s/%s", directory, filename);
00187 return fopen(filepath, mode);
00188 }
00189
00190 bool file_truncate(FILE *file, off_t len)
00191 {
00192 int fd;
00193
00194 check(file != NULL);
00195
00196 fd = fileno(file);
00197 if (fd == -1)
00198 {
00199 add_log(ADDLOG_FAIL, "[file]truncate", "unable to get descriptor of stream 0x%p: %s\n", file, strerror(errno));
00200 return false;
00201 }
00202 if (ftruncate(fd, len) != 0)
00203 {
00204 add_log(ADDLOG_FAIL, "[file]truncate", "unable to truncate file: %s\n", strerror(errno));
00205 return false;
00206 }
00207
00208 return true;
00209 }
00210