Example #1
0
// create short icon label from full path/url
char *XFE_ComposeAttachFolderView::parseItemLabel(const char *url)
{
    // (alastair) code taken from libmsg/msgsend.cpp - be nice just to call into libmsg
    // modified slightly to avoid bug of trailing / generating empty name
    // modified to return truncated label for mail/news URL's

    if (!url || strlen(url)==0)
        return XP_STRDUP("(null)");

    char *s;
    char *s2;
    char *s3;
    
    /* If we know the URL doesn't have a sensible file name in it,
       don't bother emitting a content-disposition. */
    if (!strncasecomp (url, "news:", 5))
        return XP_STRDUP("news:");
    if (!strncasecomp (url, "snews:", 6))
        return XP_STRDUP("snews:");    
    if (!strncasecomp (url, "mailbox:", 8))
        return XP_STRDUP("mailbox:");

    char *tmpLabel = XP_STRDUP(url);

    s=tmpLabel;
    /* remove trailing / or \ */
    int len=strlen(s);
    if (s[len-1]=='/' || s[len-1]=='\\')
        s[len-1]='\0';
    
    s2 = XP_STRCHR (s, ':');
    if (s2) s = s2 + 1;
    /* Take the part of the file name after the last / or \ */
    s2 = XP_STRRCHR (s, '/');
    if (s2) s = s2+1;
    s2 = XP_STRRCHR (s, '\\');
    if (s2) s = s2+1;

    /* if it's a non-file url, strip off any named anchors or search data */
    if (XP_STRNCASECMP(url,"file:",5)!=0 && url[0]!='/') {
        /* Now trim off any named anchors or search data. */
        s3 = XP_STRCHR (s, '?');
        if (s3) *s3 = 0;
        s3 = XP_STRCHR (s, '#');
        if (s3) *s3 = 0;
    }

    /* Now lose the %XX crap. */
    NET_UnEscape (s);

    char *retLabel=XP_STRDUP(s);
    XP_FREE(tmpLabel);
    
    return retLabel;
}
Example #2
0
/* to a just a filename */
static void DIR_ConvertServerFileName(DIR_Server* pServer)
{
  char* leafName = pServer->fileName;
  char* newLeafName = nsnull;
#if defined(XP_WIN) || defined(XP_OS2)
  /* jefft -- bug 73349 This is to allow users share same address book.
   * It only works if the user specify a full path filename.
   */
#ifdef XP_FileIsFullPath
  if (! XP_FileIsFullPath(leafName))
    newLeafName = XP_STRRCHR (leafName, '\\');
#endif /* XP_FileIsFullPath */
#else
  newLeafName = strrchr(leafName, '/');
#endif
    pServer->fileName = newLeafName ? strdup(newLeafName + 1) : strdup(leafName);
  if (leafName) PR_Free(leafName);
}
Example #3
0
// nyi - extract nice file name from title or url
char *XFE_URLDesktopType::getFilename(int pos)
{
    if (pos<0 || pos>=_numItems || _url[pos]==NULL)
        return XP_STRDUP("unknown");

    // (alastair) code taken from libmsg/msgsend.cpp - be nice just to call into libmsg
    // modified slightly to avoid bug of trailing / generating empty name
    // modified to return truncated label for mail/news URL's

    const char *url=_url[pos];
    
    if (!url || strlen(url)==0)
        return XP_STRDUP("(null)");

    char *s;
    char *s2;
    char *s3;
   
    /* If we know the URL doesn't have a sensible file name in it,
       don't bother emitting a content-disposition. */
    if (!XP_STRNCASECMP(url, "news:", 5))
        return XP_STRDUP("news:");
    if (!XP_STRNCASECMP(url, "snews:", 6))
        return XP_STRDUP("snews:");
    if (!XP_STRNCASECMP(url, "mailbox:", 8))
        return XP_STRDUP("mailbox:");

    char *tmpLabel = XP_STRDUP(url);

    s=tmpLabel;
    /* remove trailing / or \ */
    int len=strlen(s);
    if (s[len-1]=='/' || s[len-1]=='\\')
        s[len-1]='\0';

    s2 = XP_STRCHR (s, ':');
    if (s2) s = s2 + 1;
    /* Take the part of the file name after the last / or \ */
    if (s2 = XP_STRRCHR (s, '/'))
        s = s2+1;
    else if (s2 = XP_STRRCHR (s, '\\'))
        s = s2+1;

    /* if it's a non-file url do some additional massaging */
    if (XP_STRNCASECMP(url,"file:",5)!=0 && url[0]!='/') {
        /* Trim off any named anchors or search data. */
        s3 = XP_STRCHR (s, '?');
        if (s3) *s3 = 0;
        s3 = XP_STRCHR (s, '#');
        if (s3) *s3 = 0;

        /* Check for redundant document name.
         * Use previous URL component to provide more meaningful file name.
         */
        if (s2 &&
            XP_STRCASECMP(s,"index.html")==0 ||
            XP_STRCASECMP(s,"index.htm")==0 ||
            XP_STRCASECMP(s,"index.cgi")==0 ||
            XP_STRCASECMP(s,"index.shtml")==0 ||
            XP_STRCASECMP(s,"home.html")==0 ||
            XP_STRCASECMP(s,"home.htm")==0 ||
            XP_STRCASECMP(s,"home.cgi")==0 ||
            XP_STRCASECMP(s,"home.shtml")==0) {
            /* Trim redundant component and try again */
            *s2='\0';
            s=tmpLabel;
            /* Redo: Take the part of the file name after the last / or \ */
            if (s2 = XP_STRRCHR (s, '/'))
                s = s2+1;
            else if (s2 = XP_STRRCHR (s, '\\'))
                s = s2+1;
        }
    }

    /* Now lose the %XX crap. */
    NET_UnEscape (s);

    char *retLabel=XP_STRDUP(s);
    XP_FREE(tmpLabel);

    return retLabel;
}
Example #4
0
/* returns a unmalloced static string
 * that is only available for temporary use.
 */
