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
|
/* $NetBSD$ */
/*
* File "udfclient.c" is part of the UDFclient toolkit.
* File $Id: udfclient.c,v 1.104 2016/04/25 21:28:00 reinoud Exp $ $Name: $
*
* Copyright (c) 2003, 2004, 2005, 2006, 2011
* Reinoud Zandijk <reinoud@netbsd.org>
* All rights reserved.
*
* The UDFclient toolkit is distributed under the Clarified Artistic Licence.
* A copy of the licence is included in the distribution as
* `LICENCE.clearified.artistic' and a copy of the licence can also be
* requested at the GNU foundantion's website.
*
* Visit the UDFclient toolkit homepage http://www.13thmonkey.org/udftoolkit/
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <errno.h>
#include "udf.h"
#include "udf_bswap.h"
/* switches */
/* #define DEBUG(a) (a) */
#define DEBUG(a) if (0) { a; }
#ifndef MAX
#define MAX(a,b) ((a)>(b)?(a):(b))
#define MIN(a,b) ((a)<(b)?(a):(b))
#endif
/* include timeval to timespec conversion macro's for systems that don't provide them */
#ifndef TIMEVAL_TO_TIMESPEC
# define TIMEVAL_TO_TIMESPEC(tv, ts) do { \
(ts)->tv_sec = (tv)->tv_sec; \
(ts)->tv_nsec = (tv)->tv_usec * 1000; \
} while (/*CONSTCOND*/0)
#endif
#ifndef TIMESPEC_TO_TIMEVAL
# define TIMESPEC_TO_TIMEVAL(tv, ts) do { \
(tv)->tv_sec = (ts)->tv_sec; \
(tv)->tv_usec = (ts)->tv_nsec / 1000; \
} while (/*CONSTCOND*/0)
#endif
/* include the dump parts ... in order to get a more sane splitting */
extern void udf_dump_alive_sets(void);
/* globals */
extern int udf_verbose;
extern int uscsilib_verbose;
int read_only;
#define MAX_ARGS 100
struct curdir {
char *name;
struct udf_mountpoint *mountpoint; /* foreign */
struct udf_node *udf_node; /* foreign */
struct hash_entry *udf_mountpoint_entry; /* `current' mountpoint entry */
} curdir;
/*
* XXX
* FTP client; de volumes vooraan zetten in de VFS ; evt. in meerdere subdirs.
* general file name format
*
* /volset:pri:log/udfpath
* of
* /volset/pri/log/udfpath/
*
* XXX
*/
int udfclient_readdir(struct udf_node *udf_node, struct uio *result_uio, int *eof_res) {
struct dirent entry;
struct udf_mountpoint *mountable;
assert(result_uio);
if (!udf_node) {
/* mountables */
/* XXX result_uio needs to be long enough to hold all mountables!!!! XXX */
SLIST_FOREACH(mountable, &udf_mountables, all_next) {
strcpy(entry.d_name, mountable->mount_name);
entry.d_type = DT_DIR;
uiomove(&entry, sizeof(struct dirent), result_uio);
}
if (eof_res) *eof_res = 1;
return 0;
}
/* intree nodes : pass on to udf_readdir */
return udf_readdir(udf_node, result_uio, eof_res);
}
/* VOP_LOOKUP a-like */
int udfclient_lookup(struct udf_node *dir_node, struct udf_node **resnode, char *name) {
struct udf_mountpoint *mountable;
struct fileid_desc *fid;
struct long_ad udf_icbptr;
int lb_size, found, error;
assert(resnode);
assert(name);
*resnode = NULL;
if (!dir_node) {
/* mountables */
SLIST_FOREACH(mountable, &udf_mountables, all_next) {
if (strcmp(mountable->mount_name, name) == 0) {
/* found `root' of a mountable */
*resnode = mountable->rootdir_node;
return 0;
}
}
return ENOENT;
}
/* intree nodes : pass on to udf_lookup_name_in_dir */
lb_size = dir_node->udf_log_vol->lb_size;
fid = malloc(lb_size);
assert(fid);
error = udf_lookup_name_in_dir(dir_node, name, strlen(name), &udf_icbptr, fid, &found);
if (!error) {
error = ENOENT;
if (found)
error = udf_readin_udf_node(dir_node, &udf_icbptr, fid, resnode);
}
free(fid);
return error;
}
int udfclient_getattr(struct udf_node *udf_node, struct stat *stat) {
int error;
error = 0;
if (udf_node) {
error = udf_getattr(udf_node, stat);
/* print? */
if (error) fprintf(stderr, "Can't stat file\n");
} else {
/* dummy entry for `root' in VFS */
stat->st_mode = 0744 | S_IFDIR;
stat->st_size = 0;
stat->st_uid = 0;
stat->st_gid = 0;
}
return error;
}
/* higher level of lookup; walk tree */
/* results in a `held'/locked node upto `root' */
int udfclient_lookup_pathname(struct udf_node *cur_node, struct udf_node **res_node, char *restpath_given) {
struct udf_node *sub_node;
char *restpath, *next_element, *slashpos, *pathpos;
int error;
assert(restpath_given);
restpath = strdup(restpath_given);
/* start at root */
*res_node = NULL;
pathpos = restpath;
assert(*pathpos == '/');
pathpos++; /* strip leading '/' */
next_element = pathpos;
while (next_element && (strlen(next_element) > 0)) {
/* determine next slash position */
slashpos = strchr(next_element, '/');
if (slashpos) *slashpos++ = 0;
error = udfclient_lookup(cur_node, &sub_node, next_element);
if (error) {
free(restpath);
return error;
}
/* advance */
cur_node = sub_node;
next_element = slashpos;
}
/* we are at the end; return result */
*res_node = cur_node;
free(restpath);
return 0;
}
char *udfclient_realpath(char *cur_path, char *relpath, char **leaf) {
char *resultpath, *here, *pos;
resultpath = calloc(1, sizeof(cur_path) + sizeof(relpath) +1024);
assert(resultpath);
strcpy(resultpath, "/");
strcat(resultpath, cur_path);
strcat(resultpath, "/");
/* check if we are going back to `root' */
if (relpath && *relpath == '/') {
strcpy(resultpath, "");
}
strcat(resultpath, relpath);
/* now clean up the resulting string by removing double slashes */
here = resultpath;
while (*here) {
pos = here; while (strncmp(pos, "//", 2) == 0) pos++;
if (pos != here) strcpy(here, pos);
here++;
}
/* remove '.' and '..' sequences */
here = resultpath;
while (*here) {
/* printf("transformed to %s\n", resultpath); */
/* check for internal /./ and trailing /. */
if (strncmp(here, "/./", 3) == 0) {
strcpy(here+1, here + 3);
continue;
}
if (strcmp(here, "/.")==0) {
strcpy(here+1, here + 2);
continue;
}
if (strncmp(here, "/../", 4) == 0) {
strcpy(here+1, here + 4);
/* go for the parent */
pos = here-1; while (*pos && *pos != '/') pos--; pos++;
strcpy(pos, here+1);
here = pos;
continue;
}
if (strcmp(here, "/..")==0) {
strcpy(here+1, here + 3);
/* go for the parent */
pos = here-1; while (*pos && *pos != '/') pos--; pos++;
strcpy(pos, here+1);
here = pos;
continue;
}
here++;
}
if (leaf) {
/* find the leaf name */
here = resultpath;
while (*here) {
if (strncmp(here, "/", 1) == 0)
*leaf = here+1;
here++;
}
}
return resultpath;
}
static void print_dir_entry(struct udf_node *udf_node, char *name) {
struct stat stat;
uint64_t size;
int mode, this_mode, uid, gid;
int error;
error = udfclient_getattr(udf_node, &stat);
if (error) return;
size = stat.st_size;
mode = stat.st_mode;
uid = stat.st_uid;
gid = stat.st_gid;
if (mode & S_IFDIR) printf("d"); else printf("-");
mode = mode & 511;
this_mode = (mode >> 6) & 7;
printf("%c%c%c", "----rrrr"[this_mode & 4], "--www"[this_mode & 2], "-x"[this_mode & 1]);
this_mode = (mode >> 3) & 7;
printf("%c%c%c", "----rrrr"[this_mode & 4], "--www"[this_mode & 2], "-x"[this_mode & 1]);
this_mode = mode & 7;
printf("%c%c%c", "----rrrr"[this_mode & 4], "--www"[this_mode & 2], "-x"[this_mode & 1]);
printf(" %5d %5d %10"PRIu64" %s\n", gid, uid, size, name);
}
#define LS_SUBTREE_DIR_BUFFER_SIZE (16*1024)
void udfclient_ls(int args, char *arg1) {
struct udf_node *udf_node, *entry_node;
uint8_t *buffer;
struct uio dir_uio;
struct iovec dir_uiovec;
struct dirent *dirent;
struct stat stat;
uint32_t pos;
int eof;
char *node_name, *leaf_name;
int error;
if (args > 1) {
printf("Syntax: ls [file | dir]\n");
return;
}
if (args == 0) arg1 = "";
node_name = udfclient_realpath(curdir.name, arg1, &leaf_name);
error = udfclient_lookup_pathname(NULL, &udf_node, node_name);
if (error) {
fprintf(stderr, "%s : %s\n", arg1, strerror(error));
free(node_name);
return;
}
error = udfclient_getattr(udf_node, &stat);
if (stat.st_mode & S_IFDIR) {
printf("Directory listing of %s\n", udf_node ? leaf_name : "/");
/* start at the start of the directory */
dir_uio.uio_offset = 0;
dir_uio.uio_iov = &dir_uiovec;
dir_uio.uio_iovcnt = 1;
buffer = calloc(1, LS_SUBTREE_DIR_BUFFER_SIZE);
if (!buffer) return;
do {
dir_uiovec.iov_base = buffer;
dir_uiovec.iov_len = LS_SUBTREE_DIR_BUFFER_SIZE;
dir_uio.uio_resid = LS_SUBTREE_DIR_BUFFER_SIZE;
dir_uio.uio_rw = UIO_WRITE;
error = udfclient_readdir(udf_node, &dir_uio, &eof);
if (error) {
fprintf(stderr, "error during readdir: %s\n", strerror(error));
break;
}
pos = 0;
while (pos < LS_SUBTREE_DIR_BUFFER_SIZE - dir_uio.uio_resid) {
dirent = (struct dirent *) (buffer + pos);
error = udfclient_lookup(udf_node, &entry_node, dirent->d_name);
print_dir_entry(entry_node, dirent->d_name);
pos += sizeof(struct dirent);
}
} while (!eof);
free(buffer);
} else {
print_dir_entry(udf_node, leaf_name);
}
free(node_name);
}
#undef LS_SUBTREE_DIR_BUFFER_SIZE
void udfclient_pwd(int args) {
char pwd[1024];
char *res;
if (args) {
printf("Syntax: pwd\n");
return;
}
res = getcwd(pwd, 1024);
assert(res);
printf("UDF working directory is %s\n", curdir.name);
printf("Current FS working directory %s\n", pwd);
}
static void udfclient_print_free_amount(char *prefix, uint64_t value, uint64_t max_value) {
printf("%s %10"PRIu64" Kb (%3"PRIu64" %%) (%8.2f Mb) (%5.2f Gb)\n",
prefix, value/1024, (100*value)/max_value, (double) value/(1024*1024), (double) value/(1024*1024*1024));
}
void udfclient_free(int args) {
struct udf_part_mapping *part_mapping;
struct udf_partition *udf_partition;
struct udf_log_vol *udf_log_vol;
struct logvol_desc *lvd;
uint64_t part_size, unalloc_space, freed_space;
uint64_t total_space, free_space, await_alloc_space;
uint32_t lb_size;
int part_num;
if (args) {
printf("Syntax: free\n");
return;
}
if (!curdir.udf_node || !(udf_log_vol = curdir.udf_node->udf_log_vol)) {
printf("Can only report free space in UDF mountpoints\n");
return;
}
lb_size = udf_log_vol->lb_size;
// sector_size = udf_log_vol->sector_size;
lvd = udf_log_vol->log_vol;
udf_dump_id("Logical volume ", 128, lvd->logvol_id, &lvd->desc_charset);
total_space = udf_log_vol->total_space;
free_space = udf_log_vol->free_space;
await_alloc_space = udf_log_vol->await_alloc_space;
SLIST_FOREACH(part_mapping, &udf_log_vol->part_mappings, next_mapping) {
part_num = part_mapping->udf_virt_part_num;
udf_logvol_vpart_to_partition(udf_log_vol, part_num, NULL, &udf_partition);
assert(udf_partition);
unalloc_space = udf_partition->free_unalloc_space;
freed_space = udf_partition->free_freed_space;
part_size = udf_partition->part_length;
switch (part_mapping->udf_part_mapping_type) {
case UDF_PART_MAPPING_PHYSICAL :
printf("\tphysical partition %d\n", part_num);
printf("\t\t%8"PRIu64" K (%"PRIu64" pages) size\n", part_size/1024, part_size / lb_size);
printf("\t\t%8"PRIu64" K (%"PRIu64" pages) unallocated\n", unalloc_space/1024, unalloc_space / lb_size);
printf("\t\t%8"PRIu64" K (%"PRIu64" pages) freed\n", freed_space/1024, freed_space / lb_size);
break;
case UDF_PART_MAPPING_VIRTUAL :
printf("\tvirtual partition mapping %d\n", part_num);
printf("\t\tnot applicable\n");
break;
case UDF_PART_MAPPING_SPARABLE :
printf("\tsparable partition %d\n", part_num);
printf("\t\t%8"PRIu64" K (%"PRIu64" pages) size\n", part_size/1024, part_size / lb_size);
printf("\t\t%8"PRIu64" K (%"PRIu64" pages) unallocated\n", unalloc_space/1024, unalloc_space / lb_size);
printf("\t\t%8"PRIu64" K (%"PRIu64" pages) freed\n", freed_space/1024, freed_space / lb_size);
break;
case UDF_PART_MAPPING_META :
printf("\tmetadata 'partition' %d\n", part_num);
printf("\t\t%8"PRIu64" K (%"PRIu64" pages) unallocated\n", unalloc_space/1024, unalloc_space / lb_size);
printf("\t\t%8"PRIu64" K (%"PRIu64" pages) freed\n", freed_space/1024, freed_space / lb_size);
break;
case UDF_PART_MAPPING_ERROR :
printf("\terror partiton %d\n", part_num);
break;
default:
break;
}
}
printf("\n");
udfclient_print_free_amount("\tConfirmed free space ", free_space, total_space);
udfclient_print_free_amount("\tAwaiting allocation ", await_alloc_space, total_space);
udfclient_print_free_amount("\tEstimated free space ", free_space - await_alloc_space, total_space);
udfclient_print_free_amount("\tEstimated total used ", total_space - free_space + await_alloc_space, total_space);
printf("\n");
udfclient_print_free_amount("\tTotal size ", total_space, total_space);
}
void udfclient_cd(int args, char *arg1) {
struct udf_node *udf_node;
struct stat stat;
char *node_name, *new_curdir_name;
int error;
if (args > 1) {
printf("Syntax: cd [dir]\n");
return;
}
new_curdir_name = udfclient_realpath(curdir.name, arg1, NULL);
node_name = strdup(new_curdir_name); /* working copy */
error = udfclient_lookup_pathname(NULL, &udf_node, node_name);
if (error) {
fprintf(stderr, "%s : %s\n", arg1, strerror(error));
free(new_curdir_name);
free(node_name);
return;
}
error = udfclient_getattr(udf_node, &stat);
if (stat.st_mode & S_IFDIR) {
free(curdir.name);
curdir.name = new_curdir_name;
curdir.udf_node = udf_node;
free(node_name);
udfclient_pwd(0);
} else {
fprintf(stderr, "%s is not a directory\n", arg1);
free(new_curdir_name);
free(node_name);
}
}
void udfclient_lcd(int args, char *arg1) {
char pwd[1024];
char *res;
if (args > 1) {
printf("Syntax: lcd [dir]\n");
return;
}
if (strcmp(arg1, "" )==0) arg1 = getenv("HOME");
if (strcmp(arg1, "~")==0) arg1 = getenv("HOME");
if (chdir(arg1)) {
fprintf(stderr, "While trying to go to %s :", arg1);
perror("");
}
res = getcwd(pwd, 1024);
assert(res);
printf("Changing local directory to %s\n", pwd);
}
void udfclient_lls(int args) {
if (args) {
printf("Syntax: lls\n");
return;
}
if (system("/bin/ls")) {
perror("While listing current directory\n");
}
}
uint64_t getmtime(void) {
struct timeval tp;
gettimeofday(&tp, NULL);
return 1000000*tp.tv_sec + tp.tv_usec;
}
int udfclient_get_file(struct udf_node *udf_node, char *fullsrcname, char *fulldstname) {
struct uio file_uio;
struct iovec file_iov;
struct stat stat;
struct timeval times[2];
uint64_t file_length;
uint64_t start, now, then, eta;
uint64_t cur_speed, avg_speed, data_transfered;
uint64_t file_block_size, file_transfer_size, written;
uint8_t *file_block;
char cur_txt[32], avg_txt[32], eta_txt[32];
int fileh, len;
int notok, error;
assert(udf_node);
assert(fullsrcname);
assert(strlen(fullsrcname) >= 1);
error = 0;
/* terminal directory node? */
error = udfclient_getattr(udf_node, &stat);
if (stat.st_mode & S_IFDIR) {
len = strlen(fulldstname);
if (strcmp(fulldstname + len -2, "/.") == 0)
fulldstname[len-2] = 0;
if (strcmp(fulldstname + len -3, "/..") == 0)
return 0;
error = mkdir(fulldstname, (udf_node->stat.st_mode) & 07777);
if (!error) {
/* set owner attribute and times; access permissions allready set on creation.*/
notok = chown(fulldstname, stat.st_uid, stat.st_gid);
if (notok && (udf_verbose > UDF_VERBLEV_ACTIONS))
fprintf(stderr, "failed to set owner of directory, ignoring\n");
TIMESPEC_TO_TIMEVAL(×[0], &stat.st_atimespec); /* access time */
TIMESPEC_TO_TIMEVAL(×[1], &stat.st_mtimespec); /* modification time */
notok = utimes(fulldstname, times);
if (notok)
fprintf(stderr, "failed to set times on directory, ignoring\n");
}
if (error)
fprintf(stderr, "While creating directory `%s' : %s\n", fulldstname, strerror(errno));
return 0;
}
/* terminal file node; setting mode correctly */
fileh = open(fulldstname, O_WRONLY | O_CREAT | O_TRUNC, udf_node->stat.st_mode);
if (fileh >= 0) {
file_length = udf_node->stat.st_size;
file_block_size = 256*1024; /* block read in length */
file_block = malloc(file_block_size);
if (!file_block) {
printf("Out of memory claiming file buffer\n");
return ENOMEM;
}
/* move to uio_newuio(struct uio *uio) with fixed length uio_iovcnt? */
bzero(&file_uio, sizeof(struct uio));
file_uio.uio_rw = UIO_WRITE; /* WRITE into this space */
file_uio.uio_iovcnt = 1;
file_uio.uio_iov = &file_iov;
start = getmtime();
then = now = start;
eta = data_transfered = 0;
strcpy(avg_txt, "---"); strcpy(cur_txt, "---"); strcpy(eta_txt, "---");
file_uio.uio_offset = 0; /* begin at the start */
do {
/* fill in IO vector space; reuse blob file_block over and over */
file_transfer_size = MIN(file_block_size, file_length - file_uio.uio_offset);
file_uio.uio_resid = file_transfer_size;
file_uio.uio_iov->iov_base = file_block;
file_uio.uio_iov->iov_len = file_block_size;
error = udf_read_file_part_uio(udf_node, fullsrcname, UDF_C_USERDATA, &file_uio);
if (error) {
fprintf(stderr, "While retrieving file block : %s\n", strerror(error));
printf("\n\n\n"); /* XXX */
break;
}
written = write(fileh, file_block, file_transfer_size);
assert(written == file_transfer_size);
if ((getmtime() - now > 1000000) || ((uint64_t) file_uio.uio_offset >= file_length)) {
if (strlen(fulldstname) < 45) {
printf("\r%-45s ", fulldstname);
} else {
printf("\r...%-42s ", fulldstname + strlen(fulldstname)-42);
}
printf("%10"PRIu64" / %10"PRIu64" bytes ", (uint64_t) file_uio.uio_offset, (uint64_t) file_length);
if (file_length) printf("(%3d%%) ", (int) (100.0*(float) file_uio.uio_offset / file_length));
then = now;
now = getmtime();
cur_speed = 0;
avg_speed = 0;
if (now-start > 0) avg_speed = (1000000 * file_uio.uio_offset) / (now-start);
if (now-then > 0) cur_speed = (1000000 * (file_uio.uio_offset - data_transfered)) / (now-then);
if (avg_speed > 0) eta = (file_length - file_uio.uio_offset) / avg_speed;
data_transfered = file_uio.uio_offset;
strcpy(avg_txt, "---"); strcpy(cur_txt, "---"); strcpy(eta_txt, "---");
if (avg_speed > 0) sprintf(avg_txt, "%d", (int32_t) avg_speed/1000);
if (cur_speed > 0) sprintf(cur_txt, "%d", (int32_t) cur_speed/1000);
if (eta > 0) sprintf(eta_txt, "%02d:%02d:%02d", (int) (eta/3600), (int) (eta/60) % 60, (int) eta % 60);
printf("%6s KB/s (%6s KB/s) ETA %s", avg_txt, cur_txt, eta_txt);
fflush(stdout);
}
} while ((uint64_t) file_uio.uio_offset < file_length);
printf(" finished\n");
free(file_block);
/* set owner attribute and times; access permissions allready set on creation.*/
notok = fchown(fileh, stat.st_uid, stat.st_gid);
if (notok && (udf_verbose > UDF_VERBLEV_ACTIONS))
fprintf(stderr, "failed to set owner of file, ignoring\n");
TIMESPEC_TO_TIMEVAL(×[0], &stat.st_atimespec); /* access time */
TIMESPEC_TO_TIMEVAL(×[1], &stat.st_mtimespec); /* modification time */
notok = futimes(fileh, times);
if (notok)
fprintf(stderr, "failed to set times on directory, ignoring\n");
close(fileh);
} else {
printf("Help! can't open file %s for output\n", fulldstname);
}
return error;
}
#define GET_SUBTREE_DIR_BUFFER_SIZE (16*1024)
void udfclient_get_subtree(struct udf_node *udf_node, char *srcprefix, char *dstprefix, int recurse, uint64_t *total_size) {
struct uio dir_uio;
struct iovec dir_iovec;
uint8_t *buffer;
uint32_t pos;
char fullsrcpath[1024], fulldstpath[1024]; /* XXX arbitrary length XXX */
struct dirent *dirent;
struct stat stat;
struct udf_node *entry_node;
struct fileid_desc *fid;
struct long_ad udf_icbptr;
int lb_size, eof;
int found, isdot, isdotdot, error;
if (!udf_node)
return;
udf_node->hold++;
error = udfclient_getattr(udf_node, &stat);
if ((stat.st_mode & S_IFDIR) && recurse) {
buffer = malloc(GET_SUBTREE_DIR_BUFFER_SIZE);
if (!buffer) {
udf_node->hold--;
return;
}
lb_size = udf_node->udf_log_vol->lb_size;
fid = malloc(lb_size);
assert(fid);
/* recurse into this directory */
dir_uio.uio_offset = 0; /* begin at start */
do {
dir_iovec.iov_base = buffer;
dir_iovec.iov_len = GET_SUBTREE_DIR_BUFFER_SIZE;
dir_uio.uio_resid = GET_SUBTREE_DIR_BUFFER_SIZE;
dir_uio.uio_iovcnt = 1;
dir_uio.uio_iov = &dir_iovec;
dir_uio.uio_rw = UIO_WRITE;
error = udf_readdir(udf_node, &dir_uio, &eof);
pos = 0;
while (pos < GET_SUBTREE_DIR_BUFFER_SIZE - dir_uio.uio_resid) {
dirent = (struct dirent *) (buffer + pos);
sprintf(fullsrcpath, "%s/%s", srcprefix, dirent->d_name);
sprintf(fulldstpath, "%s/%s", dstprefix, dirent->d_name);
/* looking for '.' or '..' ? */
isdot = (strcmp(dirent->d_name, "." ) == 0);
isdotdot = (strcmp(dirent->d_name, "..") == 0);
pos += sizeof(struct dirent); /* XXX variable size dirents possible XXX */
if (isdotdot)
continue;
if (isdot) {
/* hack */
udfclient_get_subtree(udf_node, fullsrcpath, fulldstpath, 0, total_size);
continue;
}
error = udf_lookup_name_in_dir(udf_node, dirent->d_name, DIRENT_NAMLEN(dirent), &udf_icbptr, fid, &found);
if (!error) {
error = ENOENT;
if (found)
error = udf_readin_udf_node(udf_node, &udf_icbptr, fid, &entry_node);
}
if (!error)
udfclient_get_subtree(entry_node, fullsrcpath, fulldstpath, 1, total_size);
}
} while (!eof);
udf_node->hold--;
free(buffer);
free(fid);
return;
}
/* leaf node : prefix is complete name but with `/' prefix */
if (*srcprefix == '/') srcprefix++;
error = udfclient_get_file(udf_node, srcprefix, dstprefix);
udf_node->hold--;
if (!error)
*total_size += udf_node->stat.st_size;
}
#undef GET_SUBTREE_DIR_BUFFER_SIZE
void udfclient_get(int args, char *arg1, char *arg2) {
struct udf_node *udf_node;
char *source_name, *target_name;
uint64_t start, now, totalsize, avg_speed;
int error;
if (args > 2) {
printf("Syntax: get remote [local]\n");
return;
}
source_name = arg1;
target_name = arg1;
if (args == 2)
target_name = arg2;
/* source name gets substituted */
source_name = udfclient_realpath(curdir.name, source_name, NULL);
DEBUG(printf("Attempting to retrieve %s\n", source_name));
error = udfclient_lookup_pathname(NULL, &udf_node, source_name);
if (error) {
fprintf(stderr, "%s : %s\n", arg1, strerror(error));
free(source_name);
return;
}
/* get the file/dir tree */
totalsize = 0;
start = getmtime();
udfclient_get_subtree(udf_node, source_name, target_name, 1, &totalsize);
now = getmtime();
if (now-start > 0) {
avg_speed = (1000000 * totalsize) / (now-start);
printf("A total of %d kb transfered at an overal average of %d kb/sec\n", (uint32_t) (totalsize/1024), (uint32_t) (avg_speed/1024));
} else {
printf("Transfered %d kb\n", (uint32_t)(totalsize/1024));
}
/* bugalert: not releasing target_name for its not substituted */
free(source_name);
}
void udfclient_mget(int args, char *argv[]) {
struct udf_node *udf_node;
uint64_t start, now, totalsize, avg_speed;
char *node_name, *source_name, *target_name;
int arg, error;
if (args == 0) {
printf("Syntax: mget (file | dir)*\n");
return;
}
/* retrieve the series of file/dir trees and measure time/seed */
totalsize = 0;
start = getmtime();
/* process all args */
arg = 0;
node_name = NULL;
while (args) {
source_name = target_name = argv[arg];
node_name = udfclient_realpath(curdir.name, source_name, NULL);
DEBUG(printf("Attempting to retrieve %s\n", node_name));
error = udfclient_lookup_pathname(NULL, &udf_node, node_name);
printf("%d: mget trying %s\n", error, node_name);
if (!error) {
udfclient_get_subtree(udf_node, source_name, target_name, 1, &totalsize);
}
if (node_name) {
free(node_name);
node_name = NULL;
}
if (error) break; /* TODO continuation flag? */
/* advance */
arg++;
args--;
}
now = getmtime();
if (now-start > 0) {
avg_speed = (1000000 * totalsize) / (now-start);
printf("A total of %d kb transfered at an overal average of %d kb/sec\n", (uint32_t) (totalsize/1024), (uint32_t) (avg_speed/1024));
} else {
printf("Transfered %d kb\n", (uint32_t) (totalsize/1024));
}
if (node_name)
free(node_name);
}
int udfclient_put_file(struct udf_node *udf_node, char *fullsrcname, char *fulldstname) {
struct uio file_uio;
struct iovec file_iov;
uint64_t file_length;
uint64_t start, now, then, eta;
uint64_t cur_speed, avg_speed, data_transfered;
uint64_t file_block_size, file_transfer_size;
uint8_t *file_block;
char cur_txt[32], avg_txt[32], eta_txt[32];
int fileh;
int error, printed;
assert(udf_node);
assert(fullsrcname);
DEBUG(printf("Attempting to write %s\n", fullsrcname));
fileh = open(fullsrcname, O_RDONLY, 0666);
if (fileh == -1) {
fprintf(stderr, "Can't open local file %s for reading: %s\n", fullsrcname, strerror(errno));
return ENOENT;
}
/* get file length */
file_length = lseek(fileh, 0, SEEK_END);
lseek(fileh, 0, SEEK_SET);
/* check if file will fit; give it a bit of slack space until the space issue is found and fixed */
if (udf_node->udf_log_vol->free_space < file_length + udf_node->udf_log_vol->await_alloc_space + UDF_MINFREE_LOGVOL) {
return ENOSPC;
}
/* allocate file block to transfer file with */
file_block_size = 128*1024;
file_block = malloc(file_block_size);
if (!file_block) {
fprintf(stderr, "Out of memory claiming file buffer\n");
return ENOMEM;
}
/* move to uio_newuio(struct uio *uio) with fixed length uio_iovcnt? */
bzero(&file_uio, sizeof(struct uio));
file_uio.uio_rw = UIO_READ; /* READ from this space */
file_uio.uio_iovcnt = 1;
file_uio.uio_iov = &file_iov;
/* ------------ */
start = getmtime();
then = now = start;
eta = data_transfered = 0;
printed = 0;
strcpy(avg_txt, "---"); strcpy(cur_txt, "---"); strcpy(eta_txt, "---");
/* ------------ */
error = 0;
error = udf_truncate_node(udf_node, 0);
while (!error && ((uint64_t) file_uio.uio_offset < file_length)) {
file_transfer_size = MIN(file_block_size, file_length - file_uio.uio_offset);
error = read(fileh, file_block, file_transfer_size);
if (error<0) {
fprintf(stderr, "While reading in file block for writing : %s\n", strerror(errno));
error = errno;
break;
}
file_uio.uio_resid = file_transfer_size;
file_uio.uio_iov->iov_base = file_block;
file_uio.uio_iov->iov_len = file_block_size;
error = udf_write_file_part_uio(udf_node, fullsrcname, UDF_C_USERDATA, &file_uio);
if (error) {
fprintf(stderr, "\nError while writing file : %s\n", strerror(error));
break;
}
/* ------------ */
if ((getmtime() - now > 1000000) || ((uint64_t) file_uio.uio_offset >= file_length)) {
printed = 1;
if (strlen(fulldstname) < 45) {
printf("\r%-45s ", fulldstname);
} else {
printf("\r...%-42s ", fulldstname+strlen(fulldstname)-42);
}
printf("%10"PRIu64" / %10"PRIu64" bytes ", (uint64_t) file_uio.uio_offset, (uint64_t) file_length);
if (file_length) printf("(%3d%%) ", (int) (100.0*(float) file_uio.uio_offset / file_length));
then = now;
now = getmtime();
cur_speed = 0;
avg_speed = 0;
if (now-start > 0) avg_speed = (1000000 * file_uio.uio_offset) / (now-start);
if (now-then > 0) cur_speed = (1000000 * (file_uio.uio_offset - data_transfered)) / (now-then);
if (avg_speed > 0) eta = (file_length - file_uio.uio_offset) / avg_speed;
data_transfered = file_uio.uio_offset;
strcpy(avg_txt, "---"); strcpy(cur_txt, "---"); strcpy(eta_txt, "---");
if (avg_speed > 0) sprintf(avg_txt, "%d", (int32_t) avg_speed/1024);
if (cur_speed > 0) sprintf(cur_txt, "%d", (int32_t) cur_speed/1024);
if (eta > 0) sprintf(eta_txt, "%02d:%02d:%02d", (int) (eta/3600), (int) (eta/60) % 60, (int) eta % 60);
printf("%6s KB/s (%6s KB/s) ETA %s", avg_txt, cur_txt, eta_txt);
fflush(stdout);
}
/* ------------ */
}
if (!error && printed) printf(" finished\n");
close(fileh);
free(file_block);
return error;
}
int udfclient_put_subtree(struct udf_node *parent_node, char *srcprefix, char *srcname, char *dstprefix, char *dstname, uint64_t *totalsize) {
struct udf_node *file_node, *dir_node;
struct dirent *dirent;
struct stat stat;
DIR *dir;
char fullsrcpath[1024], fulldstpath[1024];
int error;
sprintf(fullsrcpath, "%s/%s", srcprefix, srcname);
sprintf(fulldstpath, "%s/%s", dstprefix, dstname);
/* stat source file */
bzero(&stat, sizeof(struct stat));
error = lstat(fullsrcpath, &stat);
if (error) {
error = errno; /* lstat symantics; returns -1 on error */
fprintf(stderr, "Can't stat file/dir \"%s\"! : %s\n", fullsrcpath, strerror(error));
return error;
}
/* test if `srcname' refers to a directory */
dir = opendir(fullsrcpath);
if (dir) {
error = udfclient_lookup(parent_node, &dir_node, dstname);
if (error) {
DEBUG(printf("Create dir %s on UDF\n", fulldstpath));
error = udf_create_directory(parent_node, dstname, &stat, &dir_node);
if (error) {
closedir(dir);
fprintf(stderr, "UDF: couldn't create new directory %s : %s\n", dstname, strerror(error));
return error;
}
}
dir_node->hold++;
dirent = readdir(dir);
while (dirent) {
if (strcmp(dirent->d_name, ".") && strcmp(dirent->d_name, "..")) {
/* skip `.' and ',,' */
error = udfclient_put_subtree(dir_node, fullsrcpath, dirent->d_name, fulldstpath, dirent->d_name, totalsize);
if (error) break;
}
dirent = readdir(dir);
}
closedir(dir);
dir_node->hold--;
return error;
}
/* leaf node : prefix is complete name but with `/' prefix */
DEBUG(printf("Put leaf: %s\n", fulldstpath));
error = udfclient_lookup(parent_node, &file_node, dstname);
if (!file_node) {
error = udf_create_file(parent_node, dstname, &stat, &file_node);
if (error) {
fprintf(stderr, "UDF: couldn't add new file entry in directory %s for %s : %s\n", dstprefix, dstname, strerror(error));
return error;
}
}
file_node->hold++;
error = udfclient_put_file(file_node, fullsrcpath, fulldstpath);
file_node->hold--;
if (error) fprintf(stderr, "UDF: Couldn't write file %s : %s\n", fulldstpath, strerror(error));
if (error) udf_remove_file(parent_node, file_node, dstname);
if (!error) *totalsize += file_node->stat.st_size;
return error;
}
void udfclient_put(int args, char *arg1, char *arg2) {
struct udf_node *curdir_node;
uint64_t start, now, totalsize, avg_speed;
char *source_name, *target_name;
int error;
if (args > 2) {
printf("Syntax: put source [destination]\n");
return;
}
if (read_only) {
printf("Modifying this filingsystem is prevented; use -W flag to enable writing on your own risk!\n");
return;
}
error = udfclient_lookup_pathname(NULL, &curdir_node, curdir.name);
if (error) {
printf("Current directory not found?\n");
return;
}
DEBUG(printf("Attempting to copy %s\n", arg1));
/* determine source and destination entities */
source_name = arg1;
target_name = arg1;
if (args == 2)
target_name = arg2;
/* writeout file/dir tree and measure the time/speed */
totalsize = 0;
start = getmtime();
error = udfclient_put_subtree(curdir_node, ".", source_name, ".", target_name, &totalsize);
now = getmtime();
if (now-start > 0) {
avg_speed = (1000000 * totalsize) / (now-start);
printf("A total of %d kb transfered at an overal average of %d kb/sec\n", (uint32_t) (totalsize/1024), (uint32_t) (avg_speed/1024));
} else {
printf("Transfered %d kb\n", (uint32_t) (totalsize/1024));
}
}
/* args start at position 0 of argv */
void udfclient_mput(int args, char **argv) {
struct udf_node *curdir_node;
uint64_t start, now, totalsize, avg_speed;
char *source_name, *target_name;
int arg, error;
if (args == 0) {
printf("Syntax: mput (file | dir)*\n");
return;
}
if (read_only) {
printf("Modifying this filingsystem is prevented; use -W flag to enable writing on your own risk!\n");
return;
}
error = udfclient_lookup_pathname(NULL, &curdir_node, curdir.name);
if (error) {
printf("Current directory not found?\n");
return;
}
/* writeout file/dir trees and measure the time/speed */
totalsize = 0;
start = getmtime();
/* process all args */
arg = 0;
while (args) {
source_name = target_name = argv[arg];
error = udfclient_put_subtree(curdir_node, ".", source_name, ".", target_name, &totalsize);
if (error) {
fprintf(stderr, "While writing file %s : %s\n", source_name, strerror(error));
break; /* TODO continuation flag? */
}
/* advance */
arg++;
args--;
}
now = getmtime();
if (now-start > 0) {
avg_speed = (1000000 * totalsize) / (now-start);
printf("A total of %d kb transfered at an overal average of %d kb/sec\n", (uint32_t)(totalsize/1024), (uint32_t)(avg_speed/1024));
} else {
printf("Transfered %d kb\n", (uint32_t)(totalsize/1024));
}
}
void udfclient_trunc(int args, char *arg1, char *arg2) {
struct udf_node *udf_node;
char *node_name;
uint64_t length;
int error;
if (args != 2) {
printf("Syntax: trunc file length\n");
return;
}
length = strtoll(arg2, NULL, 10);
node_name = udfclient_realpath(curdir.name, arg1, NULL);
error = udfclient_lookup_pathname(NULL, &udf_node, node_name);
if (error || !udf_node) {
printf("Can only truncate existing file!\n");
free(node_name);
return;
}
udf_truncate_node(udf_node, length);
free(node_name);
}
void udfclient_sync(void) {
struct udf_discinfo *udf_disc;
SLIST_FOREACH(udf_disc, &udf_discs_list, next_disc) {
udf_sync_disc(udf_disc);
}
}
#define RM_SUBTREE_DIR_BUFFER_SIZE (32*1024)
int udfclient_rm_subtree(struct udf_node *parent_node, struct udf_node *dir_node, char *name, char *full_parent_name) {
struct uio dir_uio;
struct iovec dir_iovec;
uint8_t *buffer;
uint32_t pos;
char *fullpath;
struct dirent *dirent;
struct stat stat;
struct udf_node *entry_node;
struct fileid_desc *fid;
struct long_ad udf_icbptr;
int lb_size, eof, found, isdot, isdotdot;
int error;
if (!dir_node)
return ENOENT;
error = udfclient_getattr(dir_node, &stat);
if (stat.st_mode & S_IFDIR) {
buffer = malloc(RM_SUBTREE_DIR_BUFFER_SIZE);
if (!buffer)
return ENOSPC;
lb_size = dir_node->udf_log_vol->lb_size;
fid = malloc(lb_size);
if (!fid) {
free(buffer);
return ENOSPC;
}
/* recurse into this directory */
dir_uio.uio_offset = 0; /* begin at start */
do {
dir_iovec.iov_base = buffer;
dir_iovec.iov_len = RM_SUBTREE_DIR_BUFFER_SIZE;
dir_uio.uio_resid = RM_SUBTREE_DIR_BUFFER_SIZE;
dir_uio.uio_iovcnt = 1;
dir_uio.uio_iov = &dir_iovec;
dir_uio.uio_rw = UIO_WRITE;
error = udf_readdir(dir_node, &dir_uio, &eof);
pos = 0;
while (pos < RM_SUBTREE_DIR_BUFFER_SIZE - dir_uio.uio_resid) {
dirent = (struct dirent *) (buffer + pos);
pos += sizeof(struct dirent); /* XXX variable size dirents possible XXX */
/* looking for '.' or '..' ? */
isdot = (strcmp(dirent->d_name, "." ) == 0);
isdotdot = (strcmp(dirent->d_name, "..") == 0);
if (isdot || isdotdot)
continue;
error = udf_lookup_name_in_dir(dir_node, dirent->d_name, DIRENT_NAMLEN(dirent), &udf_icbptr, fid, &found);
if (!error) {
error = ENOENT;
if (found)
error = udf_readin_udf_node(dir_node, &udf_icbptr, fid, &entry_node);
}
if (error)
break;
error = udfclient_getattr(entry_node, &stat);
if (error)
break;
/* check if the direntry is a directory or a file */
if (stat.st_mode & S_IFDIR) {
fullpath = malloc(strlen(full_parent_name) + strlen(dirent->d_name)+2);
if (fullpath) {
sprintf(fullpath, "%s/%s", full_parent_name, dirent->d_name);
error = udfclient_rm_subtree(dir_node, entry_node, dirent->d_name, fullpath);
} else {
error = ENOMEM;
}
free(fullpath);
} else {
error = udf_remove_file(dir_node, entry_node, dirent->d_name);
if (!error)
printf("rm %s/%s\n", full_parent_name, dirent->d_name);
}
if (error)
break;
}
} while (!eof);
free(buffer);
free(fid);
/* leaving directory -> delete directory itself */
if (!error) {
error = udf_remove_directory(parent_node, dir_node, name);
if (!error) printf("rmdir %s/%s\n", full_parent_name, name);
}
return error;
}
return ENOTDIR;
}
#undef RM_SUBTREE_DIR_BUFFER_SIZE
void udfclient_rm(int args, char *argv[]) {
struct udf_node *remove_node, *parent_node;
struct stat stat;
char *target_name, *leaf_name, *full_parent_name;
int error, len, arg;
if (args == 0) {
printf("Syntax: rm (file | dir)*\n");
return;
}
/* process all args; effectively multiplying an `rm' command */
arg = 0;
while (args) {
leaf_name = argv[arg];
/* lookup node; target_name gets substituted */
target_name = udfclient_realpath(curdir.name, leaf_name, &leaf_name);
error = udfclient_lookup_pathname(NULL, &remove_node, target_name);
if (error || !remove_node) {
printf("rm %s : %s\n", target_name, strerror(error));
free(target_name);
return; /* TODO continuation flag */
/* continue; */
}
full_parent_name = udfclient_realpath(target_name, "..", NULL);
error = udfclient_lookup_pathname(NULL, &parent_node, full_parent_name);
if (error || !parent_node) {
printf("rm %s : parent lookup failed : %s\n", target_name, strerror(error));
free(target_name);
free(full_parent_name);
return; /* TODO continuation flag */
/* continue; */
}
error = udfclient_getattr(remove_node, &stat);
if (!error) {
if (stat.st_mode & S_IFDIR) {
len = strlen(target_name);
if (target_name[len-1] == '/') target_name[len-1] = '\0';
error = udfclient_rm_subtree(parent_node, remove_node, leaf_name, target_name);
} else {
error = udf_remove_file(parent_node, remove_node, leaf_name);
if (!error) printf("rm %s/%s\n", full_parent_name, leaf_name);
}
}
if (error)
fprintf(stderr, "While removing file/dir : %s\n", strerror(error));
free(target_name);
free(full_parent_name);
if (error)
break; /* TODO continuation flag */
/* advance */
arg++;
args--;
}
}
void udfclient_mv(int args, char *from, char *to) {
struct udf_node *rename_me, *present, *old_parent, *new_parent;
char *rename_from_name, *rename_to_name, *old_parent_name, *new_parent_name;
int error;
if (args != 2) {
printf("Syntax: mv source destination\n");
return;
}
/* `from' gets substituted by its leaf name */
rename_from_name = udfclient_realpath(curdir.name, from, &from);
error = udfclient_lookup_pathname(NULL, &rename_me, rename_from_name);
if (error || !rename_me) {
printf("Can't find file/dir to be renamed\n");
free(rename_from_name);
return;
}
old_parent_name = udfclient_realpath(rename_from_name, "..", NULL);
error = udfclient_lookup_pathname(NULL, &old_parent, old_parent_name);
if (error || !old_parent) {
printf("Can't determine rootdir of renamed file?\n");
free(rename_from_name);
free(old_parent_name);
return;
}
/* `to' gets substituted by its leaf name */
rename_to_name = udfclient_realpath(curdir.name, to, &to);
udfclient_lookup_pathname(NULL, &present, rename_to_name);
new_parent_name = udfclient_realpath(rename_to_name, "..", NULL);
error = udfclient_lookup_pathname(NULL, &new_parent, new_parent_name);
if (error || !new_parent) {
printf("Can't determine rootdir of destination\n");
free(rename_from_name);
free(rename_to_name);
free(old_parent_name);
free(new_parent_name);
return;
}
error = udf_rename(old_parent, rename_me, from, new_parent, present, to);
if (error) {
printf("Can't move file or directory: %s\n", strerror(error));
return;
}
free(rename_from_name);
free(rename_to_name);
free(old_parent_name);
free(new_parent_name);
}
void udfclient_mkdir(int args, char *arg1) {
struct stat stat;
struct udf_node *udf_node, *parent_node;
char *full_create_name, *dirname, *parent_name;
int error;
if (args != 1) {
printf("Syntax: mkdir dir\n");
return;
}
/* get full name of dir to be created */
full_create_name = udfclient_realpath(curdir.name, arg1, &dirname);
parent_name = udfclient_realpath(full_create_name, "..", NULL);
error = udfclient_lookup_pathname(NULL, &parent_node, parent_name);
if (error || !parent_node) {
printf("Can't determine directory the new directory needs to be created in %d <%s> <%s> <%s>\n", error, parent_name, full_create_name, curdir.name);
free(full_create_name);
free(parent_name);
return;
}
bzero(&stat, sizeof(struct stat));
stat.st_uid = UINT_MAX;
stat.st_gid = UINT_MAX;
stat.st_mode = 0755 | S_IFDIR; /* don't forget this! */
error = udf_create_directory(parent_node, dirname, &stat, &udf_node);
if (error) {
printf("Can't create directory %s : %s\n", arg1, strerror(error));
}
free(full_create_name);
free(parent_name);
}
/* `line' gets more and more messed up in the proces */
char *udfclient_get_one_arg(char *line, char **result) {
unsigned char chr, limiter;
char *end_arg;
*result = NULL;
/* get prepending whitespace */
while (*line && (*line <= ' ')) line++;
chr= '\0';
limiter = ' ';
if (*line == '"') {
line++;
limiter = '"';
}
*result = line;
while (*line) {
chr = *line;
if (chr && (chr < ' ')) chr = ' ';
if (chr == 0 || chr == limiter) {
break;
} else {
*line = chr;
}
line++;
}
end_arg = line;
if (chr == limiter) line++;
/* get appended whitespace */
while (*line && (*line <= ' ')) line++;
*end_arg = 0;
return line;
}
int udfclient_get_args(char *line, char *argv[]) {
int arg, args;
for (arg = 0; arg < MAX_ARGS+1; arg++) {
argv[arg] = "";
}
/* get all arguments */
args = 0;
while (args < MAX_ARGS+1) {
line = udfclient_get_one_arg(line, &argv[args]);
args++;
if (!*line) {
return args;
}
}
printf("UDFclient implementation limit: too many arguments\n");
return 0;
}
void udfclient_interact(void) {
int args, params;
char *cmd;
char *argv[MAX_ARGS+1];
char line[4096];
udfclient_pwd(0);
while (1) {
printf("UDF> ");
clearerr(stdin);
*line = 0;
(void) fgets(line, 4096, stdin);
if ((*line == 0) && feof(stdin)) {
printf("quit\n");
return;
}
args = udfclient_get_args(line, argv);
cmd = argv[0];
params = args -1;
if (args) {
if (strcmp(cmd, "")==0) continue;
if (strcmp(cmd, "ls")==0) {
udfclient_ls(params, argv[1]);
continue;
}
if (strcmp(cmd, "cd")==0) {
udfclient_cd(params, argv[1]);
continue;
}
if (strcmp(cmd, "lcd")==0) {
udfclient_lcd(params, argv[1]);
continue;
}
if (strcmp(cmd, "lls")==0) {
udfclient_lls(params);
continue;
}
if (strcmp(cmd, "pwd")==0) {
udfclient_pwd(params);
continue;
}
if (strcmp(cmd, "free")==0) {
udfclient_free(params);
continue;
}
if (strcmp(cmd, "get")==0) {
udfclient_get(params, argv[1], argv[2]);
continue;
}
if (strcmp(cmd, "mget")==0) {
udfclient_mget(params, argv + 1);
continue;
}
if (strcmp(cmd, "put")==0) {
/* can get destination file/dir (one day) */
udfclient_put(params, argv[1], argv[2]);
continue;
}
if (strcmp(cmd, "mput")==0) {
udfclient_mput(params, argv + 1);
continue;
}
if (strcmp(cmd, "trunc")==0) {
udfclient_trunc(params, argv[1], argv[2]);
continue;
}
if (strcmp(cmd, "mkdir")==0) {
udfclient_mkdir(params, argv[1]);
continue;
}
if (strcmp(cmd, "rm")==0) {
udfclient_rm(params, argv + 1);
continue;
}
if (strcmp(cmd, "mv")==0) {
udfclient_mv(params, argv[1], argv[2]);
continue;
}
if (strcmp(cmd, "sync")==0) {
udfclient_sync();
continue;
}
if (strcmp(cmd, "help")==0) {
printf("Selected commands available (use \" pair for filenames with spaces) :\n"
"ls [file | dir]\tlists the UDF directory\n"
"cd [dir]\t\tchange current UDF directory\n"
"lcd [dir]\t\tchange current directory\n"
"lls\t\t\tlists current directory\n"
"pwd\t\t\tdisplay current directories\n"
"free\t\t\tdisplay free space on disc\n"
"get source [dest]\tretrieve a file / directory from disc\n"
"mget (file | dir)*\tretrieve set of files / directories\n"
"put source [dest]\twrite a file / directory to disc\n"
"mput (file | dir)*\twrite a set of files / directories\n"
"trunc file length\ttrunc file to length\n"
"mkdir dir\t\tcreate directory\n"
"rm (file | dir)*\tdelete set of files / directories\n"
"mv source dest\t\trename a file (limited)\n"
"sync\t\t\tsync filingsystem\n"
"quit\t\t\texits program\n"
"exit\t\t\talias for quit\n"
);
continue;
}
if (strcmp(cmd, "quit")==0 ||
strcmp(cmd, "exit")==0) {
return;
}
printf("\nUnknown command %s\n", cmd);
}
}
}
int usage(char *program) {
fprintf(stderr, "Usage: %s [options] devicename [devicename]*)\n", program);
fprintf(stderr, "-u level UDF system verbose level\n"
"-r range use only selected sessions like -3,5,7 or 6-\n"
"-W allow writing (temporary flag)\n"
"-F force mount writable when marked dirty (use with cause)\n"
"-b blocksize use alternative sectorsize; use only on files/discs\n"
"-D debug/verbose SCSI command errors\n"
"-s byteswap read sectors (for PVRs)\n"
);
return 1;
}
extern char *optarg;
extern int optind;
extern int optreset;
int main(int argc, char *argv[]) {
struct udf_discinfo *disc, *next_disc;
uint32_t alt_sector_size;
char *progname, *range;
int flag, mnt_flags;
int error;
progname = argv[0];
if (argc == 1) {
return usage(progname);
}
/* be a bit more verbose */
udf_verbose = UDF_VERBLEV_ACTIONS;
uscsilib_verbose= 0;
mnt_flags = UDF_MNT_RDONLY;
range = NULL;
alt_sector_size = 0;
while ((flag = getopt(argc, argv, "u:Dr:WFb:s")) != -1) {
switch (flag) {
case 'u' :
udf_verbose = atoi(optarg);
break;
case 'D' :
uscsilib_verbose = 1;
break;
case 'r' :
range = strdup(optarg);
if (udf_check_session_range(range)) {
fprintf(stderr, "Invalid range %s\n", range);
return usage(progname);
}
break;
case 'W' :
mnt_flags &= ~UDF_MNT_RDONLY;
break;
case 'F' :
mnt_flags |= UDF_MNT_FORCE;
break;
case 'b' :
alt_sector_size = atoi(optarg);
break;
case 's' :
mnt_flags |= UDF_MNT_BSWAP;
break;
default :
return usage(progname);
}
}
argv += optind;
argc -= optind;
if (argc == 0) return usage(progname);
if (!(mnt_flags & UDF_MNT_RDONLY)) {
printf("--------------------------------\n");
printf("WARNING: writing enabled, use on own risc\n");
printf("\t* DONT cancel program or data-loss might occure\n");
printf("\t* set dataspace userlimits very high when writing thousands of files\n");
printf("\nEnjoy your writing!\n");
printf("--------------------------------\n\n\n");
printf("%c", 7); fflush(stdout); sleep(1); printf("%c", 7); fflush(stdout); sleep(1); printf("%c", 7); fflush(stdout);
}
/* all other arguments are devices */
udf_init();
while (argc) {
printf("Opening device %s\n", *argv);
error = udf_mount_disc(*argv, range, alt_sector_size, mnt_flags, &disc);
if (error) {
fprintf(stderr, "Can't open my device; bailing out : %s\n", strerror(error));
exit(1);
}
if (read_only) disc->recordable = 0;
if (read_only) disc->rewritable = 0;
argv++; argc--;
if (udf_verbose) printf("\n\n");
}
printf("\n");
printf("Resulting list of alive sets :\n\n");
udf_dump_alive_sets();
/* interactive part */
bzero(&curdir, sizeof(struct curdir));
curdir.mountpoint = NULL;
curdir.name = strdup("/");
udfclient_ls(0, "");
udfclient_interact();
/* close part */
printf("Closing discs\n");
disc = SLIST_FIRST(&udf_discs_list);
while (disc) {
next_disc = SLIST_NEXT(disc, next_disc);
udf_dismount_disc(disc);
disc = next_disc;
}
return 0;
}
|