OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. | 2 * Copyright (c) 2012 The Native Client Authors. All rights reserved. |
3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
5 */ | 5 */ |
6 | 6 |
7 /* | 7 /* |
8 * NaCl Service Runtime. I/O Descriptor / Handle abstraction. Memory | 8 * NaCl Service Runtime. I/O Descriptor / Handle abstraction. Memory |
9 * mapping using descriptors. | 9 * mapping using descriptors. |
10 */ | 10 */ |
(...skipping 1379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1390 if (_getcwd(path, (int) len) == NULL) | 1390 if (_getcwd(path, (int) len) == NULL) |
1391 return -NaClXlateErrno(errno); | 1391 return -NaClXlateErrno(errno); |
1392 return 0; | 1392 return 0; |
1393 } | 1393 } |
1394 | 1394 |
1395 int NaClHostDescUnlink(const char *path) { | 1395 int NaClHostDescUnlink(const char *path) { |
1396 if (_unlink(path) != 0) | 1396 if (_unlink(path) != 0) |
1397 return -errno; | 1397 return -errno; |
1398 return 0; | 1398 return 0; |
1399 } | 1399 } |
| 1400 |
| 1401 int NaClHostDescTruncate(const char *path, off_t length) { |
| 1402 HANDLE hFile; |
| 1403 LARGE_INTEGER to_move; |
| 1404 DWORD err; |
| 1405 int retval = 0; |
| 1406 |
| 1407 hFile = CreateFileA(path, GENERIC_WRITE, |
| 1408 FILE_SHARE_WRITE, |
| 1409 NULL, |
| 1410 OPEN_EXISTING, |
| 1411 FILE_ATTRIBUTE_NORMAL, |
| 1412 NULL); |
| 1413 |
| 1414 to_move.QuadPart = length; |
| 1415 if (!SetFilePointerEx(hFile, to_move, (LARGE_INTEGER *) NULL, FILE_BEGIN)) { |
| 1416 err = GetLastError(); |
| 1417 retval = -NaClXlateSystemError(err); |
| 1418 goto out_file; |
| 1419 } |
| 1420 |
| 1421 if (!SetEndOfFile(hFile)) { |
| 1422 err = GetLastError(); |
| 1423 retval = -NaClXlateSystemError(err); |
| 1424 goto out_file; |
| 1425 } |
| 1426 |
| 1427 out_file: |
| 1428 CloseHandle(hFile); |
| 1429 out: |
| 1430 return retval; |
| 1431 } |
| 1432 |
| 1433 int NaClHostDescLstat(char const *host_os_pathname, nacl_host_stat_t *nhsp) { |
| 1434 } |
| 1435 |
| 1436 int NaClHostDescLink(const char *oldpath, const char *newpath) { |
| 1437 if (!CreateHardLink(newpath, oldpath, NULL)) { |
| 1438 DWORD err = GetLastError(); |
| 1439 return -NaClXlateSystemError(err); |
| 1440 } |
| 1441 return 0; |
| 1442 } |
| 1443 |
| 1444 int NaClHostDescChmod(const char *path, int mode) { |
| 1445 if (-1 == _chmod(path, mode)) { |
| 1446 return -GetErrno(); |
| 1447 } |
| 1448 return 0; |
| 1449 } |
| 1450 |
| 1451 int NaClHostDescAccess(const char *pathname, int mode) { |
| 1452 return -_access_s(path, mode); |
| 1453 } |
| 1454 |
| 1455 int NaClHostDescRename(const char *oldpath, const char *newpath) { |
| 1456 if (-1 == rename(oldpath, newpath)) { |
| 1457 return -GetErrno(); |
| 1458 } |
| 1459 return 0; |
| 1460 } |
| 1461 |
| 1462 int NaClHostDescReadlink(const char *path, char *buf, size_t bufsiz) { |
| 1463 DWORD attributes; |
| 1464 HANDLE hFile; |
| 1465 char buffer[16384]; |
| 1466 REPARSE_DATA_BUFFER rpBuffer; |
| 1467 PREPARSE_DATA_BUFFER *pBuffer; |
| 1468 wchar_t *rpath; |
| 1469 int retval; |
| 1470 errno_t err; |
| 1471 |
| 1472 if (INVALID_FILE_ATTRIBUTES == (attributes = GetFileAttributes(path))) { |
| 1473 DWORD err = GetLastError(); |
| 1474 return -NaClXlateSystemError(err); |
| 1475 } |
| 1476 |
| 1477 if (0 == (attr & REPARSE_FOLDER)) { |
| 1478 return -NACL_ABI_EINVAL; |
| 1479 } |
| 1480 |
| 1481 if (0 == (GetFileAttributes(path) & REPARSE_FOLDER)) { |
| 1482 return -NACL_ABI_EINVAL; |
| 1483 } |
| 1484 |
| 1485 hFile = CreateFileA(path, GENERIC_READ, |
| 1486 0, |
| 1487 NULL, |
| 1488 OPEN_EXISTING, |
| 1489 FILE_FLAG_OPEN_REPARSE_POINT, |
| 1490 NULL); |
| 1491 |
| 1492 if (!DeviceIoControl(hFile, |
| 1493 FSCTL_GET_REPARSE_POINT, |
| 1494 NULL, |
| 1495 0, |
| 1496 &buffer, |
| 1497 sizeof buffer, |
| 1498 &bytesReturned, |
| 1499 NULL)) { |
| 1500 } |
| 1501 |
| 1502 CloseHandle(hFile); |
| 1503 |
| 1504 offset = pBuffer->SymbolicLinkReparseBuffer.SubstituteNameOffset / sizeof wcha
r_t; |
| 1505 len = pBuffer->SymbolicLinkReparseBuffer.SubstituteNameLength; |
| 1506 pathbuf = &pBuffer->SymbolicLinkReparseBuffer.PathBuffer[offset]; |
| 1507 |
| 1508 if (0 == (retval = WideCharToMultiByte(CP_UTF8, |
| 1509 WC_COMPOSITECHECK, |
| 1510 pathbuf, |
| 1511 pathlen |
| 1512 buf, |
| 1513 bufsiz, |
| 1514 NULL, |
| 1515 NULL))) { |
| 1516 DWORD err = GetLastError(); |
| 1517 return -NaClXlateSystemError(err); |
| 1518 } |
| 1519 |
| 1520 if (0 != (err = wcstombs_s(&retval, buf, bufsiz, pathbuf, _TRUNCATE))) { |
| 1521 return -err; |
| 1522 } |
| 1523 return retval; |
| 1524 } |
| 1525 |
| 1526 int NaClHostDescSymlink(const char *oldpath, const char *newpath) { |
| 1527 struct _stati64 buffer; |
| 1528 DWORD dwFlags; |
| 1529 |
| 1530 if (-1 == _stati64(oldpath, &buffer)) { |
| 1531 return -GetErrno(); |
| 1532 } |
| 1533 |
| 1534 dwFlags = buffer.st_mode & _S_IFDIR ? SYMBOLIC_LINK_FLAG_DIRECTORY : 0x0; |
| 1535 if (!CreateSymbolicLink(newpath, oldpath, dwFlags)) { |
| 1536 DWORD err = GetLastError(); |
| 1537 return -NaClXlateSystemError(err); |
| 1538 } |
| 1539 return 0; |
| 1540 } |
| 1541 |
| 1542 int NaClHostDescUtimes(const char *filename, |
| 1543 const struct nacl_abi_timeval times[2]) { |
| 1544 struct _utimbuf times; |
| 1545 |
| 1546 if (-1 == _utime64(filename, ×)) { |
| 1547 return -GetErrno(); |
| 1548 } |
| 1549 return 0; |
| 1550 } |
| 1551 |
| 1552 int NaClHostDescFcntl(struct NaClHostDesc *d, int cmd, long arg) { |
| 1553 NaClHostDescCheckValidity("NaClHostDescFcntl", d); |
| 1554 } |
| 1555 |
| 1556 int NaClHostDescFchmod(struct NaClHostDesc *d, int mode) { |
| 1557 NaClHostDescCheckValidity("NaClHostDescFchmod", d); |
| 1558 |
| 1559 if (-1 == _chmod(path, mode)) { |
| 1560 return -GetErrno(); |
| 1561 } |
| 1562 return 0; |
| 1563 } |
| 1564 |
| 1565 int NaClHostDescFsync(struct NaClHostDesc *d) { |
| 1566 HANDLE hFile; |
| 1567 DWORD err; |
| 1568 |
| 1569 NaClHostDescCheckValidity("NaClHostDescFsync", d); |
| 1570 |
| 1571 hFile = (HANDLE) _get_osfhandle(d->d); |
| 1572 CHECK(INVALID_HANDLE_VALUE != hFile); |
| 1573 |
| 1574 if (!FlushFileBuffers(hFile)) { |
| 1575 err = GetLastError(); |
| 1576 return -NaClXlateSystemError(err); |
| 1577 } |
| 1578 |
| 1579 return 0; |
| 1580 } |
| 1581 |
| 1582 int NaClHostDescFdatasync(struct NaClHostDesc *d) { |
| 1583 HANDLE hFile; |
| 1584 DWORD err; |
| 1585 |
| 1586 NaClHostDescCheckValidity("NaClHostDescFdatasync", d); |
| 1587 |
| 1588 hFile = (HANDLE) _get_osfhandle(d->d); |
| 1589 CHECK(INVALID_HANDLE_VALUE != hFile); |
| 1590 |
| 1591 if (!FlushFileBuffers(hFile)) { |
| 1592 err = GetLastError(); |
| 1593 return -NaClXlateSystemError(err); |
| 1594 } |
| 1595 |
| 1596 return 0; |
| 1597 } |
| 1598 |
| 1599 int NaClHostDescFtruncate(struct NaClHostDesc *d, off_t length) { |
| 1600 HANDLE hFile; |
| 1601 LARGE_INTEGER to_move; |
| 1602 DWORD err; |
| 1603 nacl_off64_t orig_pos = 0; |
| 1604 |
| 1605 NaClHostDescCheckValidity("NaClHostDescFtruncate", d); |
| 1606 |
| 1607 hFile = (HANDLE) _get_osfhandle(d->d); |
| 1608 CHECK(INVALID_HANDLE_VALUE != hFile); |
| 1609 |
| 1610 /* |
| 1611 * Ensure that we do not corrupt shared implicit file position. |
| 1612 */ |
| 1613 if (d->protect_filepos) { |
| 1614 orig_pos = NaClLockAndGetCurrentFilePos(fh); |
| 1615 } |
| 1616 to_move.QuadPart = length; |
| 1617 if (!SetFilePointerEx(hFile, to_move, (LARGE_INTEGER *) NULL, FILE_BEGIN)) { |
| 1618 err = GetLastError(); |
| 1619 retval = -NaClXlateSystemError(err); |
| 1620 goto out_file; |
| 1621 } |
| 1622 if (!SetEndOfFile(hFile)) { |
| 1623 err = GetLastError(); |
| 1624 retval = -NaClXlateSystemError(err); |
| 1625 } |
| 1626 if (d->protect_filepos) { |
| 1627 NaClSetCurrentFilePosAndUnlock(fh, orig_pos); |
| 1628 } |
| 1629 } |
OLD | NEW |