VirtualBox

Changeset 37860 in vbox for trunk/src/VBox/Additions/common


Ignore:
Timestamp:
Jul 11, 2011 9:48:37 AM (14 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
72757
Message:

VBoxService/Toolbox: Implemented output for vbox_stat, now using a generic output routine for file system objects.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceToolBox.cpp

    r37814 r37860  
    4444
    4545
     46/** Options indices for "vbox_cat". */
    4647#define CAT_OPT_NO_CONTENT_INDEXED              1000
    47 
     48/** Options indices for "vbox_ls". */
    4849#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". */
    5053typedef enum VBOXSERVICETOOLBOXLSFLAG
    5154{
    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. */
     61typedef enum VBOXSERVICETOOLBOXOUTPUTFLAG
     62{
     63    VBOXSERVICETOOLBOXOUTPUTFLAG_NONE =         0x0,
     64    VBOXSERVICETOOLBOXOUTPUTFLAG_LONG =         0x1,
     65    VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE =    0x2
     66} VBOXSERVICETOOLBOXOUTPUTFLAG, *PVBOXSERVICETOOLBOXOUTPUTFLAG;
    5867
    5968/* Enable the following define to be able to debug/invoke the toolbox
     
    132141}
    133142
     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 */
     151static 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 */
     162static void VBoxServiceToolboxPrintStrmTermination()
     163{
     164    RTPrintf("%c%c%c%c", 0, 0, 0, 0);
     165}
    134166
    135167/**
     
    407439 * @param   pszName                     Object name.
    408440 * @param   cbName                      Size of object name.
    409  * @param   uFlags                      Output / handling flags.
     441 * @param   uOutputFlags                Output / handling flags of type VBOXSERVICETOOLBOXOUTPUTFLAG.
    410442 * @param   pObjInfo                    Pointer to object information.
    411443 */
    412 static int VBoxServiceToolboxLsPrintFsInfo(const char *pszName, uint16_t cbName, uint32_t uFlags,
    413                                            PRTFSOBJINFO pObjInfo)
     444static int VBoxServiceToolboxPrintFsInfo(const char *pszName, uint16_t cbName,
     445                                         uint32_t uOutputFlags,
     446                                         PRTFSOBJINFO pObjInfo)
    414447{
    415448    AssertPtrReturn(pszName, VERR_INVALID_POINTER);
     
    435468    /** @todo sticy bits++ */
    436469
    437     if (!(uFlags & VBOXSERVICETOOLBOXLSFLAG_LONG))
    438     {
    439         if (uFlags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE)
     470    if (!(uOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_LONG))
     471    {
     472        if (uOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
    440473        {
    441474            /** @todo Skip node_id if not present/available! */
     
    448481                     cFileType, cbName, pszName);
    449482
    450         if (uFlags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE) /* End of data block. */
     483        if (uOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* End of data block. */
    451484            RTPrintf("%c%c", 0, 0);
    452485    }
    453486    else
    454487    {
    455         if (uFlags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE)
     488        if (uOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
    456489        {
    457490            RTPrintf("ftype=%c%c", cFileType, 0);
     
    565598 * @param   pszDir                  Directory (path) to ouptut.
    566599 * @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 */
     602static int VBoxServiceToolboxLsHandleDir(const char *pszDir,
     603                                         uint32_t uFlags, uint32_t uOutputFlags)
    569604{
    570605    AssertPtrReturn(pszDir, VERR_INVALID_PARAMETER);
    571606
    572     if (uFlags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE)
     607    if (uFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE)
    573608        RTPrintf("dname=%s%c", pszDir, 0);
    574609    else if (uFlags & VBOXSERVICETOOLBOXLSFLAG_RECURSIVE)
     
    632667        RTListForEach(&dirList, pNodeIt, VBOXSERVICETOOLBOXDIRENTRY, Node)
    633668        {
    634             rc = VBoxServiceToolboxLsPrintFsInfo(pNodeIt->dirEntry.szName, pNodeIt->dirEntry.cbName,
    635                                                  uFlags,
    636                                                  &pNodeIt->dirEntry.Info);
     669            rc = VBoxServiceToolboxPrintFsInfo(pNodeIt->dirEntry.szName, pNodeIt->dirEntry.cbName,
     670                                               uOutputFlags,
     671                                               &pNodeIt->dirEntry.Info);
    637672            if (RT_FAILURE(rc))
    638673                break;
     
    667702                                            pszDir, pNodeIt->dirEntry.szName);
    668703                            if (RT_SUCCESS(rc))
    669                                 rc = VBoxServiceToolboxLsHandleDir(szPath, uFlags);
     704                                rc = VBoxServiceToolboxLsHandleDir(szPath,
     705                                                                   uFlags, uOutputFlags);
    670706                        }
    671707                        break;
     
    725761    bool fVerbose = false;
    726762    uint32_t fFlags = VBOXSERVICETOOLBOXLSFLAG_NONE;
     763    uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_NONE;
    727764
    728765    /* Init file list. */
     
    745782
    746783            case 'l': /* Print long format. */
    747                 fFlags |= VBOXSERVICETOOLBOXLSFLAG_LONG;
     784                fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_LONG;
    748785                break;
    749786
    750787            case LS_OPT_MACHINE_READABLE:
    751                 fFlags |= VBOXSERVICETOOLBOXLSFLAG_PARSEABLE;
     788                fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
    752789                break;
    753790
     
    799836
    800837        /* Print magic/version. */
    801         if (fFlags & 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 */);
    803840
    804841        PVBOXSERVICETOOLBOXPATHENTRY pNodeIt;
     
    815852                    if (RT_SUCCESS(rc))
    816853                    {
    817                         rc = VBoxServiceToolboxLsPrintFsInfo(pNodeIt->pszName, strlen(pNodeIt->pszName),
    818                                                              fFlags, &objInfo);
     854                        rc = VBoxServiceToolboxPrintFsInfo(pNodeIt->pszName, strlen(pNodeIt->pszName),
     855                                                           fOutputFlags, &objInfo);
    819856                    }
    820857                    else
     
    828865            }
    829866            else
    830                 rc = VBoxServiceToolboxLsHandleDir(pNodeIt->pszName, fFlags);
     867                rc = VBoxServiceToolboxLsHandleDir(pNodeIt->pszName,
     868                                                   fFlags, fOutputFlags);
    831869            if (RT_FAILURE(rc))
    832870                RTMsgError("ls: Failed while enumerating '%s', rc=%Rrc\n",
     
    834872        }
    835873
    836         if (fFlags & VBOXSERVICETOOLBOXLSFLAG_PARSEABLE) /* Output termination. */
    837             RTPrintf("%c%c%c%c", 0, 0, 0, 0);
     874        if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
     875            VBoxServiceToolboxPrintStrmTermination();
    838876    }
    839877    else if (fVerbose)
     
    9841022    static const RTGETOPTDEF s_aOptions[] =
    9851023    {
    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 }
    9901029    };
    9911030
     
    10051044    int rc = VINF_SUCCESS;
    10061045    bool fVerbose = false;
     1046    uint32_t fOutputFlags = VBOXSERVICETOOLBOXOUTPUTFLAG_LONG; /* Use long mode by default. */
    10071047
    10081048    /* Init file list. */
     
    10251065                           ValueUnion.pDef->pszLong);
    10261066                rc = VERR_INVALID_PARAMETER;
     1067                break;
     1068
     1069            case LS_OPT_MACHINE_READABLE:
     1070                fOutputFlags |= VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE;
    10271071                break;
    10281072
     
    10541098    if (RT_SUCCESS(rc))
    10551099    {
     1100        if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
     1101            VBoxServiceToolboxPrintStrmHeader("vbt_stat", 1 /* Stream version */);
     1102
    10561103        PVBOXSERVICETOOLBOXPATHENTRY pNodeIt;
    10571104        RTListForEach(&fileList, pNodeIt, VBOXSERVICETOOLBOXPATHENTRY, Node)
    10581105        {
    10591106            /* Only check for file existence for now. */
     1107            RTFSOBJINFO objInfo;
     1108            int rc2 = VINF_SUCCESS; /* Temporary rc to keep original rc. */
    10601109            if (RTFileExists(pNodeIt->pszName))
    10611110            {
    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;
    10641127            }
    10651128            else if (RTDirExists(pNodeIt->pszName))
    10661129            {
    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                }
    10691137            }
    10701138            else
     
    10761144                 * and keep failing rc. */
    10771145            }
    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. */
    10791162
    10801163        if (RTListIsEmpty(&fileList))
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette