]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Enhance the realpath() SQL function in the fileio.c extension
authordrh <>
Mon, 23 Feb 2026 11:44:30 +0000 (11:44 +0000)
committerdrh <>
Mon, 23 Feb 2026 11:44:30 +0000 (11:44 +0000)
so that it works ever for pathnames that do not exist.

FossilOrigin-Name: 27a5735fb1e194d763ab9fdb933fad4f694fb2f8ad19205d17ac81caebd82548

ext/misc/fileio.c
manifest
manifest.uuid

index e103d42332ae0cb8cd0b4c02259f0e0eab7377ca..51b748291a8247890c26e88b65b0946947040b36 100644 (file)
@@ -1114,16 +1114,20 @@ static char *portable_realpath(const char *zPath){
 ** Try to convert file or pathname X into its real, absolute pathname.
 ** Return NULL if unable.
 **
-** The file X is not required to exist.  However, if any directory
-** in the path to X does not exist, then the function returns NULL.
+** The file or directory X is not required to exist.  The answer is formed
+** by calling system realpath() on the prefix of X that does exist and
+** appending the tail of X that does not (yet) exist.
 */
 static void realpathFunc(
   sqlite3_context *context,
   int argc,
   sqlite3_value **argv
 ){
-  const char *zPath;
-  char *zOut;
+  const char *zPath;    /* Original input path */
+  char *zCopy;          /* An editable copy of zPath */
+  char *zOut;           /* The result */
+  char cSep = 0;        /* Separator turned into \000 */
+  size_t len;           /* Prefix length before cSep */
 #ifdef _WIN32
   const int isWin = 1;
 #else
@@ -1134,30 +1138,67 @@ static void realpathFunc(
   zPath = (const char*)sqlite3_value_text(argv[0]);
   if( zPath==0 ) return;
   if( zPath[0]==0 ) zPath = ".";
-  zOut = portable_realpath(zPath);
-  if( zOut==0
-   && (strchr(zPath,'/') || (isWin && strchr(zPath,'\\')))
-  ){
-    char *zCopy = sqlite3_mprintf("%s", zPath);
-    size_t i;
-    char cSep = 0;
-    if( zCopy==0 ) return;
-    for(i = strlen(zCopy) - 1; i>0; i--){
-      if( zCopy[i]=='/' || (isWin && zCopy[i]=='\\') ){
-        cSep = zCopy[i];
-        zCopy[i] = 0;
-        break;
+  zCopy = sqlite3_mprintf("%s",zPath);
+  len = strlen(zCopy);
+  while( len>1 && (zCopy[len-1]=='/' || (isWin && zCopy[len-1]=='\\')) ){
+    len--;
+  }
+  zCopy[len] = 0;
+  while( 1 /*exit-by-break*/ ){
+    zOut = portable_realpath(zCopy);
+    zCopy[len] = cSep;
+    if( zOut ){
+      if( cSep ){
+        zOut = sqlite3_mprintf("%z%s",zOut,&zCopy[len]);
       }
-    }
-    if( cSep ){
-      zOut = portable_realpath(zCopy);
-      if( zOut ){
-        zOut = sqlite3_mprintf("%z%c%s",zOut,cSep,&zCopy[i+1]);
+      break;
+    }else{
+      size_t i = len-1;
+      while( i>0 ){
+        if( zCopy[i]=='/' || (isWin && zCopy[i]=='\\') ) break;
+        i--;
+      }
+      if( i<=0 ){
+        if( zCopy[0]=='/' ){
+          zOut = zCopy;
+          zCopy = 0;
+        }else if( (zOut = portable_realpath("."))!=0 ){
+          zOut = sqlite3_mprintf("%z/%s", zOut, zCopy);
+        }          
+        break;
       }
+      cSep = zCopy[i];
+      zCopy[i] = 0;
+      len = i;
     }
-    sqlite3_free(zCopy);
   }
+  sqlite3_free(zCopy);
   if( zOut ){
+    /* Simplify any "/./" or "/../" that might have snuck into the
+    ** pathname due to appending of zCopy.  We only have to consider
+    ** unix "/" separators, because the _wfilepath() system call on
+    ** Windows will have already done this simplification for us. */
+    size_t i, j, n;
+    n = strlen(zOut);
+    for(i=j=0; i<n; i++){
+      if( zOut[i]=='/' ){
+        if( zOut[i+1]=='/' ) continue;
+        if( zOut[i+1]=='.' && i+2<n && zOut[i+2]=='/' ){
+          i += 1;
+          continue;
+        }
+        if( zOut[i+1]=='.' && i+3<n && zOut[i+2]=='.' && zOut[i+3]=='/' ){
+          while( j>0 && zOut[j-1]!='/' ){ j--; }
+          if( j>0 ){ j--; }
+          i += 2;
+          continue;
+        }
+      }
+      zOut[j++] = zOut[i];
+    }
+    zOut[j] = 0;
+
+    /* Return the result */
     sqlite3_result_text(context, zOut, -1, sqlite3_free);
   }
 }
index 0ad630f8bd8c5a907b2ea90fc4f4c000faf0f59e..0d6b98c84158c541297147df5d88c5d33e443525 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C When\sdoing\san\sSQLAR\sarchive\sextraction\sin\sthe\sCLI,\spostpone\screating\ssymlinks\suntil\safter\nall\sfiles\sand\sdirectories\shave\sbeen\screated.\s\sThis\sprevents\sa\shostile\sarchive\sfrom\ncreating\sa\ssymlink\sthrough\swhich\sit\scan\ssubsequently\swrite\scontent\soutside\sof\sthe\starget\ndirectory.\s\s[forum:forumpost/9e176adfef91c207|Forum\spost\s9e176adfef91c207].
-D 2026-02-23T01:34:14.113
+C Enhance\sthe\srealpath()\sSQL\sfunction\sin\sthe\sfileio.c\sextension\nso\sthat\sit\sworks\sever\sfor\spathnames\sthat\sdo\snot\sexist.
+D 2026-02-23T11:44:30.800
 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
@@ -373,7 +373,7 @@ F ext/misc/dbdump.c 678f1b9ae2317b4473f65d03132a2482c3f4b08920799ed80feedd2941a0
 F ext/misc/decimal.c e1da22eee70d7e3eaa99a6b761bc03c4d01d7ffa554bf3178b1f1f184932806c
 F ext/misc/eval.c 04bc9aada78c888394204b4ed996ab834b99726fb59603b0ee3ed6e049755dc1
 F ext/misc/explain.c 606100185fb90d6a1eade1ed0414d53503c86820d8956a06e3b0a56291894f2b
-F ext/misc/fileio.c 43f44347a4b70d22a7c1a68f799dc4ec4c58e39d84f04cebc6f669c6eadcb09b
+F ext/misc/fileio.c 33165b3cd99f83dcd333a338eb51491f6b01c8d96cb6ae81f96a6a096834e030
 F ext/misc/fossildelta.c 86dfa83f85f7ccd640591d8a5c6865346d0c2ee6a949d78591eceb892f1cbfec
 F ext/misc/fuzzer.c 6b231352815304ba60d8e9ec2ee73d4918e74d9b76bda8940ba2b64e8777515e
 F ext/misc/ieee754.c 2901d08a586d00a1d3c0fd89e03c57ee9e2b5f013b0daab9e49c7a48a9d5946b
@@ -2195,8 +2195,8 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee
 F tool/warnings.sh d924598cf2f55a4ecbc2aeb055c10bd5f48114793e7ba25f9585435da29e7e98
 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f
 F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c
-P 4df4999484d9008d8af3c9c340810e0cf5f57161ba053ed5501276b450577039
-R 536f614f7f299837eaceb0448191c5cb
+P 9719034d4d3becda127dc294f7f58ded9c982509c690dd55b56310912957eb51
+R bf2d4ba90ab9cecc4df495425f391fd6
 U drh
-Z 1a29d5706c125cd77c3ea8fb58018127
+Z 7b2a1df61967b73bec8a68ebbc48bbf5
 # Remove this line to create a well-formed Fossil manifest.
index 75b99ad74c88795c01484fb0aff0b1643f4b45d9..b84b3aaf79f5e3978f08eff56f6893df36b65d5f 100644 (file)
@@ -1 +1 @@
-9719034d4d3becda127dc294f7f58ded9c982509c690dd55b56310912957eb51
+27a5735fb1e194d763ab9fdb933fad4f694fb2f8ad19205d17ac81caebd82548