Changeset 69614 in vbox for trunk/src/VBox
- Timestamp:
- Nov 8, 2017 12:58:04 PM (7 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Runtime/tools/RTLs.cpp
r69434 r69614 56 56 /** Symbolic link target (allocated after the name). */ 57 57 const char *pszTarget; 58 /** Owner if applicable(allocated after the name). */ 59 const char *pszOwner; 60 /** Group if applicable (allocated after the name). */ 61 const char *pszGroup; 58 62 /** The length of szName. */ 59 63 size_t cchName; … … 529 533 static const char *rtCmdLsFormatSizeHumanReadable(PRTCMDLSOPTS pOpts, uint64_t cb, char *pszDst, size_t cbDst) 530 534 { 531 /** @todo human readable size formatting (IPRT?). */ 532 RT_NOREF(pOpts); 533 RTStrFormatU64(pszDst, cbDst, cb, 10, 0, 0, 0); 535 if (pOpts->fHumanReadableSizes) 536 { 537 static const struct 538 { 539 const char *pszSuffix; 540 uint64_t cbFactor; 541 uint64_t cbMin; 542 } s_aUnits[] = 543 { 544 { "E", _1E, _1E + _1E/2 }, 545 { "P", _1P, _1P + _1P/2 }, 546 { "T", _1T, _1T + _1T/2 }, 547 { "G", _1G, _1G + _1G/2 }, 548 { "M", _1M, _1M + _1M/2 }, 549 { "k", _1K, _1K + _1K/2 }, 550 551 { "E", UINT64_C(1000000000000000000), UINT64_C(1010000000000000000), }, 552 { "P", UINT64_C(1000000000000000), UINT64_C(1010000000000000), }, 553 { "T", UINT64_C(1000000000000), UINT64_C(1010000000000), }, 554 { "G", UINT64_C(1000000000), UINT64_C(1010000000), }, 555 { "M", UINT64_C(1000000), UINT64_C(1010000), }, 556 { "K", UINT64_C(1000), UINT64_C(1010), }, 557 }; 558 unsigned const iEnd = !pOpts->fSiUnits ? 6 : 12; 559 for (unsigned i = !pOpts->fSiUnits ? 0 : 6; i < iEnd; i++) 560 if (cb >= s_aUnits[i].cbMin) 561 { 562 RTStrFormatU64(pszDst, cbDst, cb / s_aUnits[i].cbFactor, 10, 0, 0, 0); 563 RTStrCat(pszDst, cbDst, s_aUnits[i].pszSuffix); 564 return pszDst; 565 } 566 } 567 else if (pOpts->cbBlock) 568 RTStrFormatU64(pszDst, cbDst, (cb + pOpts->cbBlock - 1) / pOpts->cbBlock, 10, 0, 0, 0); 569 else 570 RTStrFormatU64(pszDst, cbDst, cb, 10, 0, 0, 0); 534 571 return pszDst; 535 572 } … … 616 653 * @param pOpts The options and state. 617 654 * @param gid The GID to format. 655 * @param pszOwner The owner returned by the FS. 618 656 * @param pszDst The output buffer. 619 657 * @param cbDst The output buffer size. 620 658 */ 621 static const char *rtCmdLsDecimalFormatGroup(PRTCMDLSOPTS pOpts, RTGID gid, c har *pszDst, size_t cbDst)659 static const char *rtCmdLsDecimalFormatGroup(PRTCMDLSOPTS pOpts, RTGID gid, const char *pszGroup, char *pszDst, size_t cbDst) 622 660 { 623 661 if (!pOpts->fNumericalIds) 624 662 { 625 /** @todo resolve GIDs to names. */ 663 if (pszGroup) 664 { 665 RTStrCopy(pszDst, cbDst, pszGroup); 666 return pszDst; 667 } 626 668 if (gid == NIL_RTGID) 627 669 return "<Nil>"; … … 638 680 * @param pOpts The options and state. 639 681 * @param uid The UID to format. 682 * @param pszOwner The owner returned by the FS. 640 683 * @param pszDst The output buffer. 641 684 * @param cbDst The output buffer size. 642 685 */ 643 static const char *rtCmdLsDecimalFormatOwner(PRTCMDLSOPTS pOpts, RTUID uid, c har *pszDst, size_t cbDst)686 static const char *rtCmdLsDecimalFormatOwner(PRTCMDLSOPTS pOpts, RTUID uid, const char *pszOwner, char *pszDst, size_t cbDst) 644 687 { 645 688 if (!pOpts->fNumericalIds) 646 689 { 647 /** @todo resolve UIDs to names. */ 690 if (pszOwner) 691 { 692 RTStrCopy(pszDst, cbDst, pszOwner); 693 return pszDst; 694 } 648 695 if (uid == NIL_RTUID) 649 696 return "<Nil>"; … … 727 774 if (pOpts->fShowOwner) 728 775 { 729 rtCmdLsDecimalFormatOwner(pOpts, pEntry->Info.Attr.u.Unix.uid, p szTmp, cbTmp);776 rtCmdLsDecimalFormatOwner(pOpts, pEntry->Info.Attr.u.Unix.uid, pEntry->pszOwner, pszTmp, cbTmp); 730 777 cchTmp = strlen(pszTmp); 731 778 if (cchTmp > cchUidCol) … … 735 782 if (pOpts->fShowGroup) 736 783 { 737 rtCmdLsDecimalFormatGroup(pOpts, pEntry->Info.Attr.u.Unix.gid, p szTmp, cbTmp);784 rtCmdLsDecimalFormatGroup(pOpts, pEntry->Info.Attr.u.Unix.gid, pEntry->pszGroup, pszTmp, cbTmp); 738 785 cchTmp = strlen(pszTmp); 739 786 if (cchTmp > cchGidCol) … … 820 867 RTPrintf(" %*u", cchLinkCol, pEntry->Info.Attr.u.Unix.cHardlinks); 821 868 if (cchUidCol) 822 RTPrintf(" %*s", cchUidCol, rtCmdLsDecimalFormatOwner(pOpts, pEntry->Info.Attr.u.Unix.uid, pszTmp, cbTmp)); 869 RTPrintf(" %*s", cchUidCol, 870 rtCmdLsDecimalFormatOwner(pOpts, pEntry->Info.Attr.u.Unix.uid, pEntry->pszOwner, pszTmp, cbTmp)); 823 871 if (cchGidCol) 824 RTPrintf(" %*s", cchGidCol, rtCmdLsDecimalFormatGroup(pOpts, pEntry->Info.Attr.u.Unix.gid, pszTmp, cbTmp)); 872 RTPrintf(" %*s", cchGidCol, 873 rtCmdLsDecimalFormatGroup(pOpts, pEntry->Info.Attr.u.Unix.gid, pEntry->pszGroup, pszTmp, cbTmp)); 825 874 RTPrintf(" %*s", cchSizeCol, rtCmdLsFormatSize(pOpts, pEntry->Info.cbObject, pszTmp, cbTmp)); 826 875 … … 1030 1079 * @param pszEntry The entry name. 1031 1080 * @param pInfo The entry info. 1032 */ 1033 static RTEXITCODE rtCmdLsAddOne(PRTCMDLSCOLLECTION pCollection, const char *pszEntry, PRTFSOBJINFO pInfo) 1034 { 1081 * @param pszOwner The owner name if available, otherwise NULL. 1082 * @param pszGroup The group anme if available, otherwise NULL. 1083 * @param pszTarget The symbolic link target if applicable and 1084 * available, otherwise NULL. 1085 */ 1086 static RTEXITCODE rtCmdLsAddOne(PRTCMDLSCOLLECTION pCollection, const char *pszEntry, PRTFSOBJINFO pInfo, 1087 const char *pszOwner, const char *pszGroup, const char *pszTarget) 1088 { 1089 1035 1090 /* Make sure there is space in the collection for the new entry. */ 1036 1091 if (pCollection->cEntries >= pCollection->cEntriesAllocated) … … 1046 1101 /* Create and insert a new entry. */ 1047 1102 size_t const cchEntry = strlen(pszEntry); 1048 PRTCMDLSENTRY pEntry = (PRTCMDLSENTRY)RTMemAlloc(RT_OFFSETOF(RTCMDLSENTRY, szName[cchEntry + 1])); 1103 size_t const cbOwner = pszOwner ? strlen(pszOwner) + 1 : 0; 1104 size_t const cbGroup = pszGroup ? strlen(pszGroup) + 1 : 0; 1105 size_t const cbTarget = pszTarget ? strlen(pszTarget) + 1 : 0; 1106 size_t const cbEntry = RT_OFFSETOF(RTCMDLSENTRY, szName[cchEntry + 1 + cbOwner + cbGroup + cbTarget]); 1107 PRTCMDLSENTRY pEntry = (PRTCMDLSENTRY)RTMemAlloc(cbEntry); 1049 1108 if (pEntry) 1050 1109 { 1051 1110 pEntry->Info = *pInfo; 1052 1111 pEntry->pszTarget = NULL; /** @todo symbolic links. */ 1112 pEntry->pszOwner = NULL; 1113 pEntry->pszGroup = NULL; 1053 1114 pEntry->cchName = cchEntry; 1054 1115 memcpy(pEntry->szName, pszEntry, cchEntry); 1055 1116 pEntry->szName[cchEntry] = '\0'; 1117 1118 char *psz = &pEntry->szName[cchEntry + 1]; 1119 if (pszTarget) 1120 { 1121 pEntry->pszTarget = psz; 1122 memcpy(psz, pszTarget, cbTarget); 1123 psz += cbTarget; 1124 } 1125 if (pszOwner) 1126 { 1127 pEntry->pszOwner = psz; 1128 memcpy(psz, pszOwner, cbOwner); 1129 psz += cbOwner; 1130 } 1131 if (pszGroup) 1132 { 1133 pEntry->pszGroup = psz; 1134 memcpy(psz, pszGroup, cbGroup); 1135 } 1056 1136 1057 1137 pCollection->papEntries[pCollection->cEntries++] = pEntry; … … 1153 1233 if (rtCmdLsIsFilteredOut(pOpts, pDirEntry->szName, &pDirEntry->Info)) 1154 1234 continue; 1155 RTEXITCODE rcExit2 = rtCmdLsAddOne(pCollection, pDirEntry->szName, &pDirEntry->Info); 1235 1236 1237 const char *pszOwner = NULL; 1238 RTFSOBJINFO OwnerInfo; 1239 if (pDirEntry->Info.Attr.u.Unix.uid != NIL_RTUID && pOpts->fShowOwner) 1240 { 1241 rc = RTVfsDirQueryPathInfo(hVfsDir, pDirEntry->szName, &OwnerInfo, RTFSOBJATTRADD_UNIX_OWNER, RTPATH_F_ON_LINK); 1242 if (RT_SUCCESS(rc) && OwnerInfo.Attr.u.UnixOwner.szName[0]) 1243 pszOwner = &OwnerInfo.Attr.u.UnixOwner.szName[0]; 1244 } 1245 1246 const char *pszGroup = NULL; 1247 RTFSOBJINFO GroupInfo; 1248 if (pDirEntry->Info.Attr.u.Unix.gid != NIL_RTGID && pOpts->fShowGroup) 1249 { 1250 rc = RTVfsDirQueryPathInfo(hVfsDir, pDirEntry->szName, &GroupInfo, RTFSOBJATTRADD_UNIX_GROUP, RTPATH_F_ON_LINK); 1251 if (RT_SUCCESS(rc) && GroupInfo.Attr.u.UnixGroup.szName[0]) 1252 pszGroup = &GroupInfo.Attr.u.UnixGroup.szName[0]; 1253 } 1254 1255 RTEXITCODE rcExit2 = rtCmdLsAddOne(pCollection, pDirEntry->szName, &pDirEntry->Info, pszOwner, pszGroup, NULL); 1156 1256 if (rcExit2 != RTEXITCODE_SUCCESS) 1157 1257 rcExit = rcExit2; … … 1250 1350 if ( pOpts->cCollections > 0 1251 1351 || rtCmdLsNewCollection(pOpts, "") != NULL) 1252 return rtCmdLsAddOne(pOpts->papCollections[0], pszArg, &Info); 1352 { 1353 RTFSOBJINFO OwnerInfo; 1354 if (Info.Attr.u.Unix.uid != NIL_RTUID && pOpts->fShowOwner) 1355 rc = RTVfsChainQueryInfo(pszArg, &OwnerInfo, RTFSOBJATTRADD_UNIX_OWNER, fPath, NULL, NULL); 1356 else 1357 rc = VERR_NOT_SUPPORTED; 1358 const char *pszOwner = RT_SUCCESS(rc) && OwnerInfo.Attr.u.UnixOwner.szName[0] 1359 ? &OwnerInfo.Attr.u.UnixOwner.szName[0] : NULL; 1360 1361 RTFSOBJINFO GroupInfo; 1362 if (Info.Attr.u.Unix.gid != NIL_RTGID && pOpts->fShowGroup) 1363 rc = RTVfsChainQueryInfo(pszArg, &GroupInfo, RTFSOBJATTRADD_UNIX_GROUP, fPath, NULL, NULL); 1364 else 1365 rc = VERR_NOT_SUPPORTED; 1366 const char *pszGroup = RT_SUCCESS(rc) && GroupInfo.Attr.u.UnixGroup.szName[0] 1367 ? &GroupInfo.Attr.u.UnixGroup.szName[0] : NULL; 1368 1369 return rtCmdLsAddOne(pOpts->papCollections[0], pszArg, &Info, pszOwner, pszGroup, NULL); 1370 } 1253 1371 return RTEXITCODE_FAILURE; 1254 1372 }
Note:
See TracChangeset
for help on using the changeset viewer.