home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / modules / rdf / src / fs2rdf.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  13.1 KB  |  577 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18.  
  19. /* 
  20.    This file implements file system support for the rdf data model.
  21.    For more information on this file, contact rjc or guha 
  22.    For more information on RDF, look at the RDF section of www.mozilla.org
  23. */
  24.  
  25. #include "fs2rdf.h"
  26. #include "glue.h"
  27.  
  28.     /* external string references in allxpstr */
  29. extern    int    RDF_UNABLETODELETEFILE, RDF_UNABLETODELETEFOLDER;
  30.  
  31.  
  32.  
  33. char *
  34. getVolume(int volNum)
  35. {
  36.     char        *buffer = NULL;
  37. #ifdef XP_WIN
  38.     UINT driveType;
  39. #endif
  40.  
  41. #ifdef    XP_MAC
  42.     ParamBlockRec        pb;
  43.     Str32            str;
  44. #endif
  45.  
  46.     if ((buffer = getMem(64L)) == NULL)
  47.     {
  48.         return(NULL);
  49.     }
  50.  
  51. #ifdef    XP_MAC    
  52.     pb.volumeParam.ioCompletion = NULL;
  53.     pb.volumeParam.ioVolIndex = volNum + 1;
  54.     pb.volumeParam.ioNamePtr = (void *)str;
  55.     if (!PBGetVInfo(&pb,FALSE))    {
  56.         str[1+(unsigned)str[0]] = '\0';
  57.         strcpy(buffer, "file:///");
  58.         strcat(buffer, NET_Escape((char *)&str[1], URL_XALPHAS));  /* URL_PATH */
  59.         strcat(buffer,"/");
  60.         return(buffer);
  61.         }
  62. #endif
  63.  
  64. #ifdef    XP_WIN
  65.     /* Windows-specific method for getting drive info without touching the drives (Dave H.) */
  66.     sprintf(buffer, "%c:\\", volNum + 'A');
  67.     driveType = GetDriveType(buffer);
  68.     if (driveType != DRIVE_UNKNOWN && driveType != DRIVE_NO_ROOT_DIR)
  69.     {
  70.         sprintf( buffer, "file:///%c:/", volNum + 'A');
  71.         return buffer;
  72.     }
  73. #endif
  74.  
  75.     if (buffer != NULL)
  76.     {
  77.         freeMem(buffer);
  78.     }
  79.     return(NULL);
  80. }
  81.  
  82.  
  83.  
  84. void
  85. buildVolumeList(RDF_Resource fs)
  86. {
  87.     RDF_Resource    vol;
  88.     char        *volName;
  89.     int        volNum=0;
  90.  
  91.     while (volNum < 26) {
  92.        if ((volName = getVolume(volNum++)) != NULL)  {
  93.     
  94.           if ((vol = RDF_GetResource(NULL, volName, 1)) != NULL)
  95.           {
  96.             setContainerp(vol, 1);
  97.             setResourceType(vol, LFS_RT);
  98.             remoteStoreAdd(gRemoteStore, vol, gCoreVocab->RDF_parent,
  99.                     fs, RDF_RESOURCE_TYPE, 1);
  100.           }
  101.        }
  102.     }
  103. }
  104.  
  105.  
  106.  
  107. PRDir *
  108. OpenDir(char *name)
  109. {
  110.   if (startsWith("file:///", name)) {
  111.     /* return PR_OpenDir(&name[FS_URL_OFFSET]); */
  112.     return CallPROpenDirUsingFileURL(name);
  113.   } else return(NULL);
  114. }
  115.  
  116.  
  117.  
  118. RDFT
  119. MakeLFSStore (char* url)
  120. {
  121.   int        volNum=0;
  122.   RDFT ntr = (RDFT)getMem(sizeof(struct RDF_TranslatorStruct));
  123.   ntr->assert = NULL;
  124.   ntr->unassert = fsUnassert;
  125.   ntr->getSlotValue = fsGetSlotValue;
  126.   ntr->getSlotValues = fsGetSlotValues;
  127.   ntr->hasAssertion = fsHasAssertion;
  128.   ntr->nextValue = fsNextValue;
  129.   ntr->disposeCursor = fsDisposeCursor;
  130.   buildVolumeList(gNavCenter->RDF_LocalFiles);
  131.   ntr->url = copyString(url);
  132.   return ntr;
  133. }
  134.  
  135.  
  136.  
  137. PRBool
  138. fsRemoveFile(char *filePathname, PRBool justCheckWriteAccess)
  139. {
  140.     PRBool        errFlag = true;
  141.  
  142.     if (justCheckWriteAccess == true)
  143.     {
  144.         errFlag = ((!CallPRWriteAccessFileUsingFileURL(filePathname)) ? false:true);
  145.     }
  146.     else
  147.     {
  148.         if (!CallPRWriteAccessFileUsingFileURL(filePathname))
  149.         {
  150.             if (!CallPRDeleteFileUsingFileURL(filePathname))
  151.             {
  152.                 errFlag = false;
  153.             }
  154.         }
  155.         if (errFlag == true)
  156.         {
  157.             FE_Alert(((MWContext *)gRDFMWContext()),
  158.                 PR_smprintf(XP_GetString(RDF_UNABLETODELETEFILE),
  159.                 filePathname));
  160.         }
  161.     }
  162.     return(errFlag);
  163. }
  164.  
  165.  
  166.  
  167. PRBool
  168. fsRemoveDir(char *filePathname, PRBool justCheckWriteAccess)
  169. {
  170.     PRBool        dirFlag, errFlag = false;
  171.     PRDir        *d, *tempD;
  172.     PRDirEntry    *de;
  173.     char        *base, *dirStr, *fileStr;
  174.     int        len, n = PR_SKIP_BOTH;
  175.  
  176.     if (filePathname == NULL)            return(true);
  177.     if ((d = OpenDir(filePathname)) == NULL)    return(true);
  178.  
  179.     while (de = PR_ReadDir(d, n++))
  180.     {
  181.         base = NET_Escape(de->name, URL_XALPHAS);
  182.         if (base == NULL)    return(true);
  183.         fileStr = append2Strings(filePathname, base);
  184.  
  185.         XP_FREE(base);
  186.         if (fileStr == NULL)    return(true);
  187.         dirFlag = (((tempD = OpenDir(fileStr)) != NULL)    ? true:false);
  188.         if (tempD != NULL)
  189.         {
  190.  
  191.             PR_CloseDir(tempD);
  192.         }
  193.  
  194.         if (dirFlag)
  195.         {
  196.             dirStr = append2Strings(fileStr, "/");
  197.             errFlag = fsRemoveDir(dirStr, justCheckWriteAccess);
  198.             if (dirStr != NULL)    freeMem(dirStr);
  199.         }
  200.         else
  201.         {
  202.             errFlag = fsRemoveFile(fileStr, justCheckWriteAccess);
  203.         }
  204.  
  205.         freeMem(fileStr);
  206.         if (errFlag == true)    break;
  207.     }
  208.  
  209.  
  210.     PR_CloseDir(d);
  211.  
  212.     if (errFlag == false)
  213.     {
  214.         if ((dirStr = copyString(filePathname)) != NULL)
  215.         {
  216.             len = strlen(dirStr)-1;
  217.             if (dirStr[len] == '/')
  218.             {
  219.                 dirStr[len] = '\0';
  220.             }
  221.             if (justCheckWriteAccess == true)
  222.             {
  223.                 errFlag = fsRemoveFile(dirStr, justCheckWriteAccess);
  224.             }
  225.             else
  226.             {
  227.                 errFlag = (CallPR_RmDirUsingFileURL(dirStr) != 0);
  228.  
  229.                 if (errFlag == true)
  230.                 {
  231.                     FE_Alert(((MWContext *)gRDFMWContext()),
  232.                         PR_smprintf(XP_GetString(RDF_UNABLETODELETEFOLDER),
  233.                         filePathname));
  234.                 }
  235.             }
  236.             freeMem(dirStr);
  237.         }
  238.     }
  239.  
  240.     return(errFlag);
  241. }
  242.  
  243.  
  244.  
  245. PRBool
  246. fsUnassert (RDFT mcf, RDF_Resource u, RDF_Resource s, void* v, RDF_ValueType type)
  247. {
  248.     char        *filePathname;
  249.     PRBool        retVal = false;
  250.  
  251.     if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE) &&
  252.         (resourceType(u) == LFS_RT) && (containerp(v)) && 
  253.         (resourceType((RDF_Resource)v) == LFS_RT) &&
  254.         (startsWith(resourceID((RDF_Resource)v),  resourceID(u))))
  255.     {
  256.         filePathname =  resourceID((RDF_Resource)u);
  257.         if (filePathname[strlen(filePathname)-1] == '/')
  258.         {
  259.             if (!fsRemoveDir(filePathname, true))
  260.             {
  261.                 if (!fsRemoveDir(filePathname, false))
  262.                 {
  263.                     retVal = true;
  264.                 }
  265.             }
  266.         }
  267.         else
  268.         {
  269.             if (!fsRemoveFile(filePathname, true))
  270.             {
  271.                 if (!fsRemoveFile(filePathname, false))
  272.                 {
  273.                     retVal = true;
  274.                 }
  275.             }
  276.         }
  277.  
  278.         if (retVal == true)
  279.         {
  280.             sendNotifications2(mcf, RDF_DELETE_NOTIFY,
  281.                 u, s, v, type, true);
  282.         }
  283.         
  284.     /* XXX hack: always return true (even if file/dir deletion fails)
  285.              to prevent an assertion going into the localstore,
  286.              which would hide a file/dir that still exists */
  287.  
  288.         retVal = true;
  289.     }
  290.  
  291.     return(retVal);
  292. }
  293.  
  294.  
  295.  
  296. PRBool
  297. fsHasAssertion (RDFT rdf, RDF_Resource u, RDF_Resource s, void* v, 
  298.                RDF_ValueType type, PRBool tv)
  299. {
  300.     PRBool        ans = 0;
  301.     PRDir        *d;
  302.     PRDirEntry    *de;
  303.     char        *filePathname, *name;
  304.     int        len, n=0;
  305.  
  306.     if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE) &&
  307.         (resourceType(u) == LFS_RT) && (containerp(v)) && 
  308.         (resourceType((RDF_Resource)v) == LFS_RT) &&
  309.         (startsWith(resourceID((RDF_Resource)v),  resourceID(u))))
  310.     {
  311.         d = OpenDir(resourceID((RDF_Resource)v));
  312.         if ((filePathname = copyString(resourceID(u))) == NULL)
  313.         {
  314.             return(0);
  315.         }
  316.  
  317.         len = strlen(filePathname)-1;
  318.         if (filePathname[len] == '/')
  319.         {
  320.             filePathname[len] = '\0';
  321.         }
  322.         name = unescapeURL(&filePathname[ 1 + revCharSearch(XP_DIRECTORY_SEPARATOR,
  323.                 filePathname)]);
  324.         if (name == NULL)
  325.         {
  326.             freeMem(filePathname);
  327.             return(0);
  328.         }
  329.  
  330.  
  331.         while (de = PR_ReadDir(d, n++))
  332.         {
  333.             if (strcmp(name, de->name) == 0)
  334.             {
  335.                 ans = 1;
  336.                 break;
  337.             }
  338.         }
  339.  
  340.         PR_CloseDir(d);
  341.         XP_FREE(name);
  342.         freeMem(filePathname);
  343.         return ans;
  344.     }
  345.     else
  346.     {
  347.         return(0);
  348.     }
  349. }
  350.  
  351.  
  352.  
  353. void *
  354. fsGetSlotValue (RDFT rdf, RDF_Resource u, RDF_Resource s, RDF_ValueType type, PRBool inversep,  PRBool tv)
  355. {
  356.   if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE) && (fsUnitp(u)) && tv) {
  357.     if (inversep) {
  358.       char* filePathname =  resourceID(u);
  359.       size_t n = revCharSearch(XP_DIRECTORY_SEPARATOR, filePathname);
  360.       char nname[100];
  361.       PRDir *d;
  362.       PRBool ans = 0;
  363.       memcpy((char*)nname, filePathname, n);
  364.       d = OpenDir(nname);
  365.       ans = (d != NULL);
  366.       PR_CloseDir(d);
  367.       if (ans) {
  368.     RDF_Resource r = RDF_GetResource(NULL, nname, 1);
  369.     setResourceType(r, LFS_RT);
  370.     setContainerp(r, 1);
  371.     return r;
  372.       } else return NULL;
  373.     } else {
  374.       PRDir* d = OpenDir(resourceID(u));
  375.       PRDirEntry* de = PR_ReadDir(d, PR_SKIP_BOTH);
  376.       if (de != NULL) {
  377.     char nname[512];
  378.  
  379.  
  380.     sprintf(nname, "%s%c%s",  resourceID(u), XP_DIRECTORY_SEPARATOR, de->name);
  381.     PR_CloseDir(d);
  382.     return CreateFSUnit(nname, false);
  383.       } else {
  384.     PR_CloseDir(d);
  385.     return NULL;
  386.       }
  387.     }
  388.   } else if ((s == gCoreVocab->RDF_name) && (type == RDF_STRING_TYPE) && (tv) && (fsUnitp(u))) {
  389.     char *pathname, *name = NULL;
  390.     int16 n,len;
  391.  
  392.       if (pathname = copyString(resourceID(u))) {
  393.         len = strlen(pathname);
  394.         if (pathname[len-1] == '/')  pathname[--len] = '\0';
  395.         n = revCharSearch('/', pathname);
  396.         name = unescapeURL(&pathname[n+1]);
  397.         freeMem(pathname);
  398.       }
  399.     return(name);
  400.   } else if ((s == gWebData->RDF_size) && (type == RDF_INT_TYPE) && (tv) && (fsUnitp(u))) {
  401.     PRFileInfo fn;
  402.     PRStatus err;
  403.     char* filePathname =  resourceID(u);
  404.     err=PR_GetFileInfo(&filePathname[FS_URL_OFFSET], &fn);
  405.     if (err != -1) return (void *)fn.size;
  406.     else return NULL;
  407.   } else if ((s == gWebData->RDF_lastModifiedDate) && (type == RDF_INT_TYPE) && (tv) && (fsUnitp(u))) {
  408.  
  409.     PRFileInfo fn;
  410.     PRStatus err;
  411.     char* filePathname =  resourceID(u);
  412.     err = PR_GetFileInfo(&filePathname[FS_URL_OFFSET], &fn);
  413.     if (err != -1)
  414.     {
  415.         PRTime        oneMillion, dateVal;
  416.         int32        modifyTime;
  417.  
  418.     LL_I2L(oneMillion, PR_USEC_PER_SEC);
  419.     LL_DIV(dateVal, fn.modifyTime, oneMillion);
  420.     LL_L2I(modifyTime, dateVal);
  421.     return (void *)modifyTime;
  422.     }
  423.     else
  424.  
  425.     return NULL;
  426.   } else if ((s == gWebData->RDF_creationDate) && (type == RDF_INT_TYPE) && (tv) && (fsUnitp(u))) {
  427.  
  428.     PRFileInfo fn;
  429.     PRStatus err;
  430.     char* filePathname =  resourceID(u);
  431.     err = PR_GetFileInfo(&filePathname[FS_URL_OFFSET], &fn);
  432.     if (err != -1)
  433.     {
  434.         PRTime        oneMillion, dateVal;
  435.         uint32        creationTime;
  436.  
  437.     LL_I2L(oneMillion, PR_USEC_PER_SEC);
  438.     LL_DIV(dateVal, fn.creationTime, oneMillion);
  439.     LL_L2I(creationTime, dateVal);
  440.     return (void *)creationTime;
  441.     }
  442.     else
  443.  
  444.     return NULL;
  445.   }
  446.   return NULL;
  447. }
  448.  
  449.  
  450.  
  451. PRBool
  452. fileDirectoryp(RDF_Resource u)
  453. {
  454.   if (startsWith("file:",  resourceID(u))) {
  455.     PRDir *d = OpenDir(resourceID(u));
  456.     PRBool ans = (d != NULL);
  457.     if (ans) PR_CloseDir(d);
  458.     return ans;
  459.   } else return 0;
  460. }
  461.  
  462.  
  463.  
  464. RDF_Cursor
  465. fsGetSlotValues (RDFT rdf, RDF_Resource u, RDF_Resource s, 
  466.                      RDF_ValueType type,  PRBool inversep, PRBool tv)
  467. {
  468.   if ((s == gCoreVocab->RDF_parent) && (type == RDF_RESOURCE_TYPE) && (fsUnitp(u))
  469.       && (inversep) && (tv)) {
  470.     
  471.     PRDir *d = OpenDir(resourceID(u));
  472.     RDF_Cursor c;
  473.     if (d == NULL) return NULL;
  474.     c = (RDF_Cursor) getMem(sizeof(struct RDF_CursorStruct));
  475.     c->u = u;
  476.     c->count = PR_SKIP_BOTH;
  477.     c->pdata = d;
  478.     c->type = type;
  479.     return c;
  480.   } else return NULL;
  481. }
  482.  
  483.  
  484.  
  485. void *
  486. fsNextValue (RDFT rdf, RDF_Cursor c)
  487. {
  488.     PRFileInfo fn;
  489.     char *encoded = NULL;
  490.  
  491.   if (c == NULL) {
  492.     return NULL;
  493.   } else {
  494.  
  495.     PRDirEntry* de = PR_ReadDir((PRDir*)c->pdata, c->count++);
  496.     if (de == NULL) {
  497.  
  498.       PR_CloseDir((PRDir*)c->pdata);
  499.       c->pdata = NULL;
  500.       return NULL;
  501.     } else {
  502.       char nname[512], *base;
  503.       PRBool isDirectoryFlag = false, sep = ((resourceID(c->u))[strlen(resourceID(c->u))-1] == XP_DIRECTORY_SEPARATOR);
  504.       int len;
  505.  
  506.  
  507.       base = NET_Escape(de->name, URL_XALPHAS);        /* URL_PATH */
  508.       if (base != NULL)    {
  509.         if (sep) {
  510.         sprintf(nname, "%s%s",  resourceID(c->u), base);
  511.         } else
  512.         sprintf(nname, "%s/%s",  resourceID(c->u), base);
  513.         XP_FREE(base);
  514.       }
  515.  
  516.       encoded = unescapeURL(&nname[FS_URL_OFFSET]);
  517.       if (encoded != NULL) {
  518. #ifdef  XP_WIN
  519.             if (encoded[1] == '|') encoded[1] = ':';
  520. #endif
  521.  
  522.         PR_GetFileInfo(encoded, &fn);
  523.         if (fn.type == PR_FILE_DIRECTORY)    {
  524.             isDirectoryFlag = TRUE; 
  525.              len=strlen(nname);
  526.             nname[len] = '/';
  527.             nname[len+1] = '\0';
  528.             }
  529.         freeMem(encoded);
  530.       }
  531.       return CreateFSUnit(nname, isDirectoryFlag);
  532.     } 
  533.   }
  534. }
  535.  
  536.  
  537.  
  538. RDF_Error
  539. fsDisposeCursor (RDFT rdf, RDF_Cursor c)
  540. {
  541.   if (c != NULL) {
  542.     if (c->pdata) PR_CloseDir((PRDir*)c->pdata);
  543.     freeMem(c);
  544.   }
  545.   return 0;
  546. }
  547.  
  548.  
  549.  
  550. RDF_Resource
  551. CreateFSUnit (char* nname, PRBool isDirectoryFlag)
  552. {
  553.   char *name;
  554.   PRBool newName = 0;
  555.   RDF_Resource existing;
  556.   if (startsWith("file:///", nname)) {
  557.     name = nname;
  558.   } else {
  559.     name = (char*) getMem(strlen(nname) + 8);
  560.     memcpy(name,  "file:///", 8);
  561.     memcpy(&name[8], nname, strlen(nname));
  562.     newName = 1;
  563.   }
  564.   existing = RDF_GetResource(NULL, name, 0);
  565.  
  566.   if (existing != NULL) {
  567.     if (newName) freeMem(name);
  568.     return existing;
  569.   } else {
  570.     existing = RDF_GetResource(NULL, name, 1);
  571.     setResourceType(existing, LFS_RT);
  572.     setContainerp(existing, isDirectoryFlag);
  573.     if (newName) freeMem(name);
  574.     return existing;
  575.   }
  576. }
  577.