* xmlFdOpen: * @filename: the URI for matching * @out: pointer to resulting context * * Returns an xmlParserErrors code */
| 507 | * Returns an xmlParserErrors code |
| 508 | */ |
| 509 | static int |
| 510 | xmlFdOpen(const char *filename, int write, int *out) { |
| 511 | char *fromUri = NULL; |
| 512 | int flags; |
| 513 | int fd; |
| 514 | int ret; |
| 515 | |
| 516 | *out = -1; |
| 517 | if (filename == NULL) |
| 518 | return(XML_ERR_ARGUMENT); |
| 519 | |
| 520 | if (xmlConvertUriToPath(filename, &fromUri) < 0) |
| 521 | return(XML_ERR_NO_MEMORY); |
| 522 | |
| 523 | if (fromUri != NULL) |
| 524 | filename = fromUri; |
| 525 | |
| 526 | #if defined(_WIN32) |
| 527 | { |
| 528 | wchar_t *wpath; |
| 529 | |
| 530 | wpath = __xmlIOWin32UTF8ToWChar(filename); |
| 531 | if (wpath == NULL) { |
| 532 | xmlFree(fromUri); |
| 533 | return(XML_ERR_NO_MEMORY); |
| 534 | } |
| 535 | if (write) |
| 536 | flags = _O_WRONLY | _O_CREAT | _O_TRUNC; |
| 537 | else |
| 538 | flags = _O_RDONLY; |
| 539 | fd = _wopen(wpath, flags | _O_BINARY, 0666); |
| 540 | xmlFree(wpath); |
| 541 | } |
| 542 | #else |
| 543 | if (write) |
| 544 | flags = O_WRONLY | O_CREAT | O_TRUNC; |
| 545 | else |
| 546 | flags = O_RDONLY; |
| 547 | fd = open(filename, flags, 0666); |
| 548 | #endif /* WIN32 */ |
| 549 | |
| 550 | if (fd < 0) { |
| 551 | /* |
| 552 | * Windows and possibly other platforms return EINVAL |
| 553 | * for invalid filenames. |
| 554 | */ |
| 555 | if ((errno == ENOENT) || (errno == EINVAL)) { |
| 556 | ret = XML_IO_ENOENT; |
| 557 | } else { |
| 558 | /* |
| 559 | * This error won't be forwarded to the parser context |
| 560 | * which will report it a second time. |
| 561 | */ |
| 562 | ret = xmlIOErr(0, filename); |
| 563 | } |
| 564 | } else { |
| 565 | *out = fd; |
| 566 | ret = XML_ERR_OK; |
no test coverage detected