Changeset 15505
- Timestamp:
- Nov 7, 2007, 6:24:01 PM (19 years ago)
- File:
-
- 1 edited
-
trunk/psLib/src/types/psMetadataConfig.c (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/psLib/src/types/psMetadataConfig.c
r15411 r15505 11 11 * @author Joshua Hoblitt, University of Hawaii 2006-2007 12 12 * 13 * @version $Revision: 1.14 1$ $Name: not supported by cvs2svn $14 * @date $Date: 2007-1 0-29 21:36:02$13 * @version $Revision: 1.142 $ $Name: not supported by cvs2svn $ 14 * @date $Date: 2007-11-08 04:24:01 $ 15 15 * 16 16 * Copyright 2004-2005 Maui High Performance Computing Center, University of Hawaii … … 26 26 #include <limits.h> 27 27 #include <strings.h> 28 #ifdef USE_MMAP_TO_READ_FILE29 #include <sys/mman.h>30 #include <sys/stat.h>31 #include <fcntl.h>32 #include <unistd.h>33 #else34 28 #include "psSlurp.h" 35 #endif36 29 37 30 #include "psMemory.h" … … 1181 1174 PS_ASSERT_STRING_NON_EMPTY(filename, NULL); 1182 1175 1183 #ifdef USE_MMAP_TO_READ_FILE1184 // Attempt to open specified file1185 int fd = 0;1186 fd = open(filename, O_RDONLY);1187 if (fd < 0) {1188 // XXX really should return strerror() here1189 psError(PS_ERR_IO, true,1190 _("Failed to open file '%s'. Check if it exists and it has the proper "1191 "permissions."), filename);1192 close(fd);1193 return NULL;1194 }1195 1196 // Results of stat() for file1197 struct stat buf;1198 if (fstat(fd, &buf) != 0) {1199 psError(PS_ERR_IO, true, _("Unable to stat file '%s'.\n"), filename);1200 close(fd);1201 return NULL;1202 }1203 // XXX EAM : for some reason, the mmap below results in a buffer which causes1204 // bus errors if the file is empty. This seems like a strange result...1205 if (buf.st_size == 0) {1206 // We successfully read everything we could. Return a MD containing what we read.1207 close(fd);1208 return psMetadataAlloc();1209 }1210 1211 // psMetadataConfigParse() is going to read the entire file into memory so1212 // lets use mmap() to allow the system to be cleverer about what gets read1213 //1214 // We're adding 1 to buf.st_size here in case st_size / system page size has1215 // a remainder of zero (otherwise there won't be any trailing \0s at the end1216 // of the page).1217 int nBytes = buf.st_size + 1;1218 1219 void *file = mmap(0, nBytes, PROT_READ, MAP_PRIVATE, fd, 0);1220 if (file == MAP_FAILED) {1221 psError(PS_ERR_IO, true, _("failed to mmap() file %s"), filename);1222 if (close(fd) != 0) {1223 psError(PS_ERR_IO, true, _("Failed to close file '%s'."), filename);1224 }1225 return NULL;1226 }1227 #else1228 1176 psString file = psSlurpFilename(filename); 1229 #endif1230 1177 1231 1178 md = psMetadataConfigParse(md, nFail, (char *)file, overwrite); … … 1236 1183 } 1237 1184 1238 #ifdef USE_MMAP_TO_READ_FILE1239 if (munmap(file, nBytes) != 0) {1240 psError(PS_ERR_IO, true, _("failed to munmap file '%s'"), filename);1241 psFree(md);1242 return false;1243 }1244 1245 if (close(fd) != 0) {1246 psError(PS_ERR_IO, true, _("Failed to close file '%s'."), filename);1247 psFree(md);1248 return false;1249 }1250 #else1251 1185 psFree(file); 1252 #endif1253 1186 1254 1187 return md;
Note:
See TracChangeset
for help on using the changeset viewer.
