1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802
|
/*
** The Sleuth Kit
**
** Copyright (c) 2022 Basis Technology Corp. All rights reserved
** Contact: Brian Carrier [carrier <at> sleuthkit [dot] org]
**
** This software is distributed under the Common Public License 1.0
**
*/
/**
*\file logical_fs.cpp
* Contains the internal TSK logical file system functions.
*/
#include <vector>
#include <map>
#include <algorithm>
#include <string>
#include <set>
#include <string.h>
#include "tsk_fs_i.h"
#include "tsk_fs.h"
#include "tsk_logical_fs.h"
#include "tsk/img/logical_img.h"
#ifdef TSK_WIN32
#include <Windows.h>
#endif
using std::vector;
using std::string;
using std::wstring;
static uint8_t
logicalfs_inode_walk(TSK_FS_INFO *fs, TSK_INUM_T start_inum,
TSK_INUM_T end_inum, TSK_FS_META_FLAG_ENUM flags,
TSK_FS_META_WALK_CB a_action, void *a_ptr)
{
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNSUPFUNC);
tsk_error_set_errstr("block_walk for logical directory is not implemented");
return 1;
}
static uint8_t
logicalfs_block_walk(TSK_FS_INFO *a_fs, TSK_DADDR_T a_start_blk,
TSK_DADDR_T a_end_blk, TSK_FS_BLOCK_WALK_FLAG_ENUM a_flags,
TSK_FS_BLOCK_WALK_CB a_action, void *a_ptr)
{
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNSUPFUNC);
tsk_error_set_errstr("block_walk for logical directory is not implemented");
return 1;
}
static TSK_FS_BLOCK_FLAG_ENUM
logicalfs_block_getflags(TSK_FS_INFO *fs, TSK_DADDR_T a_addr)
{
return TSK_FS_BLOCK_FLAG_UNUSED;
}
static TSK_FS_ATTR_TYPE_ENUM
logicalfs_get_default_attr_type(const TSK_FS_FILE * /*a_file*/)
{
return TSK_FS_ATTR_TYPE_DEFAULT;
}
/*
* Convert a FILETIME to a timet
*
* @param ft The FILETIME to convert
*
* @return The converted timet
*/
#ifdef TSK_WIN32
static time_t
filetime_to_timet(FILETIME const& ft)
{
ULARGE_INTEGER ull;
ull.LowPart = ft.dwLowDateTime;
ull.HighPart = ft.dwHighDateTime;
return ull.QuadPart / 10000000ULL - 11644473600ULL;
}
#endif
/*
* Create a LOGICALFS_SEARCH_HELPER that will run a search for
* the given inum.
*
* @param target_inum The inum to search for
*
* @return The search helper object (must be freed by caller)
*/
static LOGICALFS_SEARCH_HELPER*
create_inum_search_helper(TSK_INUM_T target_inum) {
LOGICALFS_SEARCH_HELPER *helper = (LOGICALFS_SEARCH_HELPER *)tsk_malloc(sizeof(LOGICALFS_SEARCH_HELPER));
if (helper == NULL)
return NULL;
helper->target_found = false;
helper->search_type = LOGICALFS_SEARCH_BY_INUM;
helper->target_path = NULL;
helper->target_inum = target_inum;
helper->found_path = NULL;
return helper;
}
/*
* Create a LOGICALFS_SEARCH_HELPER that will run a search over
* the entire image. Used to find the max inum.
*
* @return The search helper object (must be freed by caller)
*/
static LOGICALFS_SEARCH_HELPER*
create_max_inum_search_helper() {
LOGICALFS_SEARCH_HELPER *helper = (LOGICALFS_SEARCH_HELPER *)tsk_malloc(sizeof(LOGICALFS_SEARCH_HELPER));
if (helper == NULL)
return NULL;
helper->target_found = false;
helper->search_type = LOGICALFS_NO_SEARCH;
helper->target_path = NULL;
helper->found_path = NULL;
return helper;
}
/*
* Create a LOGICALFS_SEARCH_HELPER that will run a search for
* the given path.
*
* @param target_path The path to search for
*
* @return The search helper object (must be freed by caller)
*/
static LOGICALFS_SEARCH_HELPER*
create_path_search_helper(const TSK_TCHAR *target_path) {
LOGICALFS_SEARCH_HELPER *helper = (LOGICALFS_SEARCH_HELPER *)tsk_malloc(sizeof(LOGICALFS_SEARCH_HELPER));
if (helper == NULL)
return NULL;
helper->target_found = false;
helper->search_type = LOGICALFS_SEARCH_BY_PATH;
helper->target_path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(target_path) + 1));
TSTRNCPY(helper->target_path, target_path, TSTRLEN(target_path) + 1);
helper->found_inum = LOGICAL_INVALID_INUM;
helper->found_path = NULL;
return helper;
}
/*
* Free the search helper object
*
* @param helper The object to free
*/
static void
free_search_helper(LOGICALFS_SEARCH_HELPER* helper) {
if (helper->target_path != NULL) {
free(helper->target_path);
}
if (helper->found_path != NULL) {
free(helper->found_path);
}
free(helper);
}
/*
* Convert a wide string to UTF8.
*
* @param source The wide string to convert.
*
* @return The converted string (must be freed by caller) or "INVALID FILE NAME" if conversion fails. NULL if memory allocation fails.
*/
#ifdef TSK_WIN32
static char*
convert_wide_string_to_utf8(const wchar_t *source) {
const char invalidName[] = "INVALID FILE NAME";
UTF16 *utf16 = (UTF16 *)source;
size_t ilen = wcslen(source);
size_t maxUTF8len = ilen * 4;
if (maxUTF8len < strlen(invalidName) + 1) {
maxUTF8len = strlen(invalidName) + 1;
}
char *dest = (char*)tsk_malloc(maxUTF8len);
if (dest == NULL) {
return NULL;
}
UTF8 *utf8 = (UTF8*)dest;
TSKConversionResult retVal =
tsk_UTF16toUTF8_lclorder((const UTF16 **)&utf16,
&utf16[ilen], &utf8,
&utf8[maxUTF8len], TSKlenientConversion);
if (retVal != TSKconversionOK) {
// If the conversion failed, use a default name
if (tsk_verbose)
tsk_fprintf(stderr,
"convert_wide_string_to_utf8: error converting logical file name to UTF-8\n");
strncpy(dest, invalidName, strlen(invalidName));
}
return dest;
}
#endif
/*
* Check if we should set the type as directory.
* We currently treat sym links as regular files to avoid
* issues trying to read then as directories.
*/
#ifdef TSK_WIN32
int
shouldTreatAsDirectory(DWORD dwFileAttributes) {
return ((dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
&& (!(dwFileAttributes & FILE_ATTRIBUTE_REPARSE_POINT)));
}
#endif
/*
* Use data in the WIN32_FIND_DATA to populate a TSK_FS_FILE object.
* Expects a_fs_file and a_fs_file->meta to be allocated
*
* @param fd The find data results
* @param a_fs_file The file to populate
*
* @return TSK_OK if successful, TSK_ERR otherwise
*/
#ifdef TSK_WIN32
TSK_RETVAL_ENUM
populate_fs_file_from_win_find_data(const WIN32_FIND_DATA* fd, TSK_FS_FILE * a_fs_file) {
if (a_fs_file == NULL || a_fs_file->meta == NULL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("populate_fs_file_from_win_find_data - a_fs_file argument not initialized");
return TSK_ERR;
}
// For the current use case, we leave the timestamps set to zero.
//a_fs_file->meta->crtime = filetime_to_timet(fd->ftCreationTime);
//a_fs_file->meta->atime = filetime_to_timet(fd->ftLastAccessTime);
//a_fs_file->meta->mtime = filetime_to_timet(fd->ftLastWriteTime);
// Set the type
if (shouldTreatAsDirectory(fd->dwFileAttributes)) {
a_fs_file->meta->type = TSK_FS_META_TYPE_DIR;
}
else {
a_fs_file->meta->type = TSK_FS_META_TYPE_REG;
}
// All files are allocated
a_fs_file->meta->flags = TSK_FS_META_FLAG_ALLOC;
// Set the file size
LARGE_INTEGER ull;
ull.LowPart = fd->nFileSizeLow;
ull.HighPart = fd->nFileSizeHigh;
a_fs_file->meta->size = ull.QuadPart;
return TSK_OK;
}
#endif
/*
* Create the wildcard search path used to find directory contents
*
* @param base_path The path to the directory to open
*
* @return The search path with wildcard appended (must be freed by caller)
*/
TSK_TCHAR * create_search_path(const TSK_TCHAR *base_path) {
size_t len = TSTRLEN(base_path);
TSK_TCHAR * searchPath;
size_t searchPathLen = len + 4;
searchPath = (TSK_TCHAR *)tsk_malloc(sizeof(TSK_TCHAR) * (searchPathLen));
if (searchPath == NULL) {
return NULL;
}
#ifdef TSK_WIN32
TSTRNCPY(searchPath, base_path, len + 1);
TSTRNCAT(searchPath, L"\\*", 4);
#else
TSTRNCPY(searchPath, base_path, len + 1);
TSTRNCAT(searchPath, "/*", 3);
#endif
return searchPath;
}
/*
* Create the wildcard search path used to find directory contents using
* the absolute directory and unicode prefix. We only call this method for
* long paths because it does not work in cygwin - prepending "\\?\" only
* works for absolute paths starting with a drive letter.
*
* @param base_path The path to the directory to open
*
* @return The search path with wildcard appended (must be freed by caller)
*/
TSK_TCHAR * create_search_path_long_path(const TSK_TCHAR *base_path) {
#ifdef TSK_WIN32
// First convert the base path to an absolute path
TCHAR absPath[LOGICAL_MAX_PATH_UNICODE];
int ret = GetFullPathNameW(base_path, LOGICAL_MAX_PATH_UNICODE, absPath, NULL);
size_t len = TSTRLEN(absPath);
TSK_TCHAR * searchPath;
size_t searchPathLen = len + 9;
searchPath = (TSK_TCHAR *)tsk_malloc(sizeof(TSK_TCHAR) * (searchPathLen));
if (searchPath == NULL) {
return NULL;
}
TSTRNCPY(searchPath, L"\\\\?\\", 5);
TSTRNCAT(searchPath, absPath, len + 1);
TSTRNCAT(searchPath, L"\\*", 4);
return searchPath;
#else
// Nothing to do here if it's not Windows
return NULL;
#endif
}
/*
* Load the names of child files and/or directories into the given vectors.
*
* @param base_path The parent path
* @param file_names Will be populated with file names contained in the parent dir (if requested)
* @param dir_names Will be populated with dir names contained in the parent dir (if requested)
* @param mode Specifies whether files, directories, or both should be loaded
*
* @return TSK_OK if successful, TSK_ERR otherwise
*/
#ifdef TSK_WIN32
static TSK_RETVAL_ENUM
load_dir_and_file_lists_win(const TSK_TCHAR *base_path, vector<wstring>& file_names, vector<wstring>& dir_names, LOGICALFS_DIR_LOADING_MODE mode) {
WIN32_FIND_DATAW fd;
HANDLE hFind;
// Create the search string (base path + "\*")
TSK_TCHAR * search_path_wildcard = create_search_path(base_path);
if (search_path_wildcard == NULL) {
return TSK_ERR;
}
// If the paths is too long, attempt to make a different version that will work
if (TSTRLEN(search_path_wildcard) >= MAX_PATH) {
free(search_path_wildcard);
search_path_wildcard = create_search_path_long_path(base_path);
if (search_path_wildcard == NULL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("load_dir_and_file_lists: Error looking up contents of directory (path too long) %" PRIttocTSK, base_path);
return TSK_ERR;
}
}
// Look up all files and folders in the base directory
hFind = ::FindFirstFileW(search_path_wildcard, &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (shouldTreatAsDirectory(fd.dwFileAttributes)) {
if (mode == LOGICALFS_LOAD_ALL || mode == LOGICALFS_LOAD_DIRS_ONLY) {
// For the moment at least, skip . and ..
if (0 != wcsncmp(fd.cFileName, L"..", 3) && 0 != wcsncmp(fd.cFileName, L".", 3)) {
dir_names.push_back(fd.cFileName);
}
}
}
else {
if (mode == LOGICALFS_LOAD_ALL || mode == LOGICALFS_LOAD_FILES_ONLY) {
// For now, consider everything else to be a file
file_names.push_back(fd.cFileName);
}
}
} while (::FindNextFileW(hFind, &fd));
::FindClose(hFind);
free(search_path_wildcard);
return TSK_OK;
}
else {
free(search_path_wildcard);
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("load_dir_and_file_lists: Error looking up contents of directory %" PRIttocTSK, base_path);
return TSK_ERR;
}
}
#endif
/*
* Finds closest cache match for the given path.
* If best_path is not NULL, caller must free.
*
* @param logical_fs_info The logical file system
* @param target_path The full path being searched for
* @param best_path The best match found in the cache (NULL if none are found, must be freed by caller otherwise)
* @param best_inum The inum matching the best path found
*
* @return TSK_ERR if an error occurred, TSK_OK otherwise
*/
static TSK_RETVAL_ENUM
find_closest_path_match_in_cache(LOGICALFS_INFO *logical_fs_info, TSK_TCHAR *target_path, TSK_TCHAR **best_path, TSK_INUM_T *best_inum) {
TSK_IMG_INFO* img_info = logical_fs_info->fs_info.img_info;
IMG_LOGICAL_INFO* logical_img_info = (IMG_LOGICAL_INFO*)img_info;
tsk_take_lock(&(img_info->cache_lock));
*best_inum = LOGICAL_INVALID_INUM;
*best_path = NULL;
int best_match_index = -1;
size_t longest_match = 0;
size_t target_len = TSTRLEN(target_path);
for (int i = 0; i < LOGICAL_INUM_CACHE_LEN; i++) {
if (logical_img_info->inum_cache[i].path != NULL) {
// Check that:
// - We haven't already found the exact match (longest_match = target_len)
// - The cache entry could potentially be a longer match than what we have so far
// - The cache entry isn't longer than what we're looking for
size_t cache_path_len = TSTRLEN(logical_img_info->inum_cache[i].path);
if ((longest_match != target_len) && (cache_path_len > longest_match) && (cache_path_len <= target_len)) {
size_t matching_len;
#ifdef TSK_WIN32
matching_len = 0;
if (0 == _wcsnicmp(target_path, logical_img_info->inum_cache[i].path, cache_path_len)) {
matching_len = cache_path_len;
}
#endif
// Save this path if:
// - It is longer than our previous best match
// - It is either the full length of the path we're searching for or is a valid
// substring of our path
if ((matching_len > longest_match) &&
((matching_len == target_len) || (((matching_len < target_len) && (target_path[matching_len] == L'/'))))) {
// We found the full path or a partial match
longest_match = matching_len;
best_match_index = i;
// For the moment, consider any potential best match to have been useful. We could
// change this to only reset the age of the actual best match.
logical_img_info->inum_cache[i].cache_age = LOGICAL_INUM_CACHE_MAX_AGE;
}
else {
// The cache entry was not useful so decrease the age
if (logical_img_info->inum_cache[i].cache_age > 1) {
logical_img_info->inum_cache[i].cache_age--;
}
}
}
else {
// The cache entry was not useful so decrease the age
if (logical_img_info->inum_cache[i].cache_age > 1) {
logical_img_info->inum_cache[i].cache_age--;
}
}
}
}
// If we found a full or partial match, store the values
if (best_match_index >= 0) {
*best_inum = logical_img_info->inum_cache[best_match_index].inum;
*best_path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(logical_img_info->inum_cache[best_match_index].path) + 1));
if (*best_path == NULL) {
tsk_release_lock(&(img_info->cache_lock));
return TSK_ERR;
}
TSTRNCPY(*best_path, logical_img_info->inum_cache[best_match_index].path, TSTRLEN(logical_img_info->inum_cache[best_match_index].path) + 1);
}
tsk_release_lock(&(img_info->cache_lock));
return TSK_OK;
}
/*
* Look up the path corresponding to the given inum in the cache.
* Returned path must be freed by caller.
*
* @param logical_fs_info The logical file system
* @param target_inum The inum we're searching for
*
* @return The path corresponding to the given inum or NULL if not found or an error occurred. Must be freed by caller.
*/
static TSK_TCHAR*
find_path_for_inum_in_cache(LOGICALFS_INFO *logical_fs_info, TSK_INUM_T target_inum) {
TSK_IMG_INFO* img_info = logical_fs_info->fs_info.img_info;
IMG_LOGICAL_INFO* logical_img_info = (IMG_LOGICAL_INFO*)img_info;
tsk_take_lock(&(img_info->cache_lock));
TSK_TCHAR *target_path = NULL;
for (int i = 0; i < LOGICAL_INUM_CACHE_LEN; i++) {
if ((target_path == NULL) && (logical_img_info->inum_cache[i].inum == target_inum)) {
// The cache entry was useful so reset the age
logical_img_info->inum_cache[i].cache_age = LOGICAL_INUM_CACHE_MAX_AGE;
// Copy the path
target_path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(logical_img_info->inum_cache[i].path) + 1));
if (target_path == NULL) {
tsk_release_lock(&(img_info->cache_lock));
return NULL;
}
TSTRNCPY(target_path, logical_img_info->inum_cache[i].path, TSTRLEN(logical_img_info->inum_cache[i].path) + 1);
}
else {
// The cache entry was not useful so decrease the age
if (logical_img_info->inum_cache[i].cache_age > 1) {
logical_img_info->inum_cache[i].cache_age--;
}
}
}
tsk_release_lock(&(img_info->cache_lock));
return target_path;
}
/*
* Add a directory to the cache
*
* @param logical_fs_info The logical file system
* @param path The directory path
* @param inum The inum corresponding to the path
*
* @return TSK_OK if successful, TSK_ERR on error
*/
static TSK_RETVAL_ENUM
add_directory_to_cache(LOGICALFS_INFO *logical_fs_info, const TSK_TCHAR *path, TSK_INUM_T inum) {
// If the path is very long then don't cache it to make sure the cache stays reasonably small.
if (TSTRLEN(path) > LOGICAL_INUM_CACHE_MAX_PATH_LEN) {
return TSK_OK;
}
TSK_IMG_INFO* img_info = logical_fs_info->fs_info.img_info;
IMG_LOGICAL_INFO* logical_img_info = (IMG_LOGICAL_INFO*)img_info;
tsk_take_lock(&(img_info->cache_lock));
// Find the next cache slot. If we find an unused slot, use that. Otherwise find the entry
// with the lowest age.
int next_slot = 0;
int lowest_age = LOGICAL_INUM_CACHE_MAX_AGE + 1;
for (int i = 0; i < LOGICAL_INUM_CACHE_LEN; i++) {
if (logical_img_info->inum_cache[i].inum == LOGICAL_INVALID_INUM) {
next_slot = i;
break;
}
if (logical_img_info->inum_cache[i].cache_age < lowest_age) {
next_slot = i;
lowest_age = logical_img_info->inum_cache[i].cache_age;
}
}
clear_inum_cache_entry(logical_img_info, next_slot);
// Copy the data
logical_img_info->inum_cache[next_slot].path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(path) + 1));
if (logical_img_info->inum_cache[next_slot].path == NULL) {
tsk_release_lock(&(img_info->cache_lock));
return TSK_ERR;
}
TSTRNCPY(logical_img_info->inum_cache[next_slot].path, path, TSTRLEN(path) + 1);
logical_img_info->inum_cache[next_slot].inum = inum;
logical_img_info->inum_cache[next_slot].cache_age = LOGICAL_INUM_CACHE_MAX_AGE;
tsk_release_lock(&(img_info->cache_lock));
return TSK_OK;
}
/*
* Main recursive method for walking the directories. Will load and sort all directories found
* in parent_path, assign an inum to each and check if this is what we're searching for, calling
* this method recursively if not.
*
* @param parent_path The full path on disk to the directory to open
* @last_inum_ptr Pointer to the last assigned inum. Will be updated for every directory found
* @search_helper Contains information on what type of search is being performed and will store the results in most cases.
*
* @return TSK_OK if successfull, TSK_ERR otherwise
*/
static TSK_RETVAL_ENUM
search_directory_recursive(LOGICALFS_INFO *logical_fs_info, const TSK_TCHAR * parent_path, TSK_INUM_T *last_inum_ptr, LOGICALFS_SEARCH_HELPER* search_helper) {
#ifdef TSK_WIN32
vector<wstring> file_names;
vector<wstring> dir_names;
#else
vector<string> file_names;
vector<string> dir_names;
#endif
// If we're searching for a file and this is the correct directory, load only the files in the folder and
// return the correct one.
if (search_helper->search_type == LOGICALFS_SEARCH_BY_INUM
&& (*last_inum_ptr == (search_helper->target_inum & 0xffff0000))
& ((search_helper->target_inum & 0xffff) != 0)) {
#ifdef TSK_WIN32
if (TSK_OK != load_dir_and_file_lists_win(parent_path, file_names, dir_names, LOGICALFS_LOAD_FILES_ONLY)) {
// Error message already set
return TSK_ERR;
}
#endif
sort(file_names.begin(), file_names.end());
// Look for the file corresponding to the given inum
size_t file_index = (search_helper->target_inum & 0xffff) - 1;
if (file_names.size() <= file_index) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_INODE_NUM);
tsk_error_set_errstr("search_directory_recusive - inum %" PRIuINUM " not found", search_helper->target_inum);
return TSK_ERR;
}
search_helper->target_found = true;
size_t found_path_len = TSTRLEN(parent_path) + 1 + TSTRLEN(file_names[file_index].c_str());
search_helper->found_path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (found_path_len + 1));
TSTRNCPY(search_helper->found_path, parent_path, TSTRLEN(parent_path) + 1);
#ifdef TSK_WIN32
TSTRNCAT(search_helper->found_path, L"\\", 2);
#else
TSTRNCAT(search_helper->found_path, "/", 2);
#endif
TSTRNCAT(search_helper->found_path, file_names[file_index].c_str(), TSTRLEN(file_names[file_index].c_str()) + 1);
return TSK_OK;
}
#ifdef TSK_WIN32
if (TSK_OK != load_dir_and_file_lists_win(parent_path, file_names, dir_names, LOGICALFS_LOAD_DIRS_ONLY)) {
// Error message already set
return TSK_ERR;
}
#endif
// Sort the directory names
sort(dir_names.begin(), dir_names.end());
// Set up the beginning of full path to the file on disk
// The directoy name being added should generally be less than 270 characters, but if necessary we will
// make more space available.
size_t allocated_dir_name_len = 270;
TSK_TCHAR* current_path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(parent_path) + 2 + allocated_dir_name_len));
if (current_path == NULL)
return TSK_ERR;
TSTRNCPY(current_path, parent_path, TSTRLEN(parent_path) + 1);
#ifdef TSK_WIN32
TSTRNCAT(current_path, L"\\", 2);
#else
TSTRNCAT(current_path, "/", 2);
#endif
size_t parent_path_len = TSTRLEN(current_path);
for (size_t i = 0; i < dir_names.size();i++) {
// If we don't have space for this name, increase the size of the buffer
if (TSTRLEN(dir_names[i].c_str()) > allocated_dir_name_len) {
free(current_path);
allocated_dir_name_len = TSTRLEN(dir_names[i].c_str()) + 20;
current_path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(parent_path) + 2 + allocated_dir_name_len));
if (current_path == NULL)
return TSK_ERR;
TSTRNCPY(current_path, parent_path, TSTRLEN(parent_path) + 1);
#ifdef TSK_WIN32
TSTRNCAT(current_path, L"\\", 2);
#else
TSTRNCAT(current_path, "/", 2);
#endif
}
// Append the current directory name to the parent path
TSTRNCPY(current_path + parent_path_len, dir_names[i].c_str(), TSTRLEN(dir_names[i].c_str()) + 1);
TSK_INUM_T current_inum = *last_inum_ptr + LOGICAL_INUM_DIR_INC;
*last_inum_ptr = current_inum;
add_directory_to_cache(logical_fs_info, current_path, current_inum);
// Check if we've found it
if ((search_helper->search_type == LOGICALFS_SEARCH_BY_PATH)
&& (TSTRCMP(current_path, search_helper->target_path) == 0)) {
search_helper->target_found = true;
search_helper->found_inum = current_inum;
free(current_path);
return TSK_OK;
}
if ((search_helper->search_type == LOGICALFS_SEARCH_BY_INUM)
&& (current_inum == search_helper->target_inum)) {
search_helper->target_found = true;
search_helper->found_path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(current_path) + 1));
if (search_helper->found_path == NULL)
return TSK_ERR;
TSTRNCPY(search_helper->found_path, current_path, TSTRLEN(current_path) + 1);
free(current_path);
return TSK_OK;
}
TSK_RETVAL_ENUM result = search_directory_recursive(logical_fs_info, current_path, last_inum_ptr, search_helper);
if (result != TSK_OK) {
free(current_path);
return result;
}
if (search_helper->target_found) {
free(current_path);
return TSK_OK;
}
}
free(current_path);
return TSK_OK;
}
/*
* Find the path corresponding to the given inum
*
* @param logical_fs_info The logical file system
* @param a_addr The inum to search for
*
* @return The path corresponding to the inum. Null on error. Must be freed by caller.
*/
static TSK_TCHAR *
load_path_from_inum(LOGICALFS_INFO *logical_fs_info, TSK_INUM_T a_addr) {
TSK_TCHAR *path = NULL;
if (a_addr == logical_fs_info->fs_info.root_inum) {
// No need to do a search - it's just the root folder
path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(logical_fs_info->base_path) + 1));
if (path == NULL)
return NULL;
TSTRNCPY(path, logical_fs_info->base_path, TSTRLEN(logical_fs_info->base_path) + 1);
return path;
}
// Default starting position for the search is the base folder
TSK_INUM_T starting_inum = logical_fs_info->fs_info.root_inum;
const TSK_TCHAR *starting_path = logical_fs_info->base_path;
// See if the directory is in the cache
TSK_INUM_T dir_addr = a_addr & 0xffff0000;
TSK_TCHAR *cache_path = find_path_for_inum_in_cache(logical_fs_info, dir_addr);
if (cache_path != NULL) {
if (dir_addr == a_addr) {
// If we were looking for a directory, we're done
return cache_path;
}
// Otherwise, set up the search parameters to start with the folder found
starting_inum = dir_addr;
starting_path = cache_path;
}
// Create the struct that holds search params and results
LOGICALFS_SEARCH_HELPER *search_helper = create_inum_search_helper(a_addr);
if (search_helper == NULL) {
return NULL;
}
// Run the search
TSK_RETVAL_ENUM result = search_directory_recursive(logical_fs_info, starting_path, &starting_inum, search_helper);
if (cache_path != NULL) {
free(cache_path);
}
if ((result != TSK_OK) || (!search_helper->target_found)) {
free_search_helper(search_helper);
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_INODE_NUM);
tsk_error_set_errstr("load_path_from_inum - failed to find path corresponding to inum %" PRIuINUM, search_helper->target_inum);
return NULL;
}
// Copy the path
path = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) * (TSTRLEN(search_helper->found_path) + 1));
if (path == NULL) {
free_search_helper(search_helper);
return NULL;
}
TSTRNCPY(path, search_helper->found_path, TSTRLEN(search_helper->found_path) + 1);
free_search_helper(search_helper);
return path;
}
static uint8_t
logicalfs_file_add_meta(TSK_FS_INFO *a_fs, TSK_FS_FILE * a_fs_file,
TSK_INUM_T inum)
{
LOGICALFS_INFO *logical_fs_info = (LOGICALFS_INFO*)a_fs;
if (a_fs_file == NULL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logicalfs_file_add_meta - null TSK_FS_FILE given");
return TSK_ERR;
}
if (a_fs_file->meta == NULL) {
if ((a_fs_file->meta = tsk_fs_meta_alloc(0)) == NULL) {
return TSK_ERR;
}
}
else {
tsk_fs_meta_reset(a_fs_file->meta);
}
a_fs_file->meta->addr = inum;
// Get the full path to the given file
TSK_TCHAR* path = load_path_from_inum(logical_fs_info, inum);
if (path == NULL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_INODE_NUM);
tsk_error_set_errstr("logicalfs_file_add_meta - Error loading directory with inum %" PRIuINUM, inum);
return TSK_ERR;
}
#ifdef TSK_WIN32
// Load the file
WIN32_FIND_DATAW fd;
HANDLE hFind;
if (TSTRLEN(path) < MAX_PATH) {
hFind = ::FindFirstFileW(path, &fd);
}
else {
TCHAR absPath[LOGICAL_MAX_PATH_UNICODE + 4];
TSTRNCPY(absPath, L"\\\\?\\", 4);
int absPathLen = GetFullPathNameW(path, LOGICAL_MAX_PATH_UNICODE, &(absPath[4]), NULL);
if (absPathLen <= 0) {
free(path);
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("logicalfs_file_add_meta: Error looking up contents of directory (path too long) %" PRIttocTSK, path);
return TSK_ERR;
}
hFind = ::FindFirstFileW(absPath, &fd);
}
if (hFind != INVALID_HANDLE_VALUE) {
TSK_RETVAL_ENUM result = populate_fs_file_from_win_find_data(&fd, a_fs_file);
::FindClose(hFind);
free(path);
return result;
}
else {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("logicalfs_file_add_meta: Error loading directory %" PRIttocTSK, path);
free(path);
return TSK_ERR;
}
#endif
free(path);
return TSK_OK;
}
/*
* Find the max inum in the logical image
*
* @param logical_fs_info The logical file system
*
* @return The max inum, or LOGICAL_INVALID_INUM if an error occurred
*/
static TSK_INUM_T
find_max_inum(LOGICALFS_INFO *logical_fs_info) {
// Create the struct that holds search params and results
LOGICALFS_SEARCH_HELPER *search_helper = create_max_inum_search_helper();
if (search_helper == NULL) {
return LOGICAL_INVALID_INUM;
}
// Run the search to get the maximum directory inum
TSK_INUM_T last_assigned_inum = logical_fs_info->fs_info.root_inum;
TSK_RETVAL_ENUM result = search_directory_recursive(logical_fs_info, logical_fs_info->base_path, &last_assigned_inum, search_helper);
free_search_helper(search_helper);
if (result != TSK_OK) {
return LOGICAL_INVALID_INUM;
}
// The maximum inum will be the inum of the last file in that folder. We don't care which file it is,
// so just getting a count is sufficient. First we need the path on disk corresponding to the last
// directory inum.
TSK_TCHAR* path = load_path_from_inum(logical_fs_info, last_assigned_inum);
if (path == NULL) {
return LOGICAL_INVALID_INUM;
}
// Finally we need to get a count of files in that last folder. The max inum is the
// folder inum plus the number of files (if none, it'll just be the folder inum).
#ifdef TSK_WIN32
vector<wstring> file_names;
vector<wstring> dir_names;
if (TSK_OK != load_dir_and_file_lists_win(path, file_names, dir_names, LOGICALFS_LOAD_FILES_ONLY)) {
free(path);
return LOGICAL_INVALID_INUM;
}
#else
vector<string> file_names;
vector<string> dir_names;
#endif
free(path);
last_assigned_inum += file_names.size();
return last_assigned_inum;
}
/*
* Find the inum corresponding to the given path
*
* @param logical_fs_info The logical file system
* @param a_addr The inum to search for
* @param base_path Will be loaded with path corresponding to the inum
* @param base_path_len Size of base_path
*
* @return The corresponding inum, or LOGICAL_INVALID_INUM if an error occurs
*/
static TSK_INUM_T
#ifdef TSK_WIN32
get_inum_from_directory_path(LOGICALFS_INFO *logical_fs_info, TSK_TCHAR *base_path, wstring& dir_path) {
#else
get_inum_from_directory_path(LOGICALFS_INFO *logical_fs_info, TSK_TCHAR *base_path, string& dir_path) {
#endif
// Get the full path on disk by combining the base path for the logical image with the relative path in dir_path
size_t len = TSTRLEN(base_path) + dir_path.length() + 1;
TSK_TCHAR *path_buf = (TSK_TCHAR*)tsk_malloc(sizeof(TSK_TCHAR) *(len + 2));
TSTRNCPY(path_buf, base_path, TSTRLEN(base_path) + 1);
#ifdef TSK_WIN32
TSTRNCAT(path_buf, L"\\", 2);
#else
TSTRNCAT(path_buf, "/", 2);
#endif
TSTRNCAT(path_buf, dir_path.c_str(), TSTRLEN(dir_path.c_str()) + 1);
// Default starting position for search is the base folder
TSK_INUM_T starting_inum = logical_fs_info->fs_info.root_inum;
const TSK_TCHAR *starting_path = logical_fs_info->base_path;
// See how close we can get using the cache
TSK_TCHAR *cache_path = NULL;
TSK_INUM_T cache_inum = LOGICAL_INVALID_INUM;
TSK_RETVAL_ENUM result = find_closest_path_match_in_cache(logical_fs_info, path_buf, &cache_path, &cache_inum);
if (result != TSK_OK) {
return LOGICAL_INVALID_INUM;
}
if (cache_inum != LOGICAL_INVALID_INUM) {
if (TSTRCMP(path_buf, cache_path) == 0) {
// We found an exact match - no need to do a search
free(cache_path);
return cache_inum;
}
// Otherwise, we at least have a better place to start the search
starting_inum = cache_inum;
starting_path = cache_path;
}
// Create the struct that holds search params and results
LOGICALFS_SEARCH_HELPER *search_helper = create_path_search_helper(path_buf);
free(path_buf);
if (search_helper == NULL) {
if (cache_path != NULL) {
free(cache_path);
}
return LOGICAL_INVALID_INUM;
}
// Run the search
TSK_INUM_T last_assigned_inum = logical_fs_info->fs_info.root_inum;
// use last_assigned_inum variable on non-win32 builds to prevent error
(void)last_assigned_inum;
result = search_directory_recursive(logical_fs_info, starting_path, &starting_inum, search_helper);
if (cache_path != NULL) {
free(cache_path);
}
// Return the target inum if found
TSK_INUM_T target_inum;
if ((result != TSK_OK) || (!search_helper->target_found)) {
target_inum = LOGICAL_INVALID_INUM;
}
else {
target_inum = search_helper->found_inum;
}
free_search_helper(search_helper);
return target_inum;
}
static TSK_RETVAL_ENUM
logicalfs_dir_open_meta(TSK_FS_INFO *a_fs, TSK_FS_DIR ** a_fs_dir,
TSK_INUM_T a_addr, int recursion_depth)
{
TSK_FS_DIR *fs_dir;
LOGICALFS_INFO *logical_fs_info = (LOGICALFS_INFO*)a_fs;
if (a_fs_dir == NULL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logicalfs_dir_open_meta: NULL fs_dir argument given");
return TSK_ERR;
}
if ((a_addr & 0xffff) != 0) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logicalfs_dir_open_meta: Inode %" PRIuINUM " is not a directory", a_addr);
return TSK_ERR;
}
if (a_addr == LOGICAL_INVALID_INUM) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logicalfs_dir_open_meta: Inode %" PRIuINUM " is not valid", a_addr);
return TSK_ERR;
}
fs_dir = *a_fs_dir;
if (fs_dir) {
tsk_fs_dir_reset(fs_dir);
fs_dir->addr = a_addr;
}
else if ((*a_fs_dir = fs_dir = tsk_fs_dir_alloc(a_fs, a_addr, 128)) == NULL) {
return TSK_ERR;
}
// Load the base path for the given meta address
TSK_TCHAR* path = load_path_from_inum(logical_fs_info, a_addr);
if (path == NULL) {
return TSK_ERR;
}
#ifdef TSK_WIN32
// Populate the fs_file field
WIN32_FIND_DATAW fd;
HANDLE hFind;
if (TSTRLEN(path) < MAX_PATH) {
hFind = ::FindFirstFileW(path, &fd);
}
else {
TCHAR absPath[LOGICAL_MAX_PATH_UNICODE + 4];
TSTRNCPY(absPath, L"\\\\?\\", 4);
int absPathLen = GetFullPathNameW(path, LOGICAL_MAX_PATH_UNICODE, &(absPath[4]), NULL);
if (absPathLen <= 0) {
free(path);
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("logicalfs_dir_open_meta: Error looking up contents of directory (path too long) %" PRIttocTSK, path);
return TSK_ERR;
}
hFind = ::FindFirstFileW(absPath, &fd);
}
if (hFind != INVALID_HANDLE_VALUE) {
if ((fs_dir->fs_file = tsk_fs_file_alloc(a_fs)) == NULL) {
free(path);
return TSK_ERR;
}
if ((fs_dir->fs_file->meta = tsk_fs_meta_alloc(0)) == NULL) {
free(path);
return TSK_ERR;
}
TSK_RETVAL_ENUM result = populate_fs_file_from_win_find_data(&fd, fs_dir->fs_file);
::FindClose(hFind);
if (result != TSK_OK) {
// Error message already set
return TSK_ERR;
}
}
else {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("logicalfs_dir_open_meta: Error loading directory %" PRIttocTSK, path);
free(path);
return TSK_ERR;
}
#endif
#ifdef TSK_WIN32
vector<wstring> file_names;
vector<wstring> dir_names;
if (TSK_OK != load_dir_and_file_lists_win(path, file_names, dir_names, LOGICALFS_LOAD_ALL)) {
// Error message already set
free(path);
return TSK_ERR;
}
#else
vector<string> file_names;
vector<string> dir_names;
#endif
// Sort the files and directories
sort(file_names.begin(), file_names.end());
sort(dir_names.begin(), dir_names.end());
// Add the folders
for (auto it = begin(dir_names); it != end(dir_names); ++it) {
TSK_INUM_T dir_inum = get_inum_from_directory_path(logical_fs_info, path, *it);
if (dir_inum == LOGICAL_INVALID_INUM) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("logicalfs_dir_open_meta: Error looking up inum from path");
return TSK_ERR;
}
TSK_FS_NAME *fs_name;
#ifdef TSK_WIN32
char *utf8Name = convert_wide_string_to_utf8(it->c_str());
if (utf8Name == NULL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNICODE);
tsk_error_set_errstr("logicalfs_dir_open_meta: Error converting wide string");
return TSK_ERR;
}
size_t name_len = strlen(utf8Name);
#else
size_t name_len = strlen(it->c_str());
#endif
if ((fs_name = tsk_fs_name_alloc(name_len, 0)) == NULL) {
#ifdef TSK_WIN32
free(utf8Name);
#endif
free(path);
return TSK_ERR;
}
fs_name->type = TSK_FS_NAME_TYPE_DIR;
fs_name->flags = TSK_FS_NAME_FLAG_ALLOC;
fs_name->par_addr = a_addr;
fs_name->meta_addr = dir_inum;
#ifdef TSK_WIN32
strncpy(fs_name->name, utf8Name, name_len);
free(utf8Name);
#else
strncpy(fs_name->name, it->c_str(), name_len);
#endif
if (tsk_fs_dir_add(fs_dir, fs_name)) {
tsk_fs_name_free(fs_name);
free(path);
return TSK_ERR;
}
tsk_fs_name_free(fs_name);
}
free(path);
// Add the files
TSK_INUM_T file_inum = a_addr | 1; // First inum is directory inum in the high part, 1 in the low part
for (auto it = begin(file_names); it != end(file_names); ++it) {
TSK_FS_NAME *fs_name;
size_t name_len;
#ifdef TSK_WIN32
char *utf8Name = convert_wide_string_to_utf8(it->c_str());
if (utf8Name == NULL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNICODE);
tsk_error_set_errstr("logicalfs_dir_open_meta: Error converting wide string");
return TSK_ERR;
}
name_len = strlen(utf8Name);
#else
name_len = it->length();
#endif
if ((fs_name = tsk_fs_name_alloc(name_len, 0)) == NULL) {
#ifdef TSK_WIN32
free(utf8Name);
#endif
return TSK_ERR;
}
fs_name->type = TSK_FS_NAME_TYPE_REG;
fs_name->flags = TSK_FS_NAME_FLAG_ALLOC;
fs_name->par_addr = a_addr;
fs_name->meta_addr = file_inum;
#ifdef TSK_WIN32
strncpy(fs_name->name, utf8Name, name_len);
free(utf8Name);
#else
strncpy(fs_name->name, it->c_str(), name_len);
#endif
if (tsk_fs_dir_add(fs_dir, fs_name)) {
tsk_fs_name_free(fs_name);
return TSK_ERR;
}
tsk_fs_name_free(fs_name);
file_inum++;
}
return TSK_OK;
}
static uint8_t
logicalfs_load_attrs(TSK_FS_FILE *file)
{
if (file == NULL || file->meta == NULL || file->fs_info == NULL)
{
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr
("logicalfs_load_attrs: called with NULL pointers");
return 1;
}
TSK_FS_META* meta = file->meta;
// See if we have already loaded the runs
if ((meta->attr != NULL)
&& (meta->attr_state == TSK_FS_META_ATTR_STUDIED)) {
return 0;
}
else if (meta->attr_state == TSK_FS_META_ATTR_ERROR) {
return 1;
}
else if (meta->attr != NULL) {
tsk_fs_attrlist_markunused(meta->attr);
}
else if (meta->attr == NULL) {
meta->attr = tsk_fs_attrlist_alloc();
}
TSK_FS_ATTR_RUN *data_run;
TSK_FS_ATTR *attr = tsk_fs_attrlist_getnew(meta->attr, TSK_FS_ATTR_NONRES);
if (attr == NULL) {
meta->attr_state = TSK_FS_META_ATTR_ERROR;
return 1;
}
if (meta->size == 0) {
data_run = NULL;
}
else {
data_run = tsk_fs_attr_run_alloc();
if (data_run == NULL) {
meta->attr_state = TSK_FS_META_ATTR_ERROR;
return 1;
}
data_run->next = NULL;
data_run->offset = 0;
data_run->addr = 0;
data_run->len = (meta->size + file->fs_info->block_size - 1) / file->fs_info->block_size;
data_run->flags = TSK_FS_ATTR_RUN_FLAG_NONE;
}
if (tsk_fs_attr_set_run(file, attr, NULL, NULL,
TSK_FS_ATTR_TYPE_DEFAULT, TSK_FS_ATTR_ID_DEFAULT,
meta->size, meta->size,
roundup(meta->size, file->fs_info->block_size),
(TSK_FS_ATTR_FLAG_ENUM)0, 0)) {
meta->attr_state = TSK_FS_META_ATTR_ERROR;
return 1;
}
// If the file has size zero, return now
if (meta->size == 0) {
meta->attr_state = TSK_FS_META_ATTR_STUDIED;
return 0;
}
// Otherwise add the data run
if (0 != tsk_fs_attr_add_run(file->fs_info, attr, data_run)) {
return 1;
}
meta->attr_state = TSK_FS_META_ATTR_STUDIED;
return 0;
}
/*
* Reads a block from a logical file. If the file is not long enough to complete the block,
* null bytes are padded on to the end of the bytes read.
*
* @param a_fs File system
* @param a_fs_file File being read
* @param a_offset Starting offset
* @param buf Holds bytes read from the file (should be the size of a block)
*
* @return Size of the block or -1 on error.
*/
ssize_t
logicalfs_read_block(TSK_FS_INFO *a_fs, TSK_FS_FILE *a_fs_file, TSK_DADDR_T a_block_num, char *buf) {
if ((a_fs == NULL) || (a_fs_file == NULL) || (a_fs_file->meta == NULL)) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logical_fs_read_block: Called with null arguments");
return -1;
}
if (a_fs->ftype != TSK_FS_TYPE_LOGICAL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logical_fs_read_block: Called with files system that is not TSK_FS_TYPE_LOGICAL");
return -1;
}
unsigned int block_size = a_fs->block_size;
// The caching used for logical file blocks is simpler than
// the version for images in img_io.c because we will always store complete
// blocks - the block size for logical files is set to the same size as
// the image cache. So each block in the cache will correspond to a
// file inum and block number.
// cache_lock is used for both the cache in IMG_INFO and
// the shared variables in the img type specific INFO structs.
// Grab it now so that it is held before any reads.
IMG_LOGICAL_INFO* logical_img_info = (IMG_LOGICAL_INFO*)a_fs->img_info;
TSK_IMG_INFO* img_info = a_fs->img_info;
LOGICALFS_INFO *logical_fs_info = (LOGICALFS_INFO*)a_fs;
tsk_take_lock(&(img_info->cache_lock));
// Check if this block is in the cache
int cache_next = 0; // index to lowest age cache (to use next)
bool match_found = 0;
for (int cache_index = 0; cache_index < TSK_IMG_INFO_CACHE_NUM; cache_index++) {
// Look into the in-use cache entries
if (img_info->cache_len[cache_index] > 0) {
if ((logical_img_info->cache_inum[cache_index] == a_fs_file->meta->addr)
// check if non-negative and cast to uint to avoid signed/unsigned comparison warning
&& (img_info->cache_off[cache_index] >= 0 && (TSK_DADDR_T)img_info->cache_off[cache_index] == a_block_num)) {
// We found it
memcpy(buf, img_info->cache[cache_index], block_size);
match_found = true;
// reset its "age" since it was useful
img_info->cache_age[cache_index] = LOGICAL_IMG_CACHE_AGE;
// we don't break out of the loop so that we update all ages
}
else {
// Decrease its "age" since it was not useful.
// We don't let used ones go below 1 so that they are not
// confused with entries that have never been used.
if (img_info->cache_age[cache_index] > 2) {
img_info->cache_age[cache_index]--;
}
// See if this is the most eligible replacement
if ((img_info->cache_len[cache_next] > 0)
&& (img_info->cache_age[cache_index] <
img_info->cache_age[cache_next])) {
cache_next = cache_index;
}
}
}
}
// If we found the block in the cache, we're done
if (match_found) {
tsk_release_lock(&(img_info->cache_lock));
return block_size;
}
// See if this file is already open
LOGICAL_FILE_HANDLE_CACHE* file_handle_entry = NULL;
for (int i = 0; i < LOGICAL_FILE_HANDLE_CACHE_LEN; i++) {
if (logical_img_info->file_handle_cache[i].inum == a_fs_file->meta->addr) {
// File is already open
file_handle_entry = &(logical_img_info->file_handle_cache[i]);
}
}
// If we didn't find it, open the file and save to the cache
if (file_handle_entry == NULL) {
// Load the path
TSK_TCHAR* path = load_path_from_inum(logical_fs_info, a_fs_file->meta->addr);
#ifdef TSK_WIN32
// Open the file
HANDLE fd;
if (TSTRLEN(path) < MAX_PATH) {
fd = CreateFileW(path, FILE_READ_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
NULL);
}
else {
TCHAR absPath[LOGICAL_MAX_PATH_UNICODE + 4];
TSTRNCPY(absPath, L"\\\\?\\", 4);
int absPathLen = GetFullPathNameW(path, LOGICAL_MAX_PATH_UNICODE, &(absPath[4]), NULL);
if (absPathLen <= 0) {
free(path);
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_GENFS);
tsk_error_set_errstr("logicalfs_read_block: Error looking up contents of directory (path too long) %" PRIttocTSK, path);
return TSK_ERR;
}
fd = CreateFileW(absPath, FILE_READ_DATA,
FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, 0,
NULL);
}
if (fd == INVALID_HANDLE_VALUE) {
tsk_release_lock(&(img_info->cache_lock));
int lastError = (int)GetLastError();
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_READ);
tsk_error_set_errstr("logical_fs_read_block: file \"%" PRIttocTSK
"\" - %d", path, lastError);
return -1;
}
#else
int fd = 0;
// use path variable on non-win32 builds to prevent error
(void)path;
#endif
// Set up this cache entry
file_handle_entry = &(logical_img_info->file_handle_cache[logical_img_info->next_file_handle_cache_slot]);
if (file_handle_entry->fd != 0) {
// Close the current file handle
#ifdef TSK_WIN32
CloseHandle(file_handle_entry->fd);
#endif
}
file_handle_entry->fd = fd;
file_handle_entry->inum = a_fs_file->meta->addr;
file_handle_entry->seek_pos = 0;
// Set up the next cache entry to use
logical_img_info->next_file_handle_cache_slot++;
if (logical_img_info->next_file_handle_cache_slot >= LOGICAL_FILE_HANDLE_CACHE_LEN) {
logical_img_info->next_file_handle_cache_slot = 0;
}
}
// Seek to the starting offset (if necessary)
TSK_OFF_T offset_to_read = a_block_num * block_size;
if (offset_to_read != file_handle_entry->seek_pos) {
#ifdef TSK_WIN32
LARGE_INTEGER li;
li.QuadPart = a_block_num * block_size;
li.LowPart = SetFilePointer(file_handle_entry->fd, li.LowPart,
&li.HighPart, FILE_BEGIN);
if ((li.LowPart == INVALID_SET_FILE_POINTER) &&
(GetLastError() != NO_ERROR)) {
tsk_release_lock(&(img_info->cache_lock));
int lastError = (int)GetLastError();
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_IMG_SEEK);
tsk_error_set_errstr("logical_fs_read_block: file addr %" PRIuINUM
" offset %" PRIdOFF " seek - %d",
a_fs_file->meta->addr, a_block_num, lastError);
return -1;
}
#endif
file_handle_entry->seek_pos = offset_to_read;
}
// Read the data
unsigned int len_to_read;
if (((a_block_num + 1) * block_size) <= (unsigned long long)a_fs_file->meta->size) {
// If the file is large enough to read the entire block, then try to do so
len_to_read = block_size;
}
else {
// Otherwise, we expect to only be able to read a smaller number of bytes
len_to_read = a_fs_file->meta->size % block_size;
memset(buf, 0, block_size);
}
#ifdef TSK_WIN32
DWORD nread;
if (FALSE == ReadFile(file_handle_entry->fd, buf, (DWORD)len_to_read, &nread, NULL)) {
tsk_release_lock(&(img_info->cache_lock));
int lastError = GetLastError();
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_IMG_READ);
tsk_error_set_errstr("logicalfs_read_block: file addr %" PRIuINUM
" offset: %" PRIu64 " read len: %" PRIuSIZE " - %d",
a_fs_file->meta->addr, a_block_num, block_size,
lastError);
return -1;
}
file_handle_entry->seek_pos += nread;
#else
// otherwise, not used; ensure used to prevent warning
(void)len_to_read;
#endif
// Copy the block into the cache
memcpy(img_info->cache[cache_next], buf, block_size);
img_info->cache_len[cache_next] = block_size;
img_info->cache_age[cache_next] = LOGICAL_IMG_CACHE_AGE;
img_info->cache_off[cache_next] = a_block_num;
logical_img_info->cache_inum[cache_next] = a_fs_file->meta->addr;
tsk_release_lock(&(img_info->cache_lock));
// If we didn't read the expected number of bytes, return an error
#ifdef TSK_WIN32
if (nread != len_to_read) {
int lastError = GetLastError();
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_IMG_READ);
tsk_error_set_errstr("logicalfs_read_block: file addr %" PRIuINUM
" offset: %" PRIdOFF " read len: %" PRIuSIZE " - %d",
a_fs_file->meta->addr, a_block_num, block_size,
lastError);
return -1;
}
#endif
return block_size;
}
/*
* Reads data from a logical file.
*
* @param a_fs File system
* @param a_fs_file File being read
* @param a_offset Starting offset
* @param a_len Length to read
* @param a_buf Holds bytes read from the file (should have length at least a_len)
*
* @return Number of bytes read or -1 on error.
*/
ssize_t
logicalfs_read(TSK_FS_INFO *a_fs, TSK_FS_FILE *a_fs_file, TSK_DADDR_T a_offset, size_t a_len, char *a_buf) {
TSK_DADDR_T current_block_num = a_offset / a_fs->block_size;
char block_buffer[LOGICAL_BLOCK_SIZE];
size_t cnt;
char *dest = a_buf;
size_t bytes_left = a_len;
size_t bytes_read = 0;
size_t filler_len = 0;
if ((a_fs == NULL) || (a_fs_file == NULL) || (a_fs_file->meta == NULL)) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logicalfs_read: Called with null arguments");
return -1;
}
if (a_offset >= (TSK_DADDR_T)a_fs_file->meta->size) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logicalfs_read: Attempted to read offset beyond end of file (file addr: %"
PRIuINUM ", file size: %" PRIdOFF ", offset: %" PRIuDADDR ")", a_fs_file->meta->addr, a_fs_file->meta->size, a_offset);
return -1;
}
// Only attempt to read to the end of the file at most
if (a_offset + a_len > (TSK_DADDR_T)a_fs_file->meta->size) {
bytes_left = a_fs_file->meta->size - a_offset;
filler_len = a_offset + a_len - a_fs_file->meta->size;
// Fill in the end of the buffer
if (filler_len > 0) {
memset(dest + bytes_left, 0, filler_len);
}
}
// Read bytes prior to the first block boundary
if (a_offset % a_fs->block_size != 0) {
// Read in the smaller of the requested length and the bytes at the end of the block
size_t len_to_read = a_fs->block_size - (a_offset % a_fs->block_size);
if (len_to_read > bytes_left) {
len_to_read = bytes_left;
}
cnt = logicalfs_read_block(a_fs, a_fs_file, current_block_num, block_buffer);
if (cnt != a_fs->block_size) {
// Error already set
return cnt;
}
memcpy(dest, block_buffer + (a_offset % a_fs->block_size), len_to_read);
dest += len_to_read;
bytes_read += len_to_read;
bytes_left -= len_to_read;
current_block_num++;
}
// Check if we're done
if (bytes_left == 0) {
return bytes_read;
}
// Read complete blocks
while (bytes_left >= a_fs->block_size) {
cnt = logicalfs_read_block(a_fs, a_fs_file, current_block_num, dest);
if (cnt != a_fs->block_size) {
// Error already set
return cnt;
}
dest += a_fs->block_size;
bytes_read += a_fs->block_size;
bytes_left -= a_fs->block_size;
current_block_num++;
}
// Check if we're done
if (bytes_left == 0) {
return bytes_read;
}
// Read the final, incomplete block
cnt = logicalfs_read_block(a_fs, a_fs_file, current_block_num, block_buffer);
if (cnt != a_fs->block_size) {
// Error already set
return cnt;
}
memcpy(dest, block_buffer, bytes_left);
dest += bytes_left;
bytes_read += bytes_left;
return bytes_read;
}
/**
* Print details about the file system to a file handle.
*
* @param fs File system to print details on
* @param hFile File handle to print text to
*
* @returns 1 on error and 0 on success
*/
static uint8_t
logicalfs_fsstat(TSK_FS_INFO * fs, FILE * hFile)
{
LOGICALFS_INFO * dirfs = (LOGICALFS_INFO*)fs;
tsk_fprintf(hFile, "FILE SYSTEM INFORMATION\n");
tsk_fprintf(hFile, "--------------------------------------------\n");
tsk_fprintf(hFile, "File System Type: Logical Directory\n");
tsk_fprintf(hFile,
"Base Directory Path: %" PRIttocTSK "\n",
dirfs->base_path);
return 0;
}
static uint8_t
logicalfs_fscheck(TSK_FS_INFO * /*fs*/, FILE * /*hFile*/)
{
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNSUPFUNC);
tsk_error_set_errstr("fscheck not supported for logical file systems");
return 1;
}
/**
* Print details on a specific file to a file handle.
*
* @param fs File system file is located in
* @param hFile File handle to print text to
* @param inum Address of file in file system
* @param numblock The number of blocks in file to force print (can go beyond file size)
* @param sec_skew Clock skew in seconds to also print times in
*
* @returns 1 on error and 0 on success
*/
static uint8_t
logicalfs_istat(TSK_FS_INFO *fs, TSK_FS_ISTAT_FLAG_ENUM flags, FILE * hFile, TSK_INUM_T inum,
TSK_DADDR_T numblock, int32_t sec_skew)
{
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNSUPFUNC);
tsk_error_set_errstr("istat not supported for logical file systems");
return 1;
}
/* logicalfs_close - close a logical file system */
static void
logicalfs_close(TSK_FS_INFO *fs)
{
if (fs != NULL) {
fs->tag = 0;
tsk_fs_free(fs);
}
}
static uint8_t
logicalfs_jentry_walk(TSK_FS_INFO * /*info*/, int /*entry*/,
TSK_FS_JENTRY_WALK_CB /*cb*/, void * /*fn*/)
{
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNSUPFUNC);
tsk_error_set_errstr("Journal support for logical directory is not implemented");
return 1;
}
static uint8_t
logicalfs_jblk_walk(TSK_FS_INFO * /*info*/, TSK_DADDR_T /*daddr*/,
TSK_DADDR_T /*daddrt*/, int /*entry*/, TSK_FS_JBLK_WALK_CB /*cb*/,
void * /*fn*/)
{
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNSUPFUNC);
tsk_error_set_errstr("Journal support for logical directory is not implemented");
return 1;
}
static uint8_t
logicalfs_jopen(TSK_FS_INFO * /*info*/, TSK_INUM_T /*inum*/)
{
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_UNSUPFUNC);
tsk_error_set_errstr("Journal support for logical directory is not implemented");
return 1;
}
int
logicalfs_name_cmp(TSK_FS_INFO * a_fs_info, const char *s1, const char *s2)
{
#ifdef TSK_WIN32
return strcasecmp(s1, s2);
#else
return tsk_fs_unix_name_cmp(a_fs_info, s1, s2);
#endif
}
TSK_FS_INFO *
logical_fs_open(TSK_IMG_INFO * img_info) {
LOGICALFS_INFO *logical_fs_info = NULL;
TSK_FS_INFO *fs = NULL;
IMG_LOGICAL_INFO *logical_img_info = NULL;
#ifndef TSK_WIN32
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logical_fs_open: logical file systems currently only enabled on Windows");
return NULL;
#endif
if (img_info->itype != TSK_IMG_TYPE_LOGICAL) {
tsk_error_reset();
tsk_error_set_errno(TSK_ERR_FS_ARG);
tsk_error_set_errstr("logical_fs_open: image must be of type TSK_IMG_TYPE_DIR");
return NULL;
}
logical_img_info = (IMG_LOGICAL_INFO *)img_info;
if ((logical_fs_info = (LOGICALFS_INFO *)tsk_fs_malloc(sizeof(LOGICALFS_INFO))) == NULL)
return NULL;
fs = &(logical_fs_info->fs_info);
logical_fs_info->base_path = logical_img_info->base_path; // To avoid having to always go through TSK_IMG_INFO
fs->tag = TSK_FS_INFO_TAG;
fs->ftype = TSK_FS_TYPE_LOGICAL;
fs->flags = (TSK_FS_INFO_FLAG_ENUM)0;
fs->img_info = img_info;
fs->offset = 0;
fs->endian = TSK_LIT_ENDIAN;
fs->duname = "None";
// Metadata info
fs->last_inum = 0; // Will set at the end
fs->root_inum = LOGICAL_ROOT_INUM;
fs->first_inum = LOGICAL_ROOT_INUM;
fs->inum_count = 0;
// Block info
fs->dev_bsize = 0;
fs->block_size = LOGICAL_BLOCK_SIZE;
fs->block_pre_size = 0;
fs->block_post_size = 0;
fs->block_count = 0;
fs->first_block = 0;
fs->last_block = INT64_MAX;
fs->last_block_act = INT64_MAX;
// Set the generic function pointers. Most will be no-ops for now.
fs->inode_walk = logicalfs_inode_walk;
fs->block_walk = logicalfs_block_walk;
fs->block_getflags = logicalfs_block_getflags;
fs->get_default_attr_type = logicalfs_get_default_attr_type;
fs->load_attrs = logicalfs_load_attrs;
fs->file_add_meta = logicalfs_file_add_meta;
fs->dir_open_meta = logicalfs_dir_open_meta;
fs->fsstat = logicalfs_fsstat;
fs->fscheck = logicalfs_fscheck;
fs->istat = logicalfs_istat;
fs->name_cmp = logicalfs_name_cmp;
fs->close = logicalfs_close;
// Journal functions - also no-ops.
fs->jblk_walk = logicalfs_jblk_walk;
fs->jentry_walk = logicalfs_jentry_walk;
fs->jopen = logicalfs_jopen;
// Calculate the last inum
fs->last_inum = find_max_inum(logical_fs_info);
// We don't really care about the last inum, but if traversing the
// folders to calculate it fails then we're going to encounter
// the same error when using the logical file system.
if (fs->last_inum == LOGICAL_INVALID_INUM) {
logicalfs_close(fs);
return NULL;
}
return fs;
}
|