PUBLIC char *
xp_FileName (const char *name, XP_FileType type, char* buf, char* configBuf)
{
    const char *conf_dir = xp_unix_config_directory(configBuf);

    switch (type)
    {
    case xpSARCacheIndex:
    {
        const char *sar_cache_dir = FE_SARCacheDir;
        if (!sar_cache_dir || !*sar_cache_dir)
            sar_cache_dir = conf_dir;
        if (sar_cache_dir [strlen (sar_cache_dir) - 1] == '/')
            sprintf (buf, "%.900sarchive.fat", sar_cache_dir);
        else
            sprintf (buf, "%.900s/archive.fat", sar_cache_dir);

        name = buf;
        break;
    }
    case xpSARCache:
    {
        /* WH_TempName() returns relative pathnames for the cache files,
           so that relative paths get written into the cacheFAT database.
           WH_FileName() converts them to absolute if they aren't already
           so that the logic of XP_FileOpen() and XP_FileRename() and who
           knows what else is simpler.
         */
        if (name != NULL && *name == '/')
            break ;

        {
            char *tmp = FE_SARCacheDir;
            if (!tmp || !*tmp) tmp = "/tmp";
            if (tmp [strlen (tmp) - 1] == '/')
                sprintf (buf, "%.500s%.500s", tmp, name);
            else
                sprintf (buf, "%.500s/%.500s", tmp, name);
            name = buf;
        }
        break;
    }
    case xpCacheFAT:
    {
        const char *cache_dir = FE_CacheDir;
        if (!cache_dir || !*cache_dir)
            cache_dir = conf_dir;
        if (cache_dir [strlen (cache_dir) - 1] == '/')
            sprintf (buf, "%.900sindex.db", cache_dir);
        else
            sprintf (buf, "%.900s/index.db", cache_dir);

        name = buf;
        break;
    }
    case xpCache:
    {
        /* WH_TempName() returns relative pathnames for the cache files,
           so that relative paths get written into the cacheFAT database.
           WH_FileName() converts them to absolute if they aren't already
           so that the logic of XP_FileOpen() and XP_FileRename() and who
           knows what else is simpler.
         */
        if (*name != '/')
        {
            char *tmp = FE_CacheDir;
            if (!tmp || !*tmp) tmp = "/tmp";
            if (tmp [strlen (tmp) - 1] == '/')
                sprintf (buf, "%.500s%.500s", tmp, name);
            else
                sprintf (buf, "%.500s/%.500s", tmp, name);
            name = buf;
        }
        break;
    }

    case xpHTTPCookie:
    {
#ifndef OLD_UNIX_FILES
        sprintf (buf, "%.900s/cookies", conf_dir);
#else  /* OLD_UNIX_FILES */
        sprintf (buf, "%.900s/.netscape-cookies", conf_dir);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;
    }
    case xpRegistry:
    {
        if ( name == NULL || *name == '\0' ) {
#ifndef OLD_UNIX_FILES
            sprintf (buf, "%.900s/registry", conf_dir);
#else  /* OLD_UNIX_FILES */
            sprintf (buf, "%.900s/.netscape-registry", conf_dir);
#endif /* OLD_UNIX_FILES */
            name = buf;
        }
        else {
            XP_ASSERT( name[0] == '/' );
        }
        break;
    }
    case xpProxyConfig:
    {
        sprintf(buf, "%.900s/proxyconf", conf_dir);
        name = buf;
        break;
    }
    case xpTemporary:
    {
        if (*name != '/')
        {
            char *tmp = FE_TempDir;
            if (!tmp || !*tmp) tmp = "/tmp";
            if (tmp [strlen (tmp) - 1] == '/')
                sprintf (buf, "%.500s%.500s", tmp, name);
            else
                sprintf (buf, "%.500s/%.500s", tmp, name);
            name = buf;
        }
        break;
    }
    case xpNewsRC:
    case xpSNewsRC:
    case xpTemporaryNewsRC:
    {
        /* In this case, `name' is "" or "host" or "host:port". */

        char *home = getenv ("HOME");
        const char *newsrc_dir = ((FE_UserNewsRC && *FE_UserNewsRC)
                                  ? FE_UserNewsRC
                                  : (home ? home : ""));
        const char *basename = (type == xpSNewsRC ? ".snewsrc" : ".newsrc");
        const char *suffix = (type == xpTemporaryNewsRC ? ".tmp" : "");
        if (*name)
            sprintf (buf, "%.800s%.1s%.8s-%.128s%.4s",
                     newsrc_dir,
                     (newsrc_dir[XP_STRLEN(newsrc_dir)-1] == '/' ? "" : "/"),
                     basename, name, suffix);
        else
            sprintf (buf, "%.800s%.1s%.128s%.4s",
                     newsrc_dir,
                     (newsrc_dir[XP_STRLEN(newsrc_dir)-1] == '/' ? "" : "/"),
                     basename, suffix);

        name = buf;
        break;
    }
    case xpNewsgroups:
    case xpSNewsgroups:
    {
#ifndef OLD_UNIX_FILES
        sprintf (buf, "%.800s/%snewsgroups-%.128s",
                 conf_dir,
                 type == xpSNewsgroups ? "s" : "",
                 name);
#else  /* OLD_UNIX_FILES */
        sprintf (buf, "%.800s/.netscape-%snewsgroups-%.128s",
                 conf_dir,
                 type == xpSNewsgroups ? "s" : "",
                 name);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;
    }

    case xpExtCacheIndex:
#ifndef OLD_UNIX_FILES
        sprintf (buf, "%.900s/cachelist", conf_dir);
#else  /* OLD_UNIX_FILES */
        sprintf (buf, "%.900s/.netscape-cache-list", conf_dir);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;

    case xpGlobalHistory:
        name = FE_GlobalHist;
        break;

    case xpCertDB:
#ifndef OLD_UNIX_FILES
        if ( name ) {
            sprintf (buf, "%.900s/cert%s.db", conf_dir, name);
        } else {
            sprintf (buf, "%.900s/cert.db", conf_dir);
        }
#else  /* OLD_UNIX_FILES */
        sprintf (buf, "%.900s/.netscape-certdb", conf_dir);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;
    case xpCertDBNameIDX:
#ifndef OLD_UNIX_FILES
        sprintf (buf, "%.900s/cert-nameidx.db", conf_dir);
#else  /* OLD_UNIX_FILES */
        sprintf (buf, "%.900s/.netscape-certdb-nameidx", conf_dir);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;
    case xpKeyDB:
#ifndef OLD_UNIX_FILES
        if ( name ) {
            sprintf (buf, "%.900s/key%s.db", conf_dir, name);
        } else {
            sprintf (buf, "%.900s/key.db", conf_dir);
        }
#else  /* OLD_UNIX_FILES */
        sprintf (buf, "%.900s/.netscape-keydb", conf_dir);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;
    case xpSecModuleDB:
        sprintf (buf, "%.900s/secmodule.db", conf_dir);
        name = buf;
        break;

    case xpSignedAppletDB:
    {
#ifndef OLD_UNIX_FILES
        if ( name ) {
            sprintf (buf, "%.900s/signedapplet%s.db", conf_dir, name);
        } else {
            sprintf (buf, "%.900s./signedapplet.db", conf_dir);
        }
#else  /* OLD_UNIX_FILES */
        sprintf (buf, "%.900s/.netscape-signedappletdb", conf_dir);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;
    }

    case xpFileToPost:
    case xpSignature:
        /* These are always absolute pathnames.
         * BUT, the user can type in whatever so
         * we can't assert if it doesn't begin
         * with a slash
         */
        break;

    case xpExtCache:
    case xpKeyChain:
    case xpURL:
    case xpHotlist:
    case xpBookmarks:
    case xpMimeTypes:
    case xpSocksConfig:
    case xpMailFolder:
#ifdef BSDI
        /* In bsdi, mkdir fails if the directory name is terminated
         * with a '/'. - dp
         */
        if (name[strlen(name)-1] == '/') {
            strcpy(buf, name);
            buf[strlen(buf)-1] = '\0';
            name = buf;
        }
#endif
#ifndef MCC_PROXY
        /*
         * These are always absolute pathnames for the Navigator.
         * Only the proxy (servers) may have pathnames relative
         * to their current working directory (the servers chdir
         * to their admin/config directory on startup.
         *
         */
        if (name) XP_ASSERT (name[0] == '/');
#endif	/* ! MCC_PROXY */

        break;

    case xpMailFolderSummary:
        /* Convert /a/b/c/foo to /a/b/c/.foo.summary (note leading dot) */
    {
        const char *slash;
        slash = strrchr (name, '/');
        if (name) XP_ASSERT (name[0] == '/');
        XP_ASSERT (slash);
        if (!slash) return 0;
        XP_MEMCPY (buf, name, slash - name + 1);
        buf [slash - name + 1] = '.';
        XP_STRCPY (buf + (slash - name) + 2, slash + 1);
        XP_STRCAT (buf, ".summary");
        name = buf;
        break;
    }

    case xpAddrBookNew:
        /* Convert foo.db to /a/b/c/foo.db */
    {
        if ( name ) {
            sprintf (buf, "%.900s/%s", conf_dir, name);
        } else {
            sprintf (buf, "%.900s/abook.nab", conf_dir);
        }
#if defined(DEBUG_tao)
        printf("\n  xpAddrBookNew, xp_FileName, buf=%s\n", buf);
#endif
        name = buf;

        break;
    }

    case xpAddrBook:
        /* Convert /a/b/c/foo to /a/b/c/foo.db (note leading dot) */
    {
        /* Tao_27jan97
         */
        char *dot = NULL;
        int len = 0;
        const char *base = NULL;

        if (name)
            XP_ASSERT (name[0] == '/');

        dot = XP_STRRCHR(name, '.');
        if (dot) {

            len = dot - name + 1;
            XP_STRNCPY_SAFE(buf, name, len);
        }/* if */

        XP_STRCAT (buf, ".nab");


        /* Tao_02jun97 don't convert addrbook.db
         * reuse len, dot
         */
        base = XP_STRRCHR(name, '/');
        if (base && *base == '/')
            base++;

#if defined(DEBUG_tao)
        printf("\n++++  xpAddrBook, before xp_FileName=%s\n", name);
#endif

        if (!base || XP_STRCMP(base, "addrbook.db"))
            /* not old addrbook.db file
             */
            name = buf;
#if defined(DEBUG_tao)
        printf("\n  xpAddrBook, xp_FileName=%s\n", name);
#endif
        break;
    }

    case xpVCardFile:
        /* Convert /a/b/c/foo to /a/b/c/foo.vcf (note leading dot) */
    {
#if 1
        /* Tao_27jan97
         */
        char *dot = NULL;
        int len = 0;

        if (name)
            XP_ASSERT (name[0] == '/');

        dot = XP_STRRCHR(name, '.');
        if (dot) {
            len = dot - name + 1;
            XP_STRNCPY_SAFE(buf, name, len);
        }/* if */

        XP_STRCAT (buf, ".vcf");
        name = buf;
#if defined(DEBUG_tao_)
        printf("\n  xp_FileName=%s\n", name);
#endif
#else
        const char *slash;
        slash = strrchr (name, '/');
        if (name) XP_ASSERT (name[0] == '/');
        XP_ASSERT (slash);
        if (!slash) return 0;
        XP_MEMCPY (buf, name, slash - name + 1);
        XP_STRCAT (buf, ".vcf");
        name = buf;
#endif
        break;
    }

    case xpLDIFFile:
        /* Convert /a/b/c/foo to /a/b/c/foo.ldif (note leading dot) */
    {
#if 1
        /* Tao_27jan97
         */
        char *dot = NULL;
        int len = 0;

        if (name)
            XP_ASSERT (name[0] == '/');

        dot = XP_STRRCHR(name, '.');
        if (dot) {
            len = dot - name + 1;
            XP_STRNCPY_SAFE(buf, name, len);
        }/* if */

        XP_STRCAT (buf, ".ldif");
        name = buf;
#if defined(DEBUG_tao_)
        printf("\n  xp_FileName=%s\n", name);
#endif

#else
        const char *slash;
        slash = strrchr (name, '/');
        if (name) XP_ASSERT (name[0] == '/');
        XP_ASSERT (slash);
        if (!slash) return 0;
        XP_MEMCPY (buf, name, slash - name + 1);
        XP_STRCAT (buf, ".ldif");
        name = buf;
#endif
        break;
    }

    case xpJSMailFilters:
        sprintf(buf, "%.900s/filters.js", conf_dir);
        name = buf;
        break;

    case xpJSHTMLFilters:
        sprintf(buf, "%.900s/hook.js", conf_dir);
        name = buf;
        break;

    case xpNewsSort:
        sprintf(buf, "%.800s/xover-cache/%.128snetscape-newsrule", conf_dir, name);
        break;
    case xpMailSort:
#ifndef OLD_UNIX_FILES
        sprintf(buf, "%.900s/mailrule", conf_dir);
#else  /* OLD_UNIX_FILES */
        sprintf(buf, "%.900s/.netscape-mailrule", conf_dir);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;
    case xpMailPopState:
#ifndef OLD_UNIX_FILES
        sprintf(buf, "%.900s/popstate", conf_dir);
#else  /* OLD_UNIX_FILES */
        sprintf(buf, "%.900s/.netscape-popstate", conf_dir);
#endif /* OLD_UNIX_FILES */
        name = buf;
        break;
    case xpMailFilterLog:
        sprintf(buf, "%.900s/.netscape-mailfilterlog", conf_dir);
        name = buf;
        break;
    case xpNewsFilterLog:
        sprintf(buf, "%.900s/.netscape-newsfilterlog", conf_dir);
        name = buf;
        break;
    case xpMailSubdirectory:
    {
        char * pEnd = strrchr(name, '/');
        strcpy(buf, name);

        /* strip off the extension */
        if(!pEnd)
            pEnd = buf;

        pEnd = strchr(pEnd, '.');
        if(pEnd)
            *pEnd = '\0';
        strcat(buf, ".sbd/");
        name = buf;
    }
    break;
    case xpXoverCache:
        sprintf(buf, "%.800s/xover-cache/%.128s", conf_dir, name);
        name = buf;
        break;

    case xpNewsHostDatabase:
        sprintf(buf, "%.800s/newsdb", conf_dir);
        name = buf;
        break;

    case xpImapRootDirectory:
    {
        char prefbuf[1024];
        int len = sizeof prefbuf / sizeof *prefbuf;
        if ((PREF_GetCharPref("mail.imap.root_dir",
                              prefbuf, &len) == PREF_NOERROR)
                && *prefbuf == '/')
            /* guard against assert: line 806, file xp_file.c */
            XP_STRNCPY_SAFE(buf, prefbuf, len);
        /* Copy back to the buffer that was passed in.
         * We couldn't have PREF_GetCharPref() just put it there
         * initially because the size of buf wasn't passed in
         * (it happens to be 1024) and PREF_GetCharPref() insists
         * on having a size. Sigh.
         */
        else
        {
            char *home = getenv ("HOME");
            sprintf(buf, "%s/ns_imap", (home ? home : ""));
        }
        name = buf;
        break;
    }

    case xpImapServerDirectory:
    {
        char prefbuf[1024];
        int len = sizeof prefbuf / sizeof *prefbuf;
        if ((PREF_GetCharPref("mail.imap.root_dir",
                              prefbuf, &len) == PREF_NOERROR)
                && *prefbuf == '/')
            /* guard against assert: line 806, file xp_file.c */
            sprintf(buf, "%s/%s", prefbuf, name);
        else
        {
            char *home = getenv ("HOME");
            sprintf(buf, "%s/ns_imap/%s", (home ? home : ""), name);
        }
        name = buf;
        break;
    }

    case xpFolderCache:
        sprintf (buf, "%s/summary.dat", conf_dir);
        name = buf;
        break;

    case xpCryptoPolicy:
    {
        extern void fe_GetProgramDirectory(char *path, int len);
        char *policyFN = "moz40p3";
        char *mozHome  = getenv("MOZILLA_HOME");
        char *lang     = getenv("LANG");
        int   result;
        char  dirName[1024];

        name = buf;
        if (!xp_unix_sprintf_stat(buf, conf_dir, lang, policyFN))
            break;
        if (!xp_unix_sprintf_stat(buf, mozHome, lang, policyFN))
            break;
        fe_GetProgramDirectory(dirName, sizeof dirName);
        if (!xp_unix_sprintf_stat(buf, dirName, lang, policyFN))
            break;

        /* couldn't find it, but must leave a valid file name in buf */
        sprintf(buf, "%.900s/%s", conf_dir, policyFN);
        break;
    }

    case xpPKCS12File:
        /* Convert /a/b/c/foo to /a/b/c/foo.p12 (note leading dot) */
    {
        int len = 0;

        if(name) {
            XP_ASSERT(name[0] == '/');

            /* only append if there is enough space in the buffer */
            /* this should be changed if the size of the buf changes */
            if((XP_STRLEN(name) + 4) <= 1020) {

                /* include NULL in length */
                len = XP_STRLEN(name) + 1;
                XP_STRNCPY_SAFE(buf, name, len);

                /* we want to concatenate ".p12" if it is not the
                 * last 4 characters of the name already.
                 * If len is less than 5 (including the terminating
                 * NULL), it is not ".p12".
                 * If the len is > 5 it may have the .p12, so we want
                 * to check and only append .p12 if the name
                 * specified does not end in .p12.
                 * only side effect -- this allows for the filename
                 * ".p12" which is fine.
                 */
                if((len >= 5) && XP_STRCASECMP(&(name[len-4-1]), ".p12")) {
                    XP_STRCAT(buf, ".p12");
                } else if(len < 5) {
                    /* can't be ".p12", so we append ".p12" */
                    XP_STRCAT(buf, ".p12");
                }
                name = buf;
            }
        }

        break;
    }

    case xpJSCookieFilters:
    {
        sprintf(buf, "%.900s/cookies.js", conf_dir);
        name = buf;
        break;
    }

    case xpLIPrefs:
    {
        sprintf(buf, "%.900s/liprefs.js", conf_dir);
        name = buf;
        break;
    }

    default:
        abort ();
    }

    return (char *) name;
}
Example #5
0
int XP_MakeDirectory(const char* name, XP_FileType type)
{
    int result;
    static XP_Bool madeXoverDir = FALSE;
    mode_t mode;
    switch (type) {
    case xpMailFolder:
        mode = 0700;
        break;
    default:
        mode = 0755;
        break;
    }
    if (type == xpXoverCache) {
        /* Once per session, we'll check that the xovercache directory is
           created before trying to make any subdirectories of it.  Sigh. ###*/
        if (!madeXoverDir) {
            madeXoverDir = TRUE;
            XP_MakeDirectory("", type);
        }
    }
#ifdef __linux
    {
        /* Linux is a chokes if the parent of the
           named directory is a symbolic link. */
        char rp[MAXPATHLEN];
        char *s, *s0 = WH_FileName (name, type);
        if (!s0) return -1;

        /* realpath is advertised to return a char* (rp) on success, and on
           failure, return 0, set errno, and leave a partial path in rp.
           But on Linux 1.2.3, it doesn't -- it returns 0, leaves the result
           in rp, and doesn't set errno.  So, if errno is unset, assume all
           is well.
         */
        rp[0] = 0;
        errno = 0;
        s = realpath (s0, rp);
        XP_FREE(s0);
        /* WTF??? if (errno) return -1; */
        if (!s) s = rp;
        if (!*s) return -1;
        result = mkdir (s, mode);
    }
#elif defined(SUNOS4)
    {
        char rp[MAXPATHLEN];
        char *s = WH_FileName (name, type);
        char *tmp;
        if (!s) return -1;

        /* Take off all trailing slashes, because some systems (SunOS 4.1.2)
           don't think mkdir("/tmp/x/") means mkdir("/tmp/x"), sigh... */
        PR_snprintf (rp, MAXPATHLEN-1, "%s", s);
        XP_FREE(s);
        while ((tmp = XP_STRRCHR(rp, '/')) && tmp[1] == '\0')
            *tmp = '\0';

        result = mkdir (rp, mode);
    }
#else /* !__linux && !SUNOS4 */
    DO_TRACE(("XP_MakeDirectory called: creating: %s", name));
    {
        char* filename = WH_FileName(name, type);
        if (!filename) return -1;
        result = mkdir(filename, mode);
        XP_FREE(filename);
    }
#endif
    return result;
}
Example #6
0
char *
xp_TempName (XP_FileType type, const char * prefix, char* buf, char* buf2, unsigned int *count)
{
#define NS_BUFFER_SIZE	1024
    char *value = buf;
    time_t now;

    *buf = 0;
    XP_ASSERT (type != xpTemporaryNewsRC);

    if (type == xpCache || type == xpSARCache)
    {
        /* WH_TempName() must return relative pathnames for the cache files,
         so that relative paths get written into the cacheFAT database,
         making the directory relocatable.
         */
        *buf = 0;
    }
    else  if ( (type == xpURL) && prefix )
    {
        if ( XP_STRRCHR(prefix, '/') )
        {
            XP_StatStruct st;

            XP_SPRINTF (buf, "%.500s", prefix);
            if (XP_Stat (buf, &st, xpURL))
                XP_MakeDirectoryR (buf, xpURL);
            prefix = "su";
        }
    }
    else
    {
        char *tmp = FE_TempDir;
        if (!tmp || !*tmp) tmp = "/tmp";
        XP_SPRINTF (buf, "%.500s", tmp);

        if (!prefix || !*prefix)
            prefix = "tmp";
    }

    XP_ASSERT (!XP_STRCHR (prefix, '/'));
    if (*buf && buf[XP_STRLEN (buf)-1] != '/')
        XP_STRCAT (buf, "/");

    /* It's good to have the cache file names be pretty long, with a bunch of
     inputs; this makes the variant part be 15 chars long, consisting of the
     current time (in seconds) followed by a counter (to differentiate
     documents opened in the same second) followed by the current pid (to
     differentiate simultanious processes.)  This organization of the bits
     has the effect that they are ordered the same lexicographically as by
     creation time.

     If name length was an issue we could cut the character count a lot by
     printing them in base 72 [A-Za-z0-9@%-_=+.,~:].
     */
    now = time ((time_t *) 0);
    sprintf (buf2,
             "%08X%03X%04X",
             (unsigned int) now,
             (unsigned int) *count,
             (unsigned int) (getpid () & 0xFFFF));

    if (++(*count) > 4095) (*count) = 0; /* keep it 3 hex digits */

#ifdef CACHE_SUBDIRS
    if (type == xpCache || type == xpSARCache)
    {
        XP_StatStruct st;
        char *s;
        char *tmp = (type == xpCache) ? FE_CacheDir : FE_SARCacheDir;
        if (!tmp || !*tmp) tmp = "/tmp";
        sprintf (buf, "%.500s", tmp);
        if (buf [XP_STRLEN(buf)-1] != '/')
            XP_STRCAT (buf, "/");

        s = buf + XP_STRLEN (buf);

        value = s;		/* return a relative path! */

        /* The name of the subdirectory is the bottom 5 bits of the time part,
         in hex (giving a total of 32 directories.) */
        sprintf (s, "%02X", (now & 0x1F));

        if (XP_Stat (buf, &st, xpURL))		/* create the dir if necessary */
            XP_MakeDirectory (buf, type);

        s[2] = '/';
        s[3] = 0;
    }
#endif /* !CACHE_SUBDIRS */

    XP_STRNCAT (value, prefix, NS_BUFFER_SIZE - XP_STRLEN(value));
    XP_STRNCAT (value, buf2, NS_BUFFER_SIZE - XP_STRLEN(value));

    /* Tao
     */
    if (type == xpAddrBook) {
        XP_STRNCAT (value, ".nab", NS_BUFFER_SIZE - XP_STRLEN(value));
    }/* if */

    value[NS_BUFFER_SIZE - 1] = '\0'; /* just in case */

    DO_TRACE(("WH_TempName called: returning: %s", value));

    return(value);
}
/* NativeComplete
 * copies the file to its final location
 * Tricky, we need to create the directories
 */
