| 1 |
#include "protoFile.h" |
| 2 |
|
| 3 |
#include <string.h> // for strerror() |
| 4 |
#include <stdio.h> // for rename() |
| 5 |
#include <time.h> // for difftime() |
| 6 |
#ifdef HAVE_FLOCK |
| 7 |
#include <sys/file.h> // for flock() |
| 8 |
#endif |
| 9 |
#ifdef WIN32 |
| 10 |
#ifndef _WIN32_WCE |
| 11 |
#include <errno.h> |
| 12 |
#include <direct.h> |
| 13 |
#include <share.h> |
| 14 |
#include <io.h> |
| 15 |
#endif // !_WIN32_WCE |
| 16 |
#else |
| 17 |
#include <unistd.h> |
| 18 |
#endif // if/else WIN32 |
| 19 |
|
| 20 |
#ifndef _WIN32_WCE |
| 21 |
#include <fcntl.h> |
| 22 |
#include <sys/types.h> |
| 23 |
#include <sys/stat.h> |
| 24 |
#endif // !_WIN32_WCE |
| 25 |
|
| 26 |
ProtoFile::ProtoFile() |
| 27 |
#ifdef _WIN32_WCE |
| 28 |
: file_ptr(NULL) |
| 29 |
#else |
| 30 |
#endif // if/else _WIN32_WCE |
| 31 |
{ |
| 32 |
} |
| 33 |
|
| 34 |
ProtoFile::~ProtoFile() |
| 35 |
{ |
| 36 |
if (IsOpen()) Close(); |
| 37 |
} // end ProtoFile::~ProtoFile() |
| 38 |
|
| 39 |
// This should be called with a full path only! (Why?) |
| 40 |
bool ProtoFile::Open(const char* thePath, int theFlags) |
| 41 |
{ |
| 42 |
bool returnvalue=false; |
| 43 |
ASSERT(!IsOpen()); |
| 44 |
if (theFlags & O_CREAT) |
| 45 |
{ |
| 46 |
// Create sub-directories as needed. |
| 47 |
char tempPath[PATH_MAX+1]; |
| 48 |
tempPath[PATH_MAX] = '\0'; |
| 49 |
strncpy(tempPath, thePath, PATH_MAX); |
| 50 |
char* ptr = strrchr(tempPath, PROTO_PATH_DELIMITER); |
| 51 |
if (NULL != ptr) |
| 52 |
{ |
| 53 |
*ptr = '\0'; |
| 54 |
ptr = NULL; |
| 55 |
while (!ProtoFile::Exists(tempPath)) |
| 56 |
{ |
| 57 |
char* ptr2 = ptr; |
| 58 |
ptr = strrchr(tempPath, PROTO_PATH_DELIMITER); |
| 59 |
if (ptr2) *ptr2 = PROTO_PATH_DELIMITER; |
| 60 |
if (ptr) |
| 61 |
{ |
| 62 |
*ptr = '\0'; |
| 63 |
} |
| 64 |
else |
| 65 |
{ |
| 66 |
ptr = tempPath; |
| 67 |
break; |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
if (ptr && ('\0' == *ptr)) *ptr++ = PROTO_PATH_DELIMITER; |
| 72 |
while (ptr) |
| 73 |
{ |
| 74 |
ptr = strchr(ptr, PROTO_PATH_DELIMITER); |
| 75 |
if (ptr) *ptr = '\0'; |
| 76 |
#ifdef WIN32 |
| 77 |
#ifdef _WIN32_WCE |
| 78 |
#ifdef _UNICODE |
| 79 |
wchar_t wideBuffer[MAX_PATH]; |
| 80 |
size_t pathLen = strlen(tempPath) + 1; |
| 81 |
if (pathLen > MAX_PATH) pathLen = MAX_PATH; |
| 82 |
mbstowcs(wideBuffer, tempPath, pathLen); |
| 83 |
if (!CreateDirectory(wideBuffer, NULL)) |
| 84 |
#else |
| 85 |
if (!CreateDirectory(tempPath, NULL)) |
| 86 |
#endif // if/else _UNICODE |
| 87 |
#else |
| 88 |
if (_mkdir(tempPath)) |
| 89 |
#endif // if/else _WIN32_WCE |
| 90 |
#else |
| 91 |
if (mkdir(tempPath, 0755)) |
| 92 |
#endif // if/else WIN32/UNIX |
| 93 |
{ |
| 94 |
PLOG(PL_FATAL, "ProtoFile::Open() mkdir(%s) error: %s\n", |
| 95 |
tempPath, GetErrorString()); |
| 96 |
return false; |
| 97 |
} |
| 98 |
if (ptr) *ptr++ = PROTO_PATH_DELIMITER; |
| 99 |
} |
| 100 |
} |
| 101 |
#ifdef WIN32 |
| 102 |
// Make sure we're in binary mode (important for WIN32) |
| 103 |
theFlags |= O_BINARY; |
| 104 |
#ifdef _WIN32_WCE |
| 105 |
if (theFlags & O_RDONLY) |
| 106 |
file_ptr = fopen(thePath, "rb"); |
| 107 |
else |
| 108 |
file_ptr = fopen(thePath, "w+b"); |
| 109 |
if (NULL != file_ptr) |
| 110 |
#else |
| 111 |
// Allow sharing of read-only files but not of files being written |
| 112 |
if (theFlags & O_RDONLY) |
| 113 |
descriptor = _sopen(thePath, theFlags, _SH_DENYNO); |
| 114 |
else |
| 115 |
descriptor = _open(thePath, theFlags, 0640); |
| 116 |
input_handle = (HANDLE)_get_osfhandle(descriptor); |
| 117 |
input_event_handle = output_handle = output_event_handle = input_handle; |
| 118 |
if(descriptor >= 0) |
| 119 |
#endif // if/else _WIN32_WCE |
| 120 |
{ |
| 121 |
offset = 0; |
| 122 |
flags = theFlags; |
| 123 |
returnvalue = true; // no error |
| 124 |
} |
| 125 |
else |
| 126 |
{ |
| 127 |
PLOG(PL_FATAL, "Error opening file \"%s\": %s\n", thePath, GetErrorString()); |
| 128 |
flags = 0; |
| 129 |
return false; |
| 130 |
} |
| 131 |
#else // WIN32 not defined |
| 132 |
if((descriptor = open(thePath, theFlags, 0640)) >= 0) |
| 133 |
{ |
| 134 |
offset = 0; |
| 135 |
returnvalue = true; // no error |
| 136 |
} |
| 137 |
else |
| 138 |
{ |
| 139 |
PLOG(PL_FATAL, "protoFile: Error opening file \"%s\": %s\n", |
| 140 |
thePath, GetErrorString()); |
| 141 |
return false; |
| 142 |
} |
| 143 |
#endif // if/else WIN32/UNIX |
| 144 |
if (returnvalue) |
| 145 |
{ |
| 146 |
return ProtoChannel::Open(); |
| 147 |
} |
| 148 |
return returnvalue; |
| 149 |
} // end ProtoFile::Open() |
| 150 |
|
| 151 |
void ProtoFile::Close() |
| 152 |
{ |
| 153 |
if (IsOpen()) |
| 154 |
{ |
| 155 |
#ifdef WIN32 |
| 156 |
#ifdef _WIN32_WCE |
| 157 |
fclose(file_ptr); |
| 158 |
file_ptr = NULL; |
| 159 |
#else |
| 160 |
_close(descriptor); |
| 161 |
descriptor = -1; |
| 162 |
#endif // if/else _WIN32_WCE |
| 163 |
#else |
| 164 |
close(descriptor); |
| 165 |
descriptor = -1; |
| 166 |
#endif // if/else WIN32 |
| 167 |
ProtoChannel::Close(); |
| 168 |
} |
| 169 |
} // end ProtoFile::Close() |
| 170 |
|
| 171 |
// Routines to try to get an exclusive lock on a file |
| 172 |
bool ProtoFile::Lock() |
| 173 |
{ |
| 174 |
#ifndef WIN32 // WIN32 files are automatically locked |
| 175 |
fchmod(descriptor, 0640 | S_ISGID); |
| 176 |
#ifdef HAVE_FLOCK |
| 177 |
if (flock(descriptor, LOCK_EX | LOCK_NB)) |
| 178 |
return false; |
| 179 |
else |
| 180 |
#else |
| 181 |
#ifdef HAVE_LOCKF |
| 182 |
if (lockf(descriptor, F_LOCK, 0)) // assume lockf if not flock |
| 183 |
return false; |
| 184 |
else |
| 185 |
#endif // HAVE_LOCKF |
| 186 |
#endif // if/else HAVE_FLOCK |
| 187 |
#endif // !WIN32 |
| 188 |
return true; |
| 189 |
} // end ProtoFile::Lock() |
| 190 |
|
| 191 |
void ProtoFile::Unlock() |
| 192 |
{ |
| 193 |
#ifndef WIN32 |
| 194 |
#ifdef HAVE_FLOCK |
| 195 |
if (0 != flock(descriptor, LOCK_UN)) |
| 196 |
{ |
| 197 |
PLOG(PL_ERROR, "ProtoFile::Unlock() flock() error: %s\n", GetErrorString()); |
| 198 |
} |
| 199 |
#else |
| 200 |
#ifdef HAVE_LOCKF |
| 201 |
if (0 != lockf(descriptor, F_ULOCK, 0)) |
| 202 |
{ |
| 203 |
PLOG(PL_ERROR, "ProtoFile::Unlock() lockf() error: %s\n", GetErrorString()); |
| 204 |
} |
| 205 |
#endif // HAVE_LOCKF |
| 206 |
#endif // if/elseHAVE_FLOCK |
| 207 |
fchmod(descriptor, 0640); |
| 208 |
#endif // !WIN32 |
| 209 |
} // end ProtoFile::UnLock() |
| 210 |
|
| 211 |
bool ProtoFile::Rename(const char* oldName, const char* newName) |
| 212 |
{ |
| 213 |
if (!strcmp(oldName, newName)) return true; // no change required |
| 214 |
// Make sure the new file name isn't an existing "busy" file |
| 215 |
// (This also builds sub-directories as needed) |
| 216 |
if (ProtoFile::IsLocked(newName)) |
| 217 |
{ |
| 218 |
PLOG(PL_FATAL, "ProtoFile::Rename() error: file is locked\n"); |
| 219 |
return false; |
| 220 |
} |
| 221 |
#ifdef WIN32 |
| 222 |
// In Win32, the new file can't already exist |
| 223 |
if (ProtoFile::Exists(newName)) |
| 224 |
{ |
| 225 |
#ifdef _WIN32_WCE |
| 226 |
#ifdef _UNICODE |
| 227 |
wchar_t wideBuffer[MAX_PATH]; |
| 228 |
size_t pathLen = strlen(newName) + 1; |
| 229 |
if (pathLen > MAX_PATH) pathLen = MAX_PATH; |
| 230 |
mbstowcs(wideBuffer, newName, pathLen); |
| 231 |
if (0 == DeleteFile(wideBuffer)) |
| 232 |
#else |
| 233 |
if (0 == DeleteFile(newName)) |
| 234 |
#endif // if/else _UNICODE |
| 235 |
{ |
| 236 |
PLOG(PL_FATAL, "ProtoFile::Rename() DeleteFile() error: %s\n", GetErrorString()); |
| 237 |
return false; |
| 238 |
} |
| 239 |
#else |
| 240 |
if (0 != _unlink(newName)) |
| 241 |
{ |
| 242 |
PLOG(PL_FATAL, "ProtoFile::Rename() _unlink() error: %s\n", GetErrorString()); |
| 243 |
return false; |
| 244 |
} |
| 245 |
#endif // if/else _WIN32_WCE |
| 246 |
} |
| 247 |
// In Win32, the old file can't be open |
| 248 |
int oldFlags = 0; |
| 249 |
if (IsOpen()) |
| 250 |
{ |
| 251 |
oldFlags = flags; |
| 252 |
oldFlags &= ~(O_CREAT | O_TRUNC); // unset these |
| 253 |
Close(); |
| 254 |
} |
| 255 |
#endif // WIN32 |
| 256 |
// Create sub-directories as needed. |
| 257 |
char tempPath[PATH_MAX+1]; |
| 258 |
tempPath[PATH_MAX] = '\0'; |
| 259 |
strncpy(tempPath, newName, PATH_MAX); |
| 260 |
char* ptr = strrchr(tempPath, PROTO_PATH_DELIMITER); |
| 261 |
if (ptr) *ptr = '\0'; |
| 262 |
ptr = NULL; |
| 263 |
while (!ProtoFile::Exists(tempPath)) |
| 264 |
{ |
| 265 |
char* ptr2 = ptr; |
| 266 |
ptr = strrchr(tempPath, PROTO_PATH_DELIMITER); |
| 267 |
if (ptr2) *ptr2 = PROTO_PATH_DELIMITER; |
| 268 |
if (ptr) |
| 269 |
{ |
| 270 |
*ptr = '\0'; |
| 271 |
} |
| 272 |
else |
| 273 |
{ |
| 274 |
ptr = tempPath; |
| 275 |
break; |
| 276 |
} |
| 277 |
} |
| 278 |
if (ptr && ('\0' == *ptr)) *ptr++ = PROTO_PATH_DELIMITER; |
| 279 |
while (ptr) |
| 280 |
{ |
| 281 |
ptr = strchr(ptr, PROTO_PATH_DELIMITER); |
| 282 |
if (ptr) *ptr = '\0'; |
| 283 |
#ifdef WIN32 |
| 284 |
#ifdef _WIN32_WCE |
| 285 |
#ifdef _UNICODE |
| 286 |
wchar_t wideBuffer[MAX_PATH]; |
| 287 |
size_t pathLen = strlen(tempPath) + 1; |
| 288 |
if (pathLen > MAX_PATH) pathLen = MAX_PATH; |
| 289 |
mbstowcs(wideBuffer, tempPath, pathLen); |
| 290 |
if (!CreateDirectory(wideBuffer, NULL)) |
| 291 |
#else |
| 292 |
if (!CreateDirectory(tempPath, NULL)) |
| 293 |
#endif // if/else _UNICODE |
| 294 |
#else |
| 295 |
if (0 != _mkdir(tempPath)) |
| 296 |
#endif // if/else _WIN32_WCE |
| 297 |
#else |
| 298 |
if (mkdir(tempPath, 0755)) |
| 299 |
#endif // if/else WIN32/UNIX |
| 300 |
{ |
| 301 |
PLOG(PL_FATAL, "ProtoFile::Rename() mkdir(%s) error: %s\n", |
| 302 |
tempPath, GetErrorString()); |
| 303 |
return false; |
| 304 |
} |
| 305 |
if (ptr) *ptr++ = PROTO_PATH_DELIMITER; |
| 306 |
} |
| 307 |
#ifdef _WIN32_WCE |
| 308 |
#ifdef _UNICODE |
| 309 |
wchar_t wideOldName[MAX_PATH]; |
| 310 |
wchar_t wideNewName[MAX_PATH]; |
| 311 |
mbstowcs(wideOldName, oldName, MAX_PATH); |
| 312 |
mbstowcs(wideNewName, newName, MAX_PATH); |
| 313 |
if (!MoveFile(wideOldName, wideNewName)) |
| 314 |
#else |
| 315 |
if (!MoveFile(oldName, newName)) |
| 316 |
#endif // if/else _UNICODE |
| 317 |
{ |
| 318 |
PLOG(PL_ERROR, "ProtoFile::Rename() MoveFile() error: %s\n", GetErrorString()); |
| 319 |
#else |
| 320 |
if (rename(oldName, newName)) |
| 321 |
{ |
| 322 |
PLOG(PL_ERROR, "ProtoFile::Rename() rename() error: %s\n", GetErrorString()); |
| 323 |
#endif // if/else _WIN32_WCE |
| 324 |
#ifdef WIN32 |
| 325 |
if (oldFlags) |
| 326 |
{ |
| 327 |
if (Open(oldName, oldFlags)) |
| 328 |
offset = 0; |
| 329 |
else |
| 330 |
PLOG(PL_ERROR, "ProtoFile::Rename() error re-opening file w/ old name\n"); |
| 331 |
} |
| 332 |
#endif // WIN32 |
| 333 |
return false; |
| 334 |
} |
| 335 |
else |
| 336 |
{ |
| 337 |
#ifdef WIN32 |
| 338 |
// (TBD) Is the file offset OK doing this??? |
| 339 |
if (oldFlags) |
| 340 |
{ |
| 341 |
if (Open(newName, oldFlags)) |
| 342 |
offset = 0; |
| 343 |
else |
| 344 |
PLOG(PL_ERROR, "ProtoFile::Rename() error opening file w/ new name\n"); |
| 345 |
} |
| 346 |
#endif // WIN32 |
| 347 |
return true; |
| 348 |
} |
| 349 |
} // end ProtoFile::Rename() |
| 350 |
|
| 351 |
bool ProtoFile::Read(char* buffer, unsigned int& numBytes) |
| 352 |
{ |
| 353 |
ASSERT(IsOpen()); |
| 354 |
while (true) |
| 355 |
{ |
| 356 |
#ifdef WIN32 |
| 357 |
#ifdef _WIN32_WCE |
| 358 |
size_t result = fread(buffer, 1, numBytes, file_ptr); |
| 359 |
#else |
| 360 |
size_t result = _read(descriptor, buffer, (unsigned int)(numBytes)); |
| 361 |
#endif // if/else _WIN32_WCE |
| 362 |
#else |
| 363 |
ssize_t result = read(descriptor, buffer, numBytes); |
| 364 |
#endif // if/else WIN32 |
| 365 |
if (result < 0) |
| 366 |
{ |
| 367 |
#ifndef _WIN32_WCE |
| 368 |
numBytes = 0; |
| 369 |
switch (errno) |
| 370 |
{ |
| 371 |
case EINTR: |
| 372 |
continue; |
| 373 |
case EAGAIN: |
| 374 |
numBytes = 0; |
| 375 |
return true; // nothing more to read for the moment |
| 376 |
default: |
| 377 |
break; |
| 378 |
} |
| 379 |
#endif // !_WIN32_WCE |
| 380 |
PLOG(PL_ERROR, "ProtoFile::Read() error: %s\n", GetErrorString()); |
| 381 |
return false; |
| 382 |
} |
| 383 |
else |
| 384 |
{ |
| 385 |
numBytes = (unsigned int)result; |
| 386 |
return true; |
| 387 |
} |
| 388 |
} // end while true |
| 389 |
} // end ProtoFile::Read() |
| 390 |
|
| 391 |
bool ProtoFile::ReadPrivate(char* buffer, unsigned int& numBytes) |
| 392 |
{ |
| 393 |
unsigned int want = numBytes; |
| 394 |
if (read_count > 0) |
| 395 |
{ |
| 396 |
unsigned int ncopy = MIN(want, read_count); |
| 397 |
memcpy(buffer, read_ptr, ncopy); |
| 398 |
read_count -= ncopy; |
| 399 |
read_ptr += ncopy; |
| 400 |
buffer +=ncopy; |
| 401 |
want -= ncopy; |
| 402 |
} |
| 403 |
while (want > 0) |
| 404 |
{ |
| 405 |
unsigned int blocksize = BUFFER_SIZE; |
| 406 |
if (!Read(read_buffer, blocksize)) |
| 407 |
{ |
| 408 |
PLOG(PL_ERROR, "ProtoFile::ReadPrivate() error: Read() failure\n"); |
| 409 |
return false; |
| 410 |
} |
| 411 |
if (blocksize > 0) |
| 412 |
{ |
| 413 |
// This check skips NULLs that have been read on some |
| 414 |
// use of trpr via tail from an NFS mounted file |
| 415 |
if (!isprint(*read_buffer) && |
| 416 |
('\t' != *read_buffer) && |
| 417 |
('\n' != *read_buffer) && |
| 418 |
('\r' != *read_buffer)) |
| 419 |
{ |
| 420 |
continue; |
| 421 |
} |
| 422 |
unsigned int ncopy = MIN(want, blocksize); |
| 423 |
memcpy(buffer, read_buffer, ncopy); |
| 424 |
read_count = blocksize - ncopy; |
| 425 |
read_ptr = read_buffer + ncopy; |
| 426 |
buffer += ncopy; |
| 427 |
want -= ncopy; |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
// Nothing more available (EOF or EAGAIN) |
| 432 |
numBytes -= want; |
| 433 |
return true; |
| 434 |
} |
| 435 |
} |
| 436 |
// Filled "buffer" with requested "numBytes" |
| 437 |
return true; |
| 438 |
} // end ProtoFile::ReadPrivate() |
| 439 |
|
| 440 |
bool ProtoFile::Readline(char* buffer, |
| 441 |
unsigned int& bufferSize) |
| 442 |
{ |
| 443 |
unsigned int count = 0; |
| 444 |
unsigned int length = bufferSize; |
| 445 |
char* ptr = buffer; |
| 446 |
while (count < length) |
| 447 |
{ |
| 448 |
unsigned int one = 1; |
| 449 |
if (ReadPrivate(ptr, one)) |
| 450 |
{ |
| 451 |
if (0 == one) |
| 452 |
{ |
| 453 |
// Must have reached EOF (treat as end of line) |
| 454 |
if (count > 0) |
| 455 |
{ |
| 456 |
*ptr = '\0'; |
| 457 |
bufferSize = count + 1; |
| 458 |
return true; |
| 459 |
} |
| 460 |
else |
| 461 |
{ |
| 462 |
bufferSize = 0; |
| 463 |
return false; |
| 464 |
} |
| 465 |
} |
| 466 |
else |
| 467 |
{ |
| 468 |
// Check to see if its end of line char |
| 469 |
if (('\n' == *ptr) || ('\r' == *ptr)) |
| 470 |
{ |
| 471 |
*ptr = '\0'; |
| 472 |
bufferSize = count; |
| 473 |
return true; |
| 474 |
} |
| 475 |
count++; |
| 476 |
ptr++; |
| 477 |
} |
| 478 |
} |
| 479 |
else |
| 480 |
{ |
| 481 |
PLOG(PL_ERROR,"ProtoFile::Readline() error: ReadPrivate() failure\n"); |
| 482 |
return false; |
| 483 |
} |
| 484 |
} |
| 485 |
// We've filled up the buffer provided with no end-of-line reached |
| 486 |
PLOG(PL_ERROR, "ProtoFile::Readline() error: line length exceeds\n", bufferSize); |
| 487 |
return false; |
| 488 |
} // end ProtoFile::Readline() |
| 489 |
|
| 490 |
size_t ProtoFile::Write(const char* buffer, size_t len) |
| 491 |
{ |
| 492 |
ASSERT(IsOpen()); |
| 493 |
size_t put = 0; |
| 494 |
while (put < len) |
| 495 |
{ |
| 496 |
#ifdef WIN32 |
| 497 |
#ifdef _WIN32_WCE |
| 498 |
size_t result = fwrite(buffer+put, 1, len-put, file_ptr); |
| 499 |
#else |
| 500 |
size_t result = _write(descriptor, buffer+put, (unsigned int)(len-put)); |
| 501 |
#endif // if/else _WIN32_WCE |
| 502 |
#else |
| 503 |
size_t result = write(descriptor, buffer+put, len-put); |
| 504 |
#endif // if/else WIN32 |
| 505 |
if (result <= 0) |
| 506 |
{ |
| 507 |
#ifndef _WIN32_WCE |
| 508 |
if (EINTR != errno) |
| 509 |
#endif // !_WIN32_WCE |
| 510 |
{ |
| 511 |
PLOG(PL_FATAL, "ProtoFile::Write() write(%d) result:%d error: %s\n", len, result, GetErrorString()); |
| 512 |
return 0; |
| 513 |
} |
| 514 |
} |
| 515 |
else |
| 516 |
{ |
| 517 |
offset += (Offset)result; |
| 518 |
put += result; |
| 519 |
} |
| 520 |
} // end while (put < len) |
| 521 |
return put; |
| 522 |
} // end ProtoFile::Write() |
| 523 |
|
| 524 |
bool ProtoFile::Seek(Offset theOffset) |
| 525 |
{ |
| 526 |
ASSERT(IsOpen()); |
| 527 |
#ifdef WIN32 |
| 528 |
#ifdef _WIN32_WCE |
| 529 |
// (TBD) properly support big files on WinCE |
| 530 |
Offset result = fseek(file_ptr, (long)theOffset, SEEK_SET); |
| 531 |
#else |
| 532 |
Offset result = _lseeki64(descriptor, theOffset, SEEK_SET); |
| 533 |
#endif // if/else _WIN32_WCE |
| 534 |
#else |
| 535 |
Offset result = lseek(descriptor, theOffset, SEEK_SET); |
| 536 |
#endif // if/else WIN32 |
| 537 |
if (result == (Offset)-1) |
| 538 |
{ |
| 539 |
PLOG(PL_FATAL, "ProtoFile::Seek() lseek() error: %s\n", GetErrorString()); |
| 540 |
return false; |
| 541 |
} |
| 542 |
else |
| 543 |
{ |
| 544 |
offset = result; |
| 545 |
return true; // no error |
| 546 |
} |
| 547 |
} // end ProtoFile::Seek() |
| 548 |
|
| 549 |
bool ProtoFile::Pad(Offset theOffset) |
| 550 |
{ |
| 551 |
if (theOffset > GetSize()) |
| 552 |
{ |
| 553 |
if (Seek(theOffset - 1)) |
| 554 |
{ |
| 555 |
char byte = 0; |
| 556 |
if (1 != Write(&byte, 1)) |
| 557 |
{ |
| 558 |
PLOG(PL_FATAL, "ProtoFile::Pad() write error: %s\n", GetErrorString()); |
| 559 |
return false; |
| 560 |
} |
| 561 |
} |
| 562 |
else |
| 563 |
{ |
| 564 |
PLOG(PL_FATAL, "ProtoFile::Pad() seek error: %s\n", GetErrorString()); |
| 565 |
return false; |
| 566 |
} |
| 567 |
} |
| 568 |
return true; |
| 569 |
} // end ProtoFile::Pad() |
| 570 |
|
| 571 |
ProtoFile::Offset ProtoFile::GetSize() const |
| 572 |
{ |
| 573 |
ASSERT(IsOpen()); |
| 574 |
#ifdef _WIN32_WCE |
| 575 |
DWORD fileSize = GetFileSize(_fileno(file_ptr), NULL); |
| 576 |
return ((Offset)fileSize); |
| 577 |
#else |
| 578 |
#ifdef WIN32 |
| 579 |
struct _stati64 info; // instead of "struct _stat" |
| 580 |
int result = _fstati64(descriptor, &info); // instead of "_fstat()" |
| 581 |
#else |
| 582 |
struct stat info; |
| 583 |
int result = fstat(descriptor, &info); |
| 584 |
#endif // if/else WIN32 |
| 585 |
if (result) |
| 586 |
{ |
| 587 |
PLOG(PL_FATAL, "Error getting file size: %s\n", GetErrorString()); |
| 588 |
return 0; |
| 589 |
} |
| 590 |
else |
| 591 |
{ |
| 592 |
return info.st_size; |
| 593 |
} |
| 594 |
#endif // if/else _WIN32_WCE |
| 595 |
} // end ProtoFile::GetSize() |
| 596 |
|
| 597 |
|
| 598 |
/*********************************************** |
| 599 |
* The ProtoFile::DirectoryIterator classes are used to |
| 600 |
* walk directory trees |
| 601 |
*/ |
| 602 |
|
| 603 |
ProtoFile::DirectoryIterator::DirectoryIterator() |
| 604 |
: current(NULL) |
| 605 |
{ |
| 606 |
} |
| 607 |
|
| 608 |
ProtoFile::DirectoryIterator::~DirectoryIterator() |
| 609 |
{ |
| 610 |
Close(); |
| 611 |
} |
| 612 |
|
| 613 |
bool ProtoFile::DirectoryIterator::Open(const char *thePath) |
| 614 |
{ |
| 615 |
if (NULL != current) Close(); |
| 616 |
if (NULL == thePath) |
| 617 |
{ |
| 618 |
PLOG(PL_ERROR, "ProtoFile::DirectoryIterator::Open() error: NULL path?!\n"); |
| 619 |
return false; |
| 620 |
} |
| 621 |
|
| 622 |
if (NORMAL == GetType(thePath)) |
| 623 |
{ |
| 624 |
// This is a non-directory file path so create a "Directory" item |
| 625 |
// to cache the path |
| 626 |
if (NULL == (current = new Directory(thePath))) |
| 627 |
{ |
| 628 |
PLOG(PL_ERROR, "ProtoFile::DirectoryIterator::Open() new Directory error: %s\n", GetErrorString()); |
| 629 |
return false; |
| 630 |
} |
| 631 |
#ifdef WIN32 |
| 632 |
current->hSearch = INVALID_HANDLE_VALUE; |
| 633 |
#else |
| 634 |
current->dptr = NULL; |
| 635 |
#endif // if/else WIN32 |
| 636 |
path_len = (int)strlen(current->Path()); |
| 637 |
path_len = MIN(PATH_MAX, path_len); |
| 638 |
return true; |
| 639 |
} |
| 640 |
#ifdef WIN32 |
| 641 |
#ifdef _WIN32_WCE |
| 642 |
if (!ProtoFile::Exists(thePath)) |
| 643 |
#else |
| 644 |
if (_access(thePath, 0)) |
| 645 |
#endif // if/else _WIN32_WCE |
| 646 |
#else |
| 647 |
if (access(thePath, X_OK)) |
| 648 |
#endif // if/else WIN32 |
| 649 |
{ |
| 650 |
PLOG(PL_ERROR, "ProtoFile::DirectoryIterator::Open() error: can't access directory: %s\n", thePath); |
| 651 |
return false; |
| 652 |
} |
| 653 |
current = new Directory(thePath); |
| 654 |
if ((NULL != current) && current->Open()) |
| 655 |
{ |
| 656 |
path_len = (int)strlen(current->Path()); |
| 657 |
path_len = MIN(PATH_MAX, path_len); |
| 658 |
return true; |
| 659 |
} |
| 660 |
else |
| 661 |
{ |
| 662 |
PLOG(PL_ERROR, "ProtoFile::DirectoryIterator::Open() error: can't open directory: %s\n", thePath); |
| 663 |
if (current) delete current; |
| 664 |
current = NULL; |
| 665 |
return false; |
| 666 |
} |
| 667 |
} // end ProtoFile::DirectoryIterator::Open() |
| 668 |
|
| 669 |
void ProtoFile::DirectoryIterator::Close() |
| 670 |
{ |
| 671 |
Directory* d; |
| 672 |
while (NULL != (d = current)) |
| 673 |
{ |
| 674 |
current = d->parent; |
| 675 |
d->Close(); |
| 676 |
delete d; |
| 677 |
} |
| 678 |
} // end ProtoFile::DirectoryIterator::Close() |
| 679 |
|
| 680 |
bool ProtoFile::DirectoryIterator::GetPath(char* pathBuffer) const |
| 681 |
{ |
| 682 |
if (NULL != current) |
| 683 |
{ |
| 684 |
Directory* d = current; |
| 685 |
while (NULL != d->parent) d = d->parent; |
| 686 |
strncpy(pathBuffer, d->Path(), PATH_MAX); |
| 687 |
return true; |
| 688 |
} |
| 689 |
else |
| 690 |
{ |
| 691 |
pathBuffer[0] = '\0'; |
| 692 |
return false; |
| 693 |
} |
| 694 |
} // end DirectoryIterator::GetPath() |
| 695 |
|
| 696 |
#ifdef WIN32 |
| 697 |
bool ProtoFile::DirectoryIterator::GetNextPath(char* fileName, bool includeFiles, bool includeDirs) |
| 698 |
{ |
| 699 |
if (!current) return false; |
| 700 |
if (INVALID_HANDLE_VALUE == current->hSearch) |
| 701 |
{ |
| 702 |
bool result = includeFiles; |
| 703 |
if (result) |
| 704 |
{ |
| 705 |
strncpy(fileName, current->path, PATH_MAX); |
| 706 |
size_t len = strlen(fileName); |
| 707 |
if (PROTO_PATH_DELIMITER == fileName[len - 1]) |
| 708 |
fileName[len - 1] = '\0'; |
| 709 |
} |
| 710 |
current = NULL; |
| 711 |
return result; |
| 712 |
} |
| 713 |
|
| 714 |
bool success = true; |
| 715 |
while(success) |
| 716 |
{ |
| 717 |
WIN32_FIND_DATA findData; |
| 718 |
if (current->hSearch == INVALID_HANDLE_VALUE) |
| 719 |
{ |
| 720 |
// Construct search string |
| 721 |
current->GetFullName(fileName); |
| 722 |
strcat(fileName, "\\*"); |
| 723 |
#ifdef _UNICODE |
| 724 |
wchar_t wideBuffer[MAX_PATH]; |
| 725 |
mbstowcs(wideBuffer, fileName, MAX_PATH); |
| 726 |
if (INVALID_HANDLE_VALUE == |
| 727 |
(current->hSearch = FindFirstFile(wideBuffer, &findData))) |
| 728 |
#else |
| 729 |
if (INVALID_HANDLE_VALUE == |
| 730 |
(current->hSearch = FindFirstFile(fileName, &findData))) |
| 731 |
#endif // if/else _UNICODE |
| 732 |
success = false; |
| 733 |
else |
| 734 |
success = true; |
| 735 |
} |
| 736 |
else |
| 737 |
{ |
| 738 |
success = (0 != FindNextFile(current->hSearch, &findData)); |
| 739 |
} |
| 740 |
|
| 741 |
// Do we have a candidate file? |
| 742 |
if (success) |
| 743 |
{ |
| 744 |
#ifdef _UNICODE |
| 745 |
char cFileName[MAX_PATH]; |
| 746 |
wcstombs(cFileName, findData.cFileName, MAX_PATH); |
| 747 |
char* ptr = strrchr(cFileName, PROTO_PATH_DELIMITER); |
| 748 |
#else |
| 749 |
char* ptr = strrchr(findData.cFileName, PROTO_PATH_DELIMITER); |
| 750 |
#endif // if/else _UNICODE |
| 751 |
if (ptr) |
| 752 |
ptr += 1; |
| 753 |
else |
| 754 |
#ifdef _UNICODE |
| 755 |
ptr = cFileName; |
| 756 |
#else |
| 757 |
ptr = findData.cFileName; |
| 758 |
#endif // if/else _UNICODE |
| 759 |
|
| 760 |
// Skip "." and ".." directories |
| 761 |
if (ptr[0] == '.') |
| 762 |
{ |
| 763 |
if ((1 == strlen(ptr)) || |
| 764 |
((ptr[1] == '.') && (2 == strlen(ptr)))) |
| 765 |
{ |
| 766 |
continue; |
| 767 |
} |
| 768 |
} |
| 769 |
current->GetFullName(fileName); |
| 770 |
strcat(fileName, ptr); |
| 771 |
ProtoFile::Type type = ProtoFile::GetType(fileName); |
| 772 |
if (includeFiles && (ProtoFile::NORMAL == type)) |
| 773 |
{ |
| 774 |
// I don't think this commented code is needed anymore |
| 775 |
//size_t nameLen = strlen(fileName); |
| 776 |
//nameLen = MIN(PATH_MAX, nameLen); |
| 777 |
//nameLen -= path_len; |
| 778 |
//memmove(fileName, fileName+path_len, nameLen); |
| 779 |
//if (nameLen < PATH_MAX) fileName[nameLen] = '\0'; |
| 780 |
return true; |
| 781 |
} |
| 782 |
else if (ProtoFile::DIRECTORY == type) |
| 783 |
{ |
| 784 |
Directory *dir = new Directory(ptr, current); |
| 785 |
if ((NULL != dir) && dir->Open()) |
| 786 |
{ |
| 787 |
// Push sub-directory onto stack and search it |
| 788 |
current = dir; |
| 789 |
if (includeDirs) |
| 790 |
{ |
| 791 |
// TBD - should we remove trailing PATH_DELIMITER? |
| 792 |
current->GetFullName(fileName); |
| 793 |
return true; |
| 794 |
} |
| 795 |
return GetNextPath(fileName, includeFiles, includeDirs); |
| 796 |
} |
| 797 |
else |
| 798 |
{ |
| 799 |
// Couldn't open try next one |
| 800 |
if (NULL != dir) |
| 801 |
{ |
| 802 |
// if "includeDirs", return path, even if we can't open it |
| 803 |
if (includeDirs) |
| 804 |
dir->GetFullName(fileName); |
| 805 |
delete dir; |
| 806 |
if (includeDirs) |
| 807 |
return true; |
| 808 |
} |
| 809 |
} |
| 810 |
} |
| 811 |
else |
| 812 |
{ |
| 813 |
// ProtoFile::INVALID file, try next one |
| 814 |
} |
| 815 |
} // end if(success) |
| 816 |
} // end while(success) |
| 817 |
|
| 818 |
// if parent, popup a level and continue search |
| 819 |
if (current->parent) |
| 820 |
{ |
| 821 |
current->Close(); |
| 822 |
Directory *dir = current; |
| 823 |
current = current->parent; |
| 824 |
delete dir; |
| 825 |
return GetNextPath(fileName, includeFiles, includeDirs); |
| 826 |
} |
| 827 |
else |
| 828 |
{ |
| 829 |
current->Close(); |
| 830 |
delete current; |
| 831 |
current = NULL; |
| 832 |
return false; |
| 833 |
} |
| 834 |
} // end ProtoFile::DirectoryIterator::GetNextPath() (WIN32) |
| 835 |
#else |
| 836 |
bool ProtoFile::DirectoryIterator::GetNextPath(char* fileName, bool includeFiles, bool includeDirs) |
| 837 |
{ |
| 838 |
//TRACE("enter ProtoFile::DirectoryIterator::GetNextPath() current:%p files:%d dirs:%d ...\n", current, includeFiles, includeDirs); |
| 839 |
if (NULL == current) return false; |
| 840 |
if (NULL == current->dptr) |
| 841 |
{ |
| 842 |
bool result = includeFiles; |
| 843 |
if (result) |
| 844 |
{ |
| 845 |
strncpy(fileName, current->path, PATH_MAX); |
| 846 |
size_t len = strlen(fileName); |
| 847 |
if (PROTO_PATH_DELIMITER == fileName[len-1]) |
| 848 |
fileName[len-1] = '\0'; |
| 849 |
} |
| 850 |
current = NULL; |
| 851 |
return result; |
| 852 |
} |
| 853 |
struct dirent *dp; |
| 854 |
while (NULL != (dp = readdir(current->dptr))) |
| 855 |
{ |
| 856 |
// Make sure it's not "." or ".." |
| 857 |
if (dp->d_name[0] == '.') |
| 858 |
{ |
| 859 |
if ((1 == strlen(dp->d_name)) || |
| 860 |
((dp->d_name[1] == '.' ) && (2 == strlen(dp->d_name)))) |
| 861 |
{ |
| 862 |
continue; // skip "." and ".." directory names |
| 863 |
} |
| 864 |
} |
| 865 |
current->GetFullName(fileName); |
| 866 |
strcat(fileName, dp->d_name); |
| 867 |
//TRACE("Getting type for %s ... ", fileName); |
| 868 |
ProtoFile::Type type = ProtoFile::GetType(fileName); |
| 869 |
if (includeFiles && (ProtoFile::NORMAL == type)) |
| 870 |
{ |
| 871 |
//TRACE("NORMAL type\n"); |
| 872 |
return true; |
| 873 |
} |
| 874 |
else if (ProtoFile::DIRECTORY == type) |
| 875 |
{ |
| 876 |
//TRACE("DIRECTORY type\n"); |
| 877 |
Directory *dir = new Directory(dp->d_name, current); |
| 878 |
if ((NULL != dir) && dir->Open()) |
| 879 |
{ |
| 880 |
// Push sub-directory onto stack and search it |
| 881 |
current = dir; |
| 882 |
if (includeDirs) |
| 883 |
{ |
| 884 |
current->GetFullName(fileName); |
| 885 |
return true; |
| 886 |
} |
| 887 |
return GetNextPath(fileName, includeFiles, includeDirs); |
| 888 |
} |
| 889 |
else |
| 890 |
{ |
| 891 |
// Couldn't open try next one |
| 892 |
if (NULL != dir) |
| 893 |
{ |
| 894 |
if (includeDirs) |
| 895 |
dir->GetFullName(fileName); |
| 896 |
delete dir; |
| 897 |
if (includeDirs) |
| 898 |
return true; |
| 899 |
} |
| 900 |
} |
| 901 |
} |
| 902 |
else |
| 903 |
{ |
| 904 |
//TRACE("INVALID type\n"); |
| 905 |
// ProtoFile::INVALID, try next one |
| 906 |
} |
| 907 |
} // end while(readdir()) |
| 908 |
|
| 909 |
// Pop up a level and recursively continue or finish if done |
| 910 |
if (current->parent) |
| 911 |
{ |
| 912 |
char path[PATH_MAX+1]; |
| 913 |
path[PATH_MAX] = '\0'; |
| 914 |
current->parent->GetFullName(path); |
| 915 |
current->Close(); |
| 916 |
Directory *dir = current; |
| 917 |
current = current->parent; |
| 918 |
delete dir; |
| 919 |
return GetNextPath(fileName, includeFiles, includeDirs); |
| 920 |
} |
| 921 |
else |
| 922 |
{ |
| 923 |
current->Close(); |
| 924 |
delete current; |
| 925 |
current = NULL; |
| 926 |
return false; // no more files remain |
| 927 |
} |
| 928 |
} // end ProtoFile::DirectoryIterator::GetNextPath() (UNIX) |
| 929 |
|
| 930 |
#endif // if/else WIN32 |
| 931 |
|
| 932 |
ProtoFile::Directory::Directory(const char* thePath, |
| 933 |
Directory* theParent) |
| 934 |
: parent(theParent), |
| 935 |
#ifdef WIN32 |
| 936 |
hSearch(INVALID_HANDLE_VALUE) |
| 937 |
#else |
| 938 |
dptr(NULL) |
| 939 |
#endif // if/else WIN32 |
| 940 |
{ |
| 941 |
strncpy(path, thePath, PATH_MAX); |
| 942 |
path[PATH_MAX] = '\0'; |
| 943 |
size_t len = MIN(PATH_MAX, strlen(path)); |
| 944 |
if ((len < PATH_MAX) && (PROTO_PATH_DELIMITER != path[len-1])) |
| 945 |
{ |
| 946 |
path[len++] = PROTO_PATH_DELIMITER; |
| 947 |
if (len < PATH_MAX) path[len] = '\0'; |
| 948 |
} |
| 949 |
} |
| 950 |
|
| 951 |
ProtoFile::Directory::~Directory() |
| 952 |
{ |
| 953 |
Close(); |
| 954 |
} |
| 955 |
|
| 956 |
bool ProtoFile::Directory::Open() |
| 957 |
{ |
| 958 |
Close(); // in case it's already open |
| 959 |
char fullName[PATH_MAX+1]; |
| 960 |
fullName[PATH_MAX] = '\0'; |
| 961 |
GetFullName(fullName); |
| 962 |
// Get rid of trailing PROTO_PATH_DELIMITER |
| 963 |
size_t len = MIN(PATH_MAX, strlen(fullName)); |
| 964 |
if (PROTO_PATH_DELIMITER == fullName[len-1]) fullName[len-1] = '\0'; |
| 965 |
#ifdef WIN32 |
| 966 |
#ifdef _UNICODE |
| 967 |
wchar_t wideBuffer[MAX_PATH]; |
| 968 |
mbstowcs(wideBuffer, fullName, MAX_PATH); |
| 969 |
DWORD attr = GetFileAttributes(wideBuffer); |
| 970 |
#else |
| 971 |
DWORD attr = GetFileAttributes(fullName); |
| 972 |
#endif // if/else _UNICODE |
| 973 |
if (0xFFFFFFFF == attr) |
| 974 |
{ |
| 975 |
PLOG(PL_ERROR, "ProtoFile::Directory::Open(%s) error: %s\n", fullName, GetErrorString()); |
| 976 |
return false; |
| 977 |
} |
| 978 |
else if (0 != (attr & FILE_ATTRIBUTE_DIRECTORY)) |
| 979 |
{ |
| 980 |
return true; |
| 981 |
} |
| 982 |
else |
| 983 |
{ |
| 984 |
PLOG(PL_ERROR, "ProtoFile::Directory::Open(%s) error: not a directory\n", fullName); |
| 985 |
return false; |
| 986 |
} |
| 987 |
#else |
| 988 |
if (NULL != (dptr = opendir(fullName))) |
| 989 |
{ |
| 990 |
return true; |
| 991 |
} |
| 992 |
else |
| 993 |
{ |
| 994 |
PLOG(PL_ERROR, "ProtoFile::Directory::Open(%s) error: %s\n", fullName, GetErrorString()); |
| 995 |
return false; |
| 996 |
} |
| 997 |
#endif // if/else WIN32 |
| 998 |
|
| 999 |
} // end ProtoFile::Directory::Open() |
| 1000 |
|
| 1001 |
void ProtoFile::Directory::Close() |
| 1002 |
{ |
| 1003 |
#ifdef WIN32 |
| 1004 |
if (hSearch != INVALID_HANDLE_VALUE) |
| 1005 |
{ |
| 1006 |
FindClose(hSearch); |
| 1007 |
hSearch = INVALID_HANDLE_VALUE; |
| 1008 |
} |
| 1009 |
#else |
| 1010 |
if (NULL != dptr) |
| 1011 |
{ |
| 1012 |
closedir(dptr); |
| 1013 |
dptr = NULL; |
| 1014 |
} |
| 1015 |
#endif // if/else WIN32 |
| 1016 |
} // end ProtoFile::Directory::Close() |
| 1017 |
|
| 1018 |
void ProtoFile::Directory::GetFullName(char* ptr) |
| 1019 |
{ |
| 1020 |
ptr[0] = '\0'; |
| 1021 |
RecursiveCatName(ptr); |
| 1022 |
} // end ProtoFile::Directory::GetFullName() |
| 1023 |
|
| 1024 |
void ProtoFile::Directory::RecursiveCatName(char* ptr) |
| 1025 |
{ |
| 1026 |
if (NULL != parent) parent->RecursiveCatName(ptr); |
| 1027 |
size_t len = MIN(PATH_MAX, strlen(ptr)); |
| 1028 |
strncat(ptr, path, PATH_MAX-len); |
| 1029 |
} // end ProtoFile::Directory::RecursiveCatName() |
| 1030 |
|
| 1031 |
// Below are some static routines for getting file/directory information |
| 1032 |
|
| 1033 |
// Is the named item a valid directory or file (or neither)?? |
| 1034 |
ProtoFile::Type ProtoFile::GetType(const char* path) |
| 1035 |
{ |
| 1036 |
#ifdef WIN32 |
| 1037 |
#ifdef _UNICODE |
| 1038 |
wchar_t wideBuffer[MAX_PATH]; |
| 1039 |
mbstowcs(wideBuffer, path, MAX_PATH); |
| 1040 |
DWORD attr = GetFileAttributes(wideBuffer); |
| 1041 |
#else |
| 1042 |
DWORD attr = GetFileAttributes(path); |
| 1043 |
#endif // if/else _UNICODE |
| 1044 |
if (0xFFFFFFFF == attr) |
| 1045 |
return INVALID; // error |
| 1046 |
else if (attr & FILE_ATTRIBUTE_DIRECTORY) |
| 1047 |
return DIRECTORY; |
| 1048 |
else |
| 1049 |
return NORMAL; |
| 1050 |
#else |
| 1051 |
struct stat file_info; |
| 1052 |
if (stat(path, &file_info)) |
| 1053 |
return INVALID; // stat() error |
| 1054 |
else if ((S_ISDIR(file_info.st_mode))) |
| 1055 |
return DIRECTORY; |
| 1056 |
else |
| 1057 |
return NORMAL; |
| 1058 |
#endif // if/else WIN32 |
| 1059 |
} // end ProtoFile::GetType() |
| 1060 |
|
| 1061 |
ProtoFile::Offset ProtoFile::GetSize(const char* path) |
| 1062 |
{ |
| 1063 |
#ifdef _WIN32_WCE |
| 1064 |
WIN32_FIND_DATA findData; |
| 1065 |
#ifdef _UNICODE |
| 1066 |
wchar_t wideBuffer[MAX_PATH]; |
| 1067 |
mbstowcs(wideBuffer, path, MAX_PATH); |
| 1068 |
if (INVALID_HANDLE_VALUE != FindFirstFile(wideBuffer, &findData)) |
| 1069 |
#else |
| 1070 |
if (INVALID_HANDLE_VALUE != FindFirstFile(path, &findData)) |
| 1071 |
#endif // if/else _UNICODE |
| 1072 |
{ |
| 1073 |
Offset fileSize = findData.nFileSizeLow; |
| 1074 |
if (sizeof(Offset) > 4) |
| 1075 |
fileSize |= ((Offset)findData.nFileSizeHigh) << (8*sizeof(DWORD)); |
| 1076 |
return fileSize; |
| 1077 |
} |
| 1078 |
else |
| 1079 |
{ |
| 1080 |
PLOG(PL_ERROR, "Error getting file size: %s\n", GetErrorString()); |
| 1081 |
return 0; |
| 1082 |
} |
| 1083 |
#else |
| 1084 |
#ifdef WIN32 |
| 1085 |
struct _stati64 info; // instead of "struct _stat" |
| 1086 |
int result = _stati64(path, &info); // instead of "_stat()" |
| 1087 |
#else |
| 1088 |
struct stat info; |
| 1089 |
int result = stat(path, &info); |
| 1090 |
#endif // if/else WIN32 |
| 1091 |
if (result) |
| 1092 |
{ |
| 1093 |
//DMSG(0, "Error getting file size: %s\n", GetErrorString()); |
| 1094 |
return 0; |
| 1095 |
} |
| 1096 |
else |
| 1097 |
{ |
| 1098 |
return info.st_size; |
| 1099 |
} |
| 1100 |
#endif // if/else _WIN32_WCE |
| 1101 |
} // end ProtoFile::GetSize() |
| 1102 |
|
| 1103 |
time_t ProtoFile::GetUpdateTime(const char* path) |
| 1104 |
{ |
| 1105 |
#ifdef _WIN32_WCE |
| 1106 |
WIN32_FIND_DATA findData; |
| 1107 |
#ifdef _UNICODE |
| 1108 |
wchar_t wideBuffer[MAX_PATH]; |
| 1109 |
mbstowcs(wideBuffer, path, MAX_PATH); |
| 1110 |
if (INVALID_HANDLE_VALUE != FindFirstFile(wideBuffer, &findData)) |
| 1111 |
#else |
| 1112 |
if (INVALID_HANDLE_VALUE != FindFirstFile(path, &findData)) |
| 1113 |
#endif // if/else _UNICODE |
| 1114 |
{ |
| 1115 |
ULARGE_INTEGER ctime = {findData.ftCreationTime.dwLowDateTime, |
| 1116 |
findData.ftCreationTime.dwHighDateTime}; |
| 1117 |
ULARGE_INTEGER atime = {findData.ftLastAccessTime.dwLowDateTime, |
| 1118 |
findData.ftLastAccessTime.dwHighDateTime}; |
| 1119 |
ULARGE_INTEGER mtime = {findData.ftLastWriteTime.dwLowDateTime, |
| 1120 |
findData.ftLastWriteTime.dwHighDateTime}; |
| 1121 |
if (ctime.QuadPart < atime.QuadPart) ctime = atime; |
| 1122 |
if (ctime.QuadPart < mtime.QuadPart) ctime = mtime; |
| 1123 |
const ULARGE_INTEGER epochTime = {0xD53E8000, 0x019DB1DE}; |
| 1124 |
ctime.QuadPart -= epochTime.QuadPart; |
| 1125 |
// Convert sytem time to seconds |
| 1126 |
ctime.QuadPart /= 10000000; |
| 1127 |
return ctime.LowPart; |
| 1128 |
} |
| 1129 |
else |
| 1130 |
{ |
| 1131 |
PLOG(PL_ERROR, "Error getting file size: %s\n", GetErrorString()); |
| 1132 |
return 0; |
| 1133 |
} |
| 1134 |
#else |
| 1135 |
#ifdef WIN32 |
| 1136 |
struct _stati64 info; // instead of "struct _stat" |
| 1137 |
int result = _stati64(path, &info); // instead of "_stat()" |
| 1138 |
#else |
| 1139 |
struct stat info; |
| 1140 |
int result = stat(path, &info); |
| 1141 |
#endif // if/else WIN32 |
| 1142 |
if (result) |
| 1143 |
{ |
| 1144 |
return (time_t)0; // stat() error |
| 1145 |
} |
| 1146 |
else |
| 1147 |
{ |
| 1148 |
#ifdef WIN32 |
| 1149 |
// Hack because Win2K and Win98 seem to work differently |
| 1150 |
//__time64_t updateTime = MAX(info.st_ctime, info.st_atime); |
| 1151 |
time_t updateTime = MAX(info.st_ctime, info.st_atime); |
| 1152 |
updateTime = MAX(updateTime, info.st_mtime); |
| 1153 |
return ((time_t)updateTime); |
| 1154 |
#else |
| 1155 |
return info.st_ctime; |
| 1156 |
#endif // if/else WIN32 |
| 1157 |
} |
| 1158 |
#endif // if/else _WIN32_WCE |
| 1159 |
} // end ProtoFile::GetUpdateTime() |
| 1160 |
|
| 1161 |
bool ProtoFile::IsLocked(const char* path) |
| 1162 |
{ |
| 1163 |
// If file doesn't exist, it's not locked |
| 1164 |
if (!Exists(path)) return false; |
| 1165 |
ProtoFile testFile; |
| 1166 |
#ifdef WIN32 |
| 1167 |
if(!testFile.Open(path, O_WRONLY | O_CREAT)) |
| 1168 |
#else |
| 1169 |
if(!testFile.Open(path, O_WRONLY | O_CREAT)) |
| 1170 |
#endif // if/else WIN32 |
| 1171 |
{ |
| 1172 |
return true; |
| 1173 |
} |
| 1174 |
else if (testFile.Lock()) |
| 1175 |
{ |
| 1176 |
// We were able to lock the file successfully |
| 1177 |
testFile.Unlock(); |
| 1178 |
testFile.Close(); |
| 1179 |
return false; |
| 1180 |
} |
| 1181 |
else |
| 1182 |
{ |
| 1183 |
testFile.Close(); |
| 1184 |
return true; |
| 1185 |
} |
| 1186 |
} // end ProtoFile::IsLocked() |
| 1187 |
|
| 1188 |
bool ProtoFile::Unlink(const char* path) |
| 1189 |
{ |
| 1190 |
// Don't unlink a file that is open (locked) |
| 1191 |
if (ProtoFile::IsLocked(path)) |
| 1192 |
{ |
| 1193 |
return false; |
| 1194 |
} |
| 1195 |
|
| 1196 |
#ifdef _WIN32_WCE |
| 1197 |
#ifdef _UNICODE |
| 1198 |
wchar_t wideBuffer[MAX_PATH]; |
| 1199 |
mbstowcs(wideBuffer, path, MAX_PATH); |
| 1200 |
if (0 == DeleteFile(wideBuffer)) |
| 1201 |
#else |
| 1202 |
if (0 == DeleteFile(path)) |
| 1203 |
#endif // if/else _UNICODE |
| 1204 |
{ |
| 1205 |
PLOG(PL_FATAL, "ProtoFile::Unlink() DeletFile() error: %s\n", GetErrorString()); |
| 1206 |
return false; |
| 1207 |
} |
| 1208 |
#else |
| 1209 |
#ifdef WIN32 |
| 1210 |
if (_unlink(path)) |
| 1211 |
#else |
| 1212 |
if (unlink(path)) |
| 1213 |
#endif // if/else WIN32 |
| 1214 |
{ |
| 1215 |
PLOG(PL_FATAL, "ProtoFile::Unlink() unlink error: %s\n", GetErrorString()); |
| 1216 |
return false; |
| 1217 |
} |
| 1218 |
#endif // if/else _WIN32_WCE |
| 1219 |
else |
| 1220 |
{ |
| 1221 |
return true; |
| 1222 |
} |
| 1223 |
|
| 1224 |
} // end ProtoFile::Unlink() |
| 1225 |
|
| 1226 |
|
| 1227 |
bool ProtoFile::PathList::AppendPath(const char* thePath) |
| 1228 |
{ |
| 1229 |
Path* pathItem = new Path(thePath); |
| 1230 |
if (NULL == pathItem) |
| 1231 |
{ |
| 1232 |
PLOG(PL_ERROR, "ProtoFile::PathList::Append() new Path error: %s\n", GetErrorString()); |
| 1233 |
return false; |
| 1234 |
} |
| 1235 |
Append(*pathItem); |
| 1236 |
return true; |
| 1237 |
} // end ProtoFile::PathList::AppendPath() |
| 1238 |
|
| 1239 |
ProtoFile::PathList::PathIterator::PathIterator(PathList& pathList, bool updatesOnly, time_t initTime) |
| 1240 |
: list_iterator(pathList) |
| 1241 |
{ |
| 1242 |
Init(updatesOnly, initTime); |
| 1243 |
} |
| 1244 |
|
| 1245 |
ProtoFile::PathList::PathIterator::~PathIterator() |
| 1246 |
{ |
| 1247 |
dir_iterator.Close(); |
| 1248 |
} |
| 1249 |
|
| 1250 |
bool ProtoFile::PathList::PathIterator::GetNextPath(char* path, bool includeFiles, bool includeDirs) |
| 1251 |
{ |
| 1252 |
while (true) |
| 1253 |
{ |
| 1254 |
// First, get any files from pending directory iterator |
| 1255 |
while (dir_iterator.GetNextPath(path, includeFiles, includeDirs)) |
| 1256 |
{ |
| 1257 |
if (updates_only) |
| 1258 |
{ |
| 1259 |
time_t updateTime = ProtoFile::GetUpdateTime(path); |
| 1260 |
//TRACE("path:%s updateTime:%ld last_time:%ld big_time:%ld\n", path, updateTime, last_time, big_time); |
| 1261 |
if (difftime(updateTime, big_time) > 0.0) big_time = updateTime; |
| 1262 |
if (difftime(updateTime, last_time) <= 0.0) |
| 1263 |
continue; // this file has not been updated |
| 1264 |
} |
| 1265 |
return true; |
| 1266 |
} |
| 1267 |
dir_iterator.Close(); |
| 1268 |
Path* nextPath = list_iterator.GetNextItem(); |
| 1269 |
if (NULL == nextPath) |
| 1270 |
{ |
| 1271 |
break; // reached end of list |
| 1272 |
} |
| 1273 |
bool validPath = false; |
| 1274 |
switch (ProtoFile::GetType(nextPath->GetPath())) |
| 1275 |
{ |
| 1276 |
case DIRECTORY: |
| 1277 |
if (!dir_iterator.Open(nextPath->GetPath())) |
| 1278 |
PLOG(PL_ERROR, "ProtoFile::PathList::PathIterator::GetNextPath() error: unable to open directory: %s\n", |
| 1279 |
nextPath->GetPath()); |
| 1280 |
if (includeDirs) |
| 1281 |
validPath = true; // even return paths for directories we can't open |
| 1282 |
break; |
| 1283 |
case NORMAL: |
| 1284 |
if (includeFiles) |
| 1285 |
validPath = true; |
| 1286 |
break; |
| 1287 |
default: |
| 1288 |
break; |
| 1289 |
} |
| 1290 |
if (validPath) |
| 1291 |
{ |
| 1292 |
if (updates_only) |
| 1293 |
{ |
| 1294 |
time_t updateTime = ProtoFile::GetUpdateTime(nextPath->GetPath()); |
| 1295 |
//TRACE("path:%s updateTime:%ld last_time:%ld big_time:%ld\n", path, updateTime, last_time, big_time); |
| 1296 |
if (difftime(updateTime, big_time) > 0.0) big_time = updateTime; |
| 1297 |
if (difftime(updateTime, last_time) <= 0.0) |
| 1298 |
continue; // this file has not been updated |
| 1299 |
} |
| 1300 |
strncpy(path, nextPath->GetPath(), PATH_MAX); |
| 1301 |
return true; |
| 1302 |
} |
| 1303 |
} |
| 1304 |
return false; |
| 1305 |
} // end ProtoFile::PathList::PathIterator::GetNextPath() |
| 1306 |
|