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 #include <utils/system/file.h>
00027 #include <core/exceptions/system.h>
00028
00029 #include <sys/types.h>
00030 #include <sys/stat.h>
00031 #include <unistd.h>
00032 #include <fcntl.h>
00033 #include <string.h>
00034 #include <errno.h>
00035 #include <stdlib.h>
00036 #include <cstdio>
00037
00038 namespace fawkes {
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 UnableToOpenFileException::UnableToOpenFileException(const char *filename, int error)
00049 : Exception("Unable to open file", error)
00050 {
00051 append("File that could not be opened: %s", filename);
00052 }
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 File::File(const char *filename, FileOpenMethod method)
00072 {
00073 fd = -1;
00074
00075 switch (method)
00076 {
00077 case OVERWRITE:
00078 fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
00079 fn = strdup(filename);
00080 break;
00081
00082 case APPEND:
00083 fd = open(filename, O_RDWR | O_CREAT | O_APPEND, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
00084 fn = strdup(filename);
00085 break;
00086
00087 case ADD_SUFFIX:
00088 {
00089 char *filename_ext = strdup(filename);
00090 int index = 0;
00091 while (File::exists(filename_ext)) {
00092 free(filename_ext);
00093 if ( asprintf(&filename_ext, "%s.%d", filename, ++index) == -1 ) {
00094 throw OutOfMemoryException("Could not allocate filename string");
00095 }
00096
00097 }
00098 fd = open(filename_ext, O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
00099 fn = filename_ext;
00100 }
00101 break;
00102
00103 default:
00104 printf("%s [line %d]: Unkown method.\n", __FILE__, __LINE__);
00105 }
00106
00107 if (-1 == fd)
00108 {
00109 throw UnableToOpenFileException(filename, errno);
00110 }
00111
00112 fp = fdopen(fd, "r+");
00113 }
00114
00115
00116
00117 File::~File()
00118 {
00119
00120 fclose(fp);
00121 free(fn);
00122 }
00123
00124
00125
00126
00127
00128 FILE *
00129 File::stream() const
00130 {
00131 return fp;
00132 }
00133
00134
00135
00136
00137 const char *
00138 File::filename() const
00139 {
00140 return fn;
00141 }
00142
00143
00144
00145
00146
00147
00148 bool
00149 File::exists(const char *filename)
00150 {
00151 return (access(filename, F_OK) == 0);
00152 }
00153
00154
00155
00156
00157
00158
00159 bool
00160 File::is_regular(const char *filename)
00161 {
00162 struct stat s;
00163
00164 if ( stat(filename, &s) == 0 ) {
00165 return S_ISREG(s.st_mode);
00166 } else {
00167 return false;
00168 }
00169 }
00170
00171
00172 }