VirtualBox

Changeset 42727 in vbox


Ignore:
Timestamp:
Aug 9, 2012 7:15:02 PM (13 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
79936
Message:

Additions/common/VBoxService: extend vbox_mktemp to handle directories to, and to offer a secure option when the IPRT API is in place.

File:
1 edited

Legend:

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

    r42717 r42727  
    136136             "                             [--verbose|-v] [<file>...]\n"
    137137             "  rm <general options>       [-r|-R] <file>...\n"
    138              "  mktemp <general options>   <template>\n"
     138             "  mktemp <general options>   [--directory|-d] [--secure|-s]\n"
     139             "                             [--mode|-m <mode>] <template>\n"
    139140             "  mkdir <general options>    [--mode|-m] [--parents|-p]\n"
    140141             "                             [--verbose|-v] <directory>...\n"
     
    195196{
    196197    RTPrintf("%c%c%c%c", 0, 0, 0, 0);
     198}
     199
     200
     201/**
     202 * Parse a file mode string from the command line (currently octal only)
     203 * and print an error message and return an error if necessary.
     204 */
     205static int vboxServiceToolboxParseMode(const char *pcszMode, RTFMODE *pfMode)
     206{
     207    int rc = RTStrToUInt32Ex(pcszMode, NULL, 8 /* Base */, pfMode);
     208    if (RT_FAILURE(rc)) /* Only octet based values supported right now! */
     209        RTMsgError("Mode flag strings not implemented yet! Use octal numbers instead. (%s)\n",
     210                   pcszMode);
     211    return rc;
    197212}
    198213
     
    11181133static char g_paszMkTempHelp[] =
    11191134    "  VBoxService [--use-toolbox] vbox_mktemp <general options> <template>\n\n"
    1120     "Create a temporary directory based on the template supplied. The last string\n"
     1135    "Create a temporary directory based on the template supplied. The first string\n"
    11211136    "of consecutive 'X' characters in the template will be replaced to form a unique\n"
    1122     "name for the directory.\n"
     1137    "name for the directory.  The template must contain an absolute path.  The\n"
     1138    "default creation mode is 0600 for files and 0700 for directories.\n"
     1139    "Options:\n\n"
     1140    "  [--directory|-d]           Create a directory instead of a file.\n"
     1141    "  [--mode|-m <mode>]         Create the object with mode <mode>.\n"
     1142    "  [--secure|-s]              Fail if the object cannot be created securely.\n"
    11231143    "\n";
    11241144
     
    11631183        { "--machinereadable", VBOXSERVICETOOLBOXOPT_MACHINE_READABLE,
    11641184          RTGETOPT_REQ_NOTHING },
     1185        { "--directory", 'd', RTGETOPT_REQ_NOTHING },
     1186        { "--mode",      'm', RTGETOPT_REQ_STRING },
     1187        { "--secure",    's', RTGETOPT_REQ_NOTHING },
     1188    };
     1189
     1190    enum
     1191    {
     1192        /* Isn't that a bit long?  s/VBOXSERVICETOOLBOX/VSTB/ ? */
     1193        /** Create a temporary directory instead of a temporary file. */
     1194        VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY = RT_BIT_32(0),
     1195        /** Only create the temporary object if the operation is expected
     1196         * to be secure.  Not guaranteed to be supported on a particular
     1197         * set-up. */
     1198        VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE    = RT_BIT_32(1)
    11651199    };
    11661200
     
    11761210    uint32_t fFlags       = 0;
    11771211    uint32_t fOutputFlags = 0;
    1178     int cNonOptions       = 0;
    1179     char *pszName;
     1212    int      cNonOptions  = 0;
     1213    RTFMODE  fMode        = 0700;
     1214    bool     fModeSet     = false;
     1215    char    *pszName;
    11801216
    11811217    while (   (ch = RTGetOpt(&GetState, &ValueUnion))
     
    11981234                break;
    11991235
     1236            case 'd':
     1237                fFlags |= VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY;
     1238                break;
     1239
     1240            case 'm':
     1241                rc = vboxServiceToolboxParseMode(ValueUnion.psz, &fMode);
     1242                if (RT_FAILURE(rc))
     1243                    return RTEXITCODE_SYNTAX;
     1244                fModeSet = true;
     1245#ifndef RT_OS_WINDOWS
     1246                umask(0); /* RTDirCreate workaround */
     1247#endif
     1248                break;
     1249            case 's':
     1250                fFlags |= VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE;
     1251                break;
     1252
    12001253            case VINF_GETOPT_NOT_OPTION:
    12011254                /* RTGetOpt will sort these to the end of the argv vector so
     
    12201273    }
    12211274
     1275    if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE && fModeSet)
     1276    {
     1277        toolboxMkTempReport("'-s' and '-m' parameters cannot be used together.\n", "",
     1278                            true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
     1279        return RTEXITCODE_SYNTAX;
     1280    }
    12221281    /* We need exactly one template, containing at least one 'X'. */
    12231282    if (RT_SUCCESS(rc) && cNonOptions != 1)
     
    12251284        toolboxMkTempReport("Please specify exactly one template.\n", "",
    12261285                            true, VERR_INVALID_PARAMETER, fOutputFlags, &rc);
    1227         return RTEXITCODE_FAILURE;
     1286        return RTEXITCODE_SYNTAX;
    12281287    }
    12291288    pszName = argv[argc - 1];
     
    12381297    if (RT_SUCCESS(rc))
    12391298    {
    1240         rc = RTDirCreateTemp(pszName, 0700);
    1241         toolboxMkTempReport("Created temporary directory '%s'.\n",
    1242                             pszName, RT_SUCCESS(rc), rc, fOutputFlags, NULL);
    1243         toolboxMkTempReport("The following error occurred while creating a temporary directory with template '%s': %Rrc.\n",
    1244                             pszName, RT_FAILURE(rc), rc, fOutputFlags, NULL);
     1299        if (fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_DIRECTORY)
     1300        {
     1301            rc =   fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE
     1302                 ? RTDirCreateTempSecure(pszName)
     1303                 : RTDirCreateTemp(pszName, fMode);
     1304            toolboxMkTempReport("Created temporary directory '%s'.\n",
     1305                                pszName, RT_SUCCESS(rc), rc, fOutputFlags, NULL);
     1306            /* RTDirCreateTemp[Secure] sets the template to "" on failure. */
     1307            toolboxMkTempReport("The following error occurred while creating the temporary directory%s: %Rrc.\n",
     1308                                pszName, RT_FAILURE(rc), rc, fOutputFlags, NULL);
     1309        }
     1310        else
     1311        {
     1312            rc =   fFlags & VBOXSERVICETOOLBOXMKTEMPFLAG_SECURE
     1313                 ? RTFileCreateTempSecure(pszName)
     1314                 : RTFileCreateTemp(pszName, fMode);
     1315            toolboxMkTempReport("Created temporary file '%s'.\n",
     1316                                pszName, RT_SUCCESS(rc), rc, fOutputFlags, NULL);
     1317            /* RTFileCreateTemp[Secure] sets the template to "" on failure. */
     1318            toolboxMkTempReport("The following error occurred while creating the temporary file%s: %Rrc.\n",
     1319                                pszName, RT_FAILURE(rc), rc, fOutputFlags, NULL);
     1320        }
    12451321        if (fOutputFlags & VBOXSERVICETOOLBOXOUTPUTFLAG_PARSEABLE) /* Output termination. */
    12461322            VBoxServiceToolboxPrintStrmTermination();
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