dra_file_util.cxx
Go to the documentation of this file.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
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049 #include <fcntl.h>
00050 #include <unistd.h>
00051 #include <sys/stat.h>
00052 #include <sys/mman.h>
00053 #include <sys/types.h>
00054 #include <sys/param.h>
00055
00056 #include "defs.h"
00057 #include "x_libgen.h"
00058 #include "glob.h"
00059 #include "erbe.h"
00060 #include "erglob.h"
00061 #include "file_util.h"
00062
00063 #include "dra_internal.h"
00064
00065
00066
00067
00068
00069
00070
00071
00072 INT DRA_file_desc = -1;
00073
00074 char *DRA_file_mmap = NULL;
00075
00076 char DRA_file_name[MAXPATHLEN];
00077
00078
00079
00080
00081
00082
00083
00084
00085 static void DRA_Make_File_Name();
00086
00087
00088
00089
00090
00091
00092
00093 static char *DRA_keep_old_file;
00094
00095 static off_t DRA_file_size, DRA_bytes_to_keep;
00096
00097 static const char* DRA_DIRECTORY = "/rii_files/";
00098
00099 static const char* DRA_FILE_EXTENSION = ".rii";
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116
00117 void
00118 DRA_Open_And_Map_File()
00119 {
00120 Set_Error_Phase("Reading prelinker file");
00121
00122 DRA_Make_File_Name();
00123
00124 struct stat stat_buf;
00125 errno = 0;
00126
00127 DRA_file_desc = open(DRA_file_name, O_RDONLY);
00128
00129 if (DRA_file_desc < 0 || fstat(DRA_file_desc, &stat_buf) != 0) {
00130 close(DRA_file_desc);
00131 ErrMsg(EC_DRA_rii_file_io, DRA_file_name, errno);
00132 return;
00133 }
00134
00135 DRA_file_mmap = (char *) mmap(0,
00136 stat_buf.st_size,
00137 PROT_READ|PROT_WRITE,
00138 MAP_PRIVATE,
00139 DRA_file_desc,
00140 0);
00141
00142 if (DRA_file_mmap == (char *)(MAP_FAILED)) {
00143 close(DRA_file_desc);
00144 ErrMsg(EC_DRA_rii_file_io, DRA_file_name, errno);
00145 return;
00146 }
00147
00148 close(DRA_file_desc);
00149
00150
00151
00152 DRA_file_size = stat_buf.st_size;
00153
00154
00155
00156
00157
00158
00159
00160
00161 char *first_sep = strstr(DRA_file_mmap, DRA_FILE_SEPARATOR);
00162
00163
00164
00165 if (first_sep == NULL) {
00166 (void) unlink(DRA_file_name);
00167 ErrMsg(EC_DRA_rii_file_format, DRA_file_name);
00168 return;
00169 }
00170
00171 char *second_sep = strstr(first_sep+1, DRA_FILE_SEPARATOR);
00172
00173 if (second_sep == NULL) {
00174 DRA_bytes_to_keep = DRA_file_size;
00175 }
00176 else {
00177 DRA_bytes_to_keep = second_sep - DRA_file_mmap;
00178 }
00179
00180 DRA_keep_old_file = CXX_NEW_ARRAY(char, DRA_bytes_to_keep, Malloc_Mem_Pool);
00181 if (DRA_keep_old_file == NULL) {
00182 ErrMsg(EC_No_Mem, "DRA_Open_And_Map_File");
00183 return;
00184 }
00185 else {
00186 (void) strncpy(DRA_keep_old_file, DRA_file_mmap, DRA_bytes_to_keep);
00187 }
00188 }
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202 void
00203 DRA_Set_Write_Location(void)
00204 {
00205 errno = 0;
00206
00207 DRA_file_desc = open(DRA_file_name, O_WRONLY|O_TRUNC);
00208
00209 if (DRA_file_desc < 0) {
00210 close(DRA_file_desc);
00211 ErrMsg(EC_DRA_rii_file_io, DRA_file_name, errno);
00212 return;
00213 }
00214
00215 write(DRA_file_desc, (void*)DRA_keep_old_file, DRA_bytes_to_keep);
00216 write(DRA_file_desc, (void*)DRA_FILE_SEPARATOR, strlen(DRA_FILE_SEPARATOR));
00217
00218 CXX_DELETE_ARRAY(DRA_keep_old_file, Malloc_Mem_Pool);
00219 }
00220
00221
00222
00223
00224
00225
00226
00227
00228
00229
00230
00231
00232 void
00233 DRA_Mem_Unmap_File()
00234 {
00235
00236 (void) munmap(DRA_file_mmap, (size_t)DRA_file_size);
00237 }
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247
00248
00249
00250 void
00251 DRA_Close_File()
00252 {
00253 if (DRA_file_desc >= 0) {
00254 close(DRA_file_desc);
00255 }
00256 }
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 static void
00279 DRA_Make_File_Name()
00280 {
00281 char *obj_file_name = Obj_File_Name ?
00282 Obj_File_Name : New_Extension (Src_File_Name, ".o");
00283
00284 char *dir = ux_dirname(obj_file_name);
00285 strcpy (DRA_file_name, dir);
00286 strcat (DRA_file_name, DRA_DIRECTORY);
00287
00288 char *base = ux_basename(obj_file_name);
00289 INT baselen = strlen(base);
00290
00291 if (base[baselen-2] == '.' && base[baselen-1] == 'o')
00292 strcpy(&base[baselen-2], DRA_FILE_EXTENSION);
00293 else
00294 strcpy(&base[baselen], DRA_FILE_EXTENSION);
00295
00296 strcat (DRA_file_name, base);
00297 }
00298