Changeset 37860 in vbox for trunk/src/VBox/Additions/common
- Timestamp:
- Jul 11, 2011 9:48:37 AM (14 years ago)
- svn:sync-xref-src-repo-rev:
- 72757
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/common/VBoxService/VBoxServiceToolBox.cpp
r37814 r37860 44 44 45 45 46 /** Options indices for "vbox_cat". */ 46 47 #define CAT_OPT_NO_CONTENT_INDEXED 1000 47 48 /** Options indices for "vbox_ls". */ 48 49 #define LS_OPT_MACHINE_READABLE 1000 49 50 /** Options indices for "vbox_stat". */ 51 #define STAT_OPT_MACHINE_READABLE 1000 52 /** Flags for "vbox_ls". */ 50 53 typedef enum VBOXSERVICETOOLBOXLSFLAG 51 54 { 52 VBOXSERVICETOOLBOXLSFLAG_NONE = 0, 53 VBOXSERVICETOOLBOXLSFLAG_RECURSIVE = 0x00000001, 54 VBOXSERVICETOOLBOXLSFLAG_LONG = 0x00000002, 55 VBOXSERVICETOOLBOXLSFLAG_PARSEABLE = 0x00000004, 56 VBOXSERVICETOOLBOXLSFLAG_SYMLINKS = 0x00000008 57 } VBOXSERVICETOOLBOXLSFLAG; 55 VBOXSERVICETOOLBOXLSFLAG_NONE = 0x0, 56 VBOXSERVICETOOLBOXLSFLAG_RECURSIVE = 0x1, 57 VBOXSERVICETOOLBOXLSFLAG_SYMLINKS = 0x2 58 } VBOXSERVICETOOLBOXLSFLAG, *PVBOXSERVICETOOLBOXLSFLAG; 59 60 /** Flags for fs object output. */ 61 typedef enum VBOXSERVICETOOLBOXOUTPUTFLAG 62 { 63 VBOXSERVICETOOLBOXOUTPUTFLAG_NONE = 0x0, 64 VBOXSERVICETOOLBOXOUTPUTFLAG_LONG = 0x1, 65 VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE = 0x2 66 } VBOXSERVICETOOLBOXOUTPUTFLAG, *PVBOXSERVICETOOLBOXOUTPUTFLAG; 58 67 59 68 /* Enable the following define to be able to debug/invoke the toolbox … … 132 141 } 133 142 143 /** 144 * Prints a parseable stream header which contains the actual tool 145 * which was called/used along with its stream version. 146 * 147 * @param pszToolName Name of the tool being used, e.g. "vbt_ls". 148 * @param uVersion Stream version name. Handy for distinguishing 149 * different stream versions later. 150 */ 151 static void VBoxServiceToolboxPrintStrmHeader(const char *pszToolName, uint32_t uVersion) 152 { 153 AssertPtrReturnVoid(pszToolName); 154 RTPrintf("hdr_id=%s%chdr_ver=%u%c", pszToolName, 0, uVersion, 0); 155 } 156 157 /** 158 * Prints a standardized termination sequence indicating that the 159 * parseable stream just ended. 160 * 161 */ 162 static void VBoxServiceToolboxPrintStrmTermination() 163 { 164 RTPrintf("%c%c%c%c", 0, 0, 0, 0); 165 } 134 166 135 167 /** … … 407 439 * @param pszName Object name. 408 440 * @param cbName Size of object name. 409 * @param u Flags Output / handling flags.441 * @param uOutputFlags Output / handling flags of type VBOXSERVICETOOLBOXOUTPUTFLAG. 410 442 * @param pObjInfo Pointer to object information. 411 443 */ 412 static int VBoxServiceToolboxLsPrintFsInfo(const char *pszName, uint16_t cbName, uint32_t uFlags, 413 PRTFSOBJINFO pObjInfo) 444 static int VBoxServiceToolboxPrintFsInfo(const char *pszName, uint16_t cbName, 445 uint32_t uOutputFlags, 446 PRTFSOBJINFO pObjInfo) 414 447 { 415 448 AssertPtrReturn(pszName, VERR_INVALID_POINTER); … … 435 468 /** @todo sticy bits++ */ 436 469 437 if (!(u Flags & VBOXSERVICETOOLBOXLSFLAG_LONG))438 { 439 if (u Flags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE)470 if (!(uOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_LONG)) 471 { 472 if (uOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) 440 473 { 441 474 /** @todo Skip node_id if not present/available! */ … … 448 481 cFileType, cbName, pszName); 449 482 450 if (u Flags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE) /* End of data block. */483 if (uOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* End of data block. */ 451 484 RTPrintf("%c%c", 0, 0); 452 485 } 453 486 else 454 487 { 455 if (u Flags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE)488 if (uOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) 456 489 { 457 490 RTPrintf("ftype=%c%c", cFileType, 0); … … 565 598 * @param pszDir Directory (path) to ouptut. 566 599 * @param uFlags Flags of type VBOXSERVICETOOLBOXLSFLAG. 567 */ 568 static int VBoxServiceToolboxLsHandleDir(const char *pszDir, uint32_t uFlags) 600 * @param uOutputFlags Flags of type VBOXSERVICETOOLBOXOUTPUTFLAG. 601 */ 602 static int VBoxServiceToolboxLsHandleDir(const char *pszDir, 603 uint32_t uFlags, uint32_t uOutputFlags) 569 604 { 570 605 AssertPtrReturn(pszDir, VERR_INVALID_PARAMETER); 571 606 572 if (uFlags & VBOXSERVICETOOLBOX LSFLAG_PARSEABLE)607 if (uFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) 573 608 RTPrintf("dname=%s%c", pszDir, 0); 574 609 else if (uFlags & VBOXSERVICETOOLBOXLSFLAG_RECURSIVE) … … 632 667 RTListForEach(&dirList, pNodeIt, VBOXSERVICETOOLBOXDIRENTRY, Node) 633 668 { 634 rc = VBoxServiceToolbox LsPrintFsInfo(pNodeIt->dirEntry.szName, pNodeIt->dirEntry.cbName,635 uFlags,636 669 rc = VBoxServiceToolboxPrintFsInfo(pNodeIt->dirEntry.szName, pNodeIt->dirEntry.cbName, 670 uOutputFlags, 671 &pNodeIt->dirEntry.Info); 637 672 if (RT_FAILURE(rc)) 638 673 break; … … 667 702 pszDir, pNodeIt->dirEntry.szName); 668 703 if (RT_SUCCESS(rc)) 669 rc = VBoxServiceToolboxLsHandleDir(szPath, uFlags); 704 rc = VBoxServiceToolboxLsHandleDir(szPath, 705 uFlags, uOutputFlags); 670 706 } 671 707 break; … … 725 761 bool fVerbose = false; 726 762 uint32_t fFlags = VBOXSERVICETOOLBOXLSFLAG_NONE; 763 uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_NONE; 727 764 728 765 /* Init file list. */ … … 745 782 746 783 case 'l': /* Print long format. */ 747 f Flags |= VBOXSERVICETOOLBOXLSFLAG_LONG;784 fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_LONG; 748 785 break; 749 786 750 787 case LS_OPT_MACHINE_READABLE: 751 f Flags |= VBOXSERVICETOOLBOXLSFLAG_PARSEABLE;788 fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE; 752 789 break; 753 790 … … 799 836 800 837 /* Print magic/version. */ 801 if (f Flags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE)802 RTPrintf("hdr_id=vbt_ls%chdr_ver=%u%c", 0, 1 /* Version 1 */, 0);838 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) 839 VBoxServiceToolboxPrintStrmHeader("vbt_ls", 1 /* Stream version */); 803 840 804 841 PVBOXSERVICETOOLBOXPATHENTRY pNodeIt; … … 815 852 if (RT_SUCCESS(rc)) 816 853 { 817 rc = VBoxServiceToolbox LsPrintFsInfo(pNodeIt->pszName, strlen(pNodeIt->pszName),818 fFlags, &objInfo);854 rc = VBoxServiceToolboxPrintFsInfo(pNodeIt->pszName, strlen(pNodeIt->pszName), 855 fOutputFlags, &objInfo); 819 856 } 820 857 else … … 828 865 } 829 866 else 830 rc = VBoxServiceToolboxLsHandleDir(pNodeIt->pszName, fFlags); 867 rc = VBoxServiceToolboxLsHandleDir(pNodeIt->pszName, 868 fFlags, fOutputFlags); 831 869 if (RT_FAILURE(rc)) 832 870 RTMsgError("ls: Failed while enumerating '%s', rc=%Rrc\n", … … 834 872 } 835 873 836 if (f Flags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE) /* Output termination. */837 RTPrintf("%c%c%c%c", 0, 0, 0, 0);874 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */ 875 VBoxServiceToolboxPrintStrmTermination(); 838 876 } 839 877 else if (fVerbose) … … 984 1022 static const RTGETOPTDEF s_aOptions[] = 985 1023 { 986 { "--file-system", 'f', RTGETOPT_REQ_NOTHING }, 987 { "--dereference", 'L', RTGETOPT_REQ_NOTHING}, 988 { "--terse", 't', RTGETOPT_REQ_NOTHING}, 989 { "--verbose", 'v', RTGETOPT_REQ_NOTHING} 1024 { "--file-system", 'f', RTGETOPT_REQ_NOTHING }, 1025 { "--dereference", 'L', RTGETOPT_REQ_NOTHING }, 1026 { "--machinereadable", STAT_OPT_MACHINE_READABLE, RTGETOPT_REQ_NOTHING }, 1027 { "--terse", 't', RTGETOPT_REQ_NOTHING }, 1028 { "--verbose", 'v', RTGETOPT_REQ_NOTHING } 990 1029 }; 991 1030 … … 1005 1044 int rc = VINF_SUCCESS; 1006 1045 bool fVerbose = false; 1046 uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_LONG; /* Use long mode by default. */ 1007 1047 1008 1048 /* Init file list. */ … … 1025 1065 ValueUnion.pDef->pszLong); 1026 1066 rc = VERR_INVALID_PARAMETER; 1067 break; 1068 1069 case LS_OPT_MACHINE_READABLE: 1070 fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE; 1027 1071 break; 1028 1072 … … 1054 1098 if (RT_SUCCESS(rc)) 1055 1099 { 1100 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */ 1101 VBoxServiceToolboxPrintStrmHeader("vbt_stat", 1 /* Stream version */); 1102 1056 1103 PVBOXSERVICETOOLBOXPATHENTRY pNodeIt; 1057 1104 RTListForEach(&fileList, pNodeIt, VBOXSERVICETOOLBOXPATHENTRY, Node) 1058 1105 { 1059 1106 /* Only check for file existence for now. */ 1107 RTFSOBJINFO objInfo; 1108 int rc2 = VINF_SUCCESS; /* Temporary rc to keep original rc. */ 1060 1109 if (RTFileExists(pNodeIt->pszName)) 1061 1110 { 1062 /** @todo Do some more work (query size etc.) here later. 1063 * Not needed for now. */ 1111 RTFILE file; 1112 rc2 = RTFileOpen(&file, pNodeIt->pszName, RTFILE_O_OPEN | RTFILE_O_DENY_NONE | RTFILE_O_READ); 1113 if (RT_SUCCESS(rc2)) 1114 { 1115 rc2 = RTFileQueryInfo(file, &objInfo, RTFSOBJATTRADD_UNIX); 1116 if (RT_FAILURE(rc2)) 1117 RTMsgError("stat: Unable to query information for '%s', rc=%Rrc\n", 1118 pNodeIt->pszName, rc2); 1119 RTFileClose(file); 1120 } 1121 else 1122 RTMsgError("stat: Failed opening '%s', rc=%Rrc\n", 1123 pNodeIt->pszName, rc2); 1124 1125 if (RT_FAILURE(rc2)) 1126 rc = rc2; 1064 1127 } 1065 1128 else if (RTDirExists(pNodeIt->pszName)) 1066 1129 { 1067 /** @todo Do some more work (query size etc.) here later. 1068 * Not needed for now. */ 1130 PRTDIR pDir; 1131 rc2 = RTDirOpen(&pDir, pNodeIt->pszName); 1132 if (RT_SUCCESS(rc2)) 1133 { 1134 rc2 = RTDirQueryInfo(pDir, &objInfo, RTFSOBJATTRADD_UNIX); 1135 RTDirClose(pDir); 1136 } 1069 1137 } 1070 1138 else … … 1076 1144 * and keep failing rc. */ 1077 1145 } 1078 } 1146 1147 if (RT_SUCCESS(rc2)) 1148 { 1149 rc2 = VBoxServiceToolboxPrintFsInfo(pNodeIt->pszName, 1150 strlen(pNodeIt->pszName) /* cbName */, 1151 fOutputFlags, 1152 &objInfo); 1153 if (RT_FAILURE(rc2)) 1154 rc = rc2; 1155 } 1156 } 1157 1158 if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */ 1159 VBoxServiceToolboxPrintStrmTermination(); 1160 1161 /* At this point the overall result (success/failure) should be in rc. */ 1079 1162 1080 1163 if (RTListIsEmpty(&fileList))
Note:
See TracChangeset
for help on using the changeset viewer.