int nsInstallFile::NativeComplete()
{
    char* currentName = NULL;
    char* finalName = NULL;
    char* finalNamePlatform;
    int result = 0;

    if (tempFile == NULL) {
        return -1;
    }
    /* Get the names */
    currentName = tempFile->ToNewCString();

    PR_ASSERT(finalFile != NULL);
    finalNamePlatform = finalFile->ToNewCString();
    finalName = XP_PlatformFileToURL(finalNamePlatform);

    if ( finalName == NULL || currentName == NULL ) {
        /* memory or JRI problems */
        result = -1;
        goto end;
    } else {
        /* convert finalName name to xpURL form by stripping "file://" */
        char *temp = XP_STRDUP(&finalName[7]);
        XP_FREE(finalName);
        finalName = temp;
    }

    if (finalName != NULL) {
        if ( XP_STRCMP(finalName, currentName) == 0 ) {
            /* No need to rename, they are the same */
            result = 0;
        } else {
            XP_StatStruct s;
            if ( XP_Stat( finalName, &s, xpURL ) != 0 ) {
                /* Target file doesn't exist, try to rename file */
                result = XP_FileRename(currentName, xpURL, finalName, xpURL);
            } else {
                /* Target exists, can't trust XP_FileRename--do platform
                 * specific stuff in FE_ReplaceExistingFile()
                 */
                result = -1;
            }
        }
    } else {
        /* memory problem */
        result = -1;
    }

    if (result != 0) {
        XP_StatStruct s;
        if ( XP_Stat( finalName, &s, xpURL ) == 0 ) {
            /* File already exists, need to remove the original */
            result = FE_ReplaceExistingFile(currentName, xpURL, finalName, xpURL, force);

            if ( result == SU_REBOOT_NEEDED ) {
#ifdef XP_WIN16
                if (!utilityScheduled) {
                    utilityScheduled = PR_TRUE;
                    FE_ScheduleRenameUtility();
                }
#endif
            }
        } else {
            /* Directory might not exist, check and create if necessary */
            char separator;
            char * end;
            separator = '/';
            end = XP_STRRCHR(finalName, separator);
            if (end) {
                end[0] = 0;
                result = XP_MakeDirectoryR( finalName, xpURL);
                end[0] = separator;
                if ( 0 == result )
                    result = XP_FileRename(currentName, xpURL, finalName, xpURL);
            }
        }
#ifdef XP_UNIX
        /* Last try, can't rename() across file systems on UNIX */
        if ( -1 == result ) {
            result = FE_CopyFile(currentName, finalName);
        }
#endif
    }

end:
    XP_FREEIF(finalName);
    delete currentName;
    delete finalNamePlatform;
    return result;
}