Exemple #1
0
int
CPNameConvertTo(char const *nameIn, // IN:  Buf to convert
                size_t bufOutSize,  // IN:  Size of the output buffer
                char *bufOut,       // OUT: Output buffer
                char pathSep)       // IN:  path separator to use
{
   char *origOut = bufOut;
   char const *endOut = bufOut + bufOutSize;
   size_t cpNameLength = 0;

   ASSERT(nameIn);
   ASSERT(bufOut);

   /* Skip any path separators at the beginning of the input string */
   while (*nameIn == pathSep) {
      nameIn++;
   }

    /*
     * Copy the string to the output buf, converting all path separators into '\0'.
     * Collapse multiple consecutive path separators into a single one since
     * CPName_GetComponent can't handle consecutive path separators.
     */
   while (*nameIn != '\0' && bufOut < endOut) {
      if (*nameIn == pathSep) {
         *bufOut = '\0';
         do {
            nameIn++;
         } while (*nameIn == pathSep);
      } else {
         *bufOut = *nameIn;
         nameIn++;
      }
      bufOut++;
   }

   /*
    * NUL terminate. XXX This should go away.
    *
    * When we get rid of NUL termination here, this test should
    * also change to "if (*nameIn != '\0')".
    */
   if (bufOut == endOut) {
      return -1;
   }
   *bufOut = '\0';

   /* Path name size should not require more than 4 bytes. */
   ASSERT((bufOut - origOut) <= 0xFFFFFFFF);

   /* If there were any trailing path separators, dont count them [krishnan] */
   cpNameLength = bufOut - origOut;
   while ((cpNameLength >= 1) && (origOut[cpNameLength - 1] == 0)) {
      cpNameLength--;
   }
   cpNameLength = HgfsEscape_Undo(origOut, cpNameLength);

   /* Return number of bytes used */
   return (int) cpNameLength;
}
char *
HgfsUri_ConvertFromPathToHgfsUri(const char *pathName,  // IN: path to convert
                                 Bool hgfsOnly)         // IN
{
   char *shareUri = NULL;
   Bool isHgfsName = FALSE;
   char *sharesDefaultRootPath = NULL;

   /* We can only operate on full paths. */
   if (pathName[0] != DIRSEPC) {
      return shareUri;
   }

   /* Retrieve the servername & share name in use. */
   if (!HgfsHlpr_QuerySharesDefaultRootPath(&sharesDefaultRootPath)) {
      Debug("%s: Unable to query shares default root path\n", __FUNCTION__);
      goto exit;
   }

   if (Unicode_StartsWith(pathName, sharesDefaultRootPath)) {
      char *relativeSharePath = NULL;
      char *escapedSharePath = NULL;
      UnicodeIndex relativePathStart = strlen(sharesDefaultRootPath);
      if (   strlen(pathName) > relativePathStart
          && pathName[relativePathStart] == DIRSEPC) {
         relativePathStart++;
      }
      relativeSharePath = Unicode_RemoveRange(pathName, 0, relativePathStart);
      HgfsEscape_Undo(relativeSharePath, strlen(relativeSharePath) + 1);
      escapedSharePath = g_uri_escape_string(relativeSharePath, "/", FALSE);
      shareUri = Unicode_Append(GHI_HGFS_SHARE_URL_UTF8, escapedSharePath);
      g_free(escapedSharePath);
      free(relativeSharePath);
      isHgfsName = TRUE;
   }

exit:
   if (!isHgfsName && !hgfsOnly) {
      /* Only convert non-hgfs file name if hgfsOnly is not set. */
      char *escapedPath = g_uri_escape_string(pathName, "/", FALSE);
      shareUri = Str_Asprintf(NULL,
                              "file://%s",
                               escapedPath);
      g_free(escapedPath);
   }
   HgfsHlpr_FreeSharesRootPath(sharesDefaultRootPath);
   return shareUri;
}