]> git.ipfire.org Git - thirdparty/sqlite.git/commitdiff
Simple reading and writing now works.
authordrh <>
Fri, 9 Sep 2022 11:41:54 +0000 (11:41 +0000)
committerdrh <>
Fri, 9 Sep 2022 11:41:54 +0000 (11:41 +0000)
FossilOrigin-Name: 00845ac9ef2616e90f5f6a20da3dd040fa8bdcfe73f72fe9c06039561150a82d

ext/misc/vfskv.c
manifest
manifest.uuid

index bde2f0833642042120491fcf66f0e37a182c83db..9897cd818599417a180c4c736fc90133ab1957f5 100644 (file)
@@ -28,11 +28,16 @@ SQLITE_EXTENSION_INIT1
 /*****************************************************************************
 ** Debugging logic
 */
-#if 1
+#if 0
 #define KVVFS_TRACE(X)  printf X;
 #else
 #define KVVFS_TRACE(X)
 #endif
+#if 0
+#define KVVFS_LOG(X)  printf X;
+#else
+#define KVVFS_LOG(X)
+#endif
 
 
 /*****************************************************************************
@@ -258,11 +263,12 @@ static int kvstorageRead(
     KVVFS_TRACE(("KVVFS-READ   %-14s (-1)\n", pStore->zKey));
     return -1;
   }else{
-    fread(zBuf, nBuf-1, 1, fd);
+    sqlite3_int64 n = fread(zBuf, 1, nBuf-1, fd);
     fclose(fd);
-    KVVFS_TRACE(("KVVFS-READ   %-14s (%d) %.30s\n", pStore->zKey,
-                 nBuf-1, zBuf));
-    return nBuf-1;
+    zBuf[n] = 0;
+    KVVFS_TRACE(("KVVFS-READ   %-14s (%lld) %.30s\n", pStore->zKey,
+                 n, zBuf));
+    return (int)n;
   }
 }
 
@@ -276,6 +282,7 @@ static int kvvfsClose(sqlite3_file *pProtoFile){
   KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
   KVVfsVfs *pVfs = pFile->pVfs;
 
+  KVVFS_LOG(("xClose %s\n", pFile->isJournal ? "journal" : "db"));
   if( pVfs->pFiles==pFile ){
     pVfs->pFiles = pFile->pNext;
     if( pVfs->pFiles==0 ){
@@ -294,7 +301,6 @@ static int kvvfsClose(sqlite3_file *pProtoFile){
     }
   }
   sqlite3_free(pFile->aJrnl);
-  sqlite3_free(pFile);
   return SQLITE_OK;
 }
 
@@ -321,8 +327,8 @@ static int kvvfsEncode(const char *aData, int nData, char *aOut){
       ** and so forth.
       */
       int k;
-      for(k=1; a[i+k]==0 && i+k<nData; k++){}
-      i += k;
+      for(k=1; i+k<nData && a[i+k]==0; k++){}
+      i += k-1;
       while( k>0 ){
         aOut[j++] = 'a'+(k%26);
         k /= 26;
@@ -336,7 +342,7 @@ static int kvvfsEncode(const char *aData, int nData, char *aOut){
 /* Convert hex to binary */
 static char kvvfsHexToBinary(char c){
   if( c>='0' && c<='9' ) return c - '0';
-  if( c>='a' && c<='f' ) return c - 'a' + 10;
+  if( c>='A' && c<='F' ) return c - 'A' + 10;
   return 0;
 }
 
@@ -366,10 +372,8 @@ static int kvvfsDecode(const char *aIn, char *aOut, int nOut){
       }
     }else{
       if( j>nOut ) return -1;
-      aOut[j] = kvvfsHexToBinary(aIn[i])<<4;
-      i++;
-      aOut[j] += kvvfsHexToBinary(aIn[i]);
-      i++;
+      aOut[j] = kvvfsHexToBinary(aIn[i++])<<4;
+      aOut[j++] += kvvfsHexToBinary(aIn[i++]);
     }
   }
   return j;
@@ -423,6 +427,7 @@ static int kvvfsReadFromJournal(
   sqlite_int64 iOfst
 ){
   assert( pFile->isJournal );
+  KVVFS_LOG(("xRead('journal',%d,%lld)\n", iAmt, iOfst));
   if( pFile->aJrnl==0 ){
     int szTxt = kvstorageRead(pFile->pVfs->pStore, "journal", 0, 0);
     char *aTxt;
@@ -458,23 +463,36 @@ static int kvvfsReadFromDb(
   char aData[131073];
   assert( iOfst>=0 );
   assert( iAmt>=0 );
-  if( (iOfst % iAmt)!=0 ){
-    return SQLITE_IOERR_READ;
-  }
-  if( iAmt!=100 || iOfst!=0 ){
+  KVVFS_LOG(("xRead('db',%d,%lld)\n", iAmt, iOfst));
+  if( iOfst+iAmt>=512 ){
+    if( (iOfst % iAmt)!=0 ){
+      return SQLITE_IOERR_READ;
+    }
     if( (iAmt & (iAmt-1))!=0 || iAmt<512 || iAmt>65536 ){
       return SQLITE_IOERR_READ;
     }
     pFile->szPage = iAmt;
+    pgno = 1 + iOfst/iAmt;
+  }else{
+    pgno = 1;
   }
-  pgno = 1 + iOfst/iAmt;
   sqlite3_snprintf(sizeof(zKey), zKey, "pg-%u", pgno);
   got = kvstorageRead(pFile->pVfs->pStore, zKey, aData, sizeof(aData)-1);
   if( got<0 ){
     n = 0;
   }else{
     aData[got] = 0;
-    n = kvvfsDecode(aData, zBuf, iAmt);
+    if( iOfst+iAmt<512 ){
+      n = kvvfsDecode(aData, &aData[1000], 1000);
+      if( n>=iOfst+iAmt ){
+        memcpy(zBuf, &aData[1000+iOfst], iAmt);
+        n = iAmt;
+      }else{
+        n = 0;
+      }
+    }else{
+      n = kvvfsDecode(aData, zBuf, iAmt);
+    }
   }
   if( n<iAmt ){
     memset(zBuf+n, 0, iAmt-n);
@@ -500,6 +518,7 @@ static int kvvfsRead(
   }else{
     rc = kvvfsReadFromDb(pFile,zBuf,iAmt,iOfst);
   }
+  KVVFS_LOG(("xRead result: 0x%x\n", rc));
   return rc;
 }
 
@@ -528,6 +547,7 @@ static int kvvfsWriteToJournal(
   sqlite_int64 iOfst
 ){
   sqlite3_int64 iEnd = iOfst+iAmt;
+  KVVFS_LOG(("xWrite('journal',%d,%lld)\n", iAmt, iOfst));
   if( iEnd>=0x10000000 ) return SQLITE_FULL;
   if( pFile->aJrnl==0 || pFile->nJrnl<iEnd ){
     char *aNew = sqlite3_realloc(pFile->aJrnl, iEnd);
@@ -556,6 +576,7 @@ static int kvvfsWriteToDb(
   unsigned int pgno;
   char zKey[30];
   char aData[131073];
+  KVVFS_LOG(("xWrite('db',%d,%lld)\n", iAmt, iOfst));
   assert( iAmt>=512 && iAmt<=65536 );
   assert( (iAmt & (iAmt-1))==0 );
   pgno = 1 + iOfst/iAmt;
@@ -585,6 +606,7 @@ static int kvvfsWrite(
   }else{
     rc = kvvfsWriteToDb(pFile,zBuf,iAmt,iOfst);
   }
+  KVVFS_LOG(("xWrite result: 0x%x\n", rc));
   return rc;
 }
 
@@ -594,6 +616,7 @@ static int kvvfsWrite(
 static int kvvfsTruncate(sqlite3_file *pProtoFile, sqlite_int64 size){
   KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
   if( pFile->isJournal ){
+    KVVFS_LOG(("xTruncate('journal',%lld)\n", size));
     assert( size==0 );
     kvstorageDelete(pFile->pVfs->pStore, "journal");
     sqlite3_free(pFile->aJrnl);
@@ -607,6 +630,7 @@ static int kvvfsTruncate(sqlite3_file *pProtoFile, sqlite_int64 size){
   ){
     char zKey[50];
     unsigned int pgno, pgnoMax;
+    KVVFS_LOG(("xTruncate('db',%lld)\n", size));
     pgno = 1 + size/pFile->szPage;
     pgnoMax = 2 + pFile->szDb/pFile->szPage;
     while( pgno<=pgnoMax ){
@@ -629,11 +653,13 @@ static int kvvfsSync(sqlite3_file *pProtoFile, int flags){
   KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
   char *zOut;
   if( !pFile->isJournal ){
+    KVVFS_LOG(("xSync('db')\n"));
     if( pFile->szDb>0 ){
       kvvfsWriteFileSize(pFile, pFile->szDb);
     }
     return SQLITE_OK;
   }
+  KVVFS_LOG(("xSync('journal')\n"));
   if( pFile->nJrnl<=0 ){
      return kvvfsTruncate(pProtoFile, 0);
   }
@@ -659,10 +685,17 @@ static int kvvfsSync(sqlite3_file *pProtoFile, int flags){
 static int kvvfsFileSize(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
   KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
   if( pFile->isJournal ){
+    KVVFS_LOG(("xFileSize('journal')\n"));
     *pSize = pFile->nJrnl;
   }else{
-    *pSize = pFile->szDb;
+    KVVFS_LOG(("xFileSize('db')\n"));
+    if( pFile->szDb>=0 ){
+      *pSize = pFile->szDb;
+    }else{
+      *pSize = kvvfsReadFileSize(pFile);
+    }
   }
+  KVVFS_LOG(("xFileSize: %lld\n", *pSize));
   return SQLITE_OK;
 }
 
@@ -672,6 +705,8 @@ static int kvvfsFileSize(sqlite3_file *pProtoFile, sqlite_int64 *pSize){
 static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
   KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
   assert( !pFile->isJournal );
+  KVVFS_LOG(("xLock(%d)\n", eLock));
+
   if( eLock!=SQLITE_LOCK_NONE ){
     pFile->szDb = kvvfsReadFileSize(pFile);
   }
@@ -684,6 +719,7 @@ static int kvvfsLock(sqlite3_file *pProtoFile, int eLock){
 static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
   KVVfsFile *pFile = (KVVfsFile *)pProtoFile;
   assert( !pFile->isJournal );
+  KVVFS_LOG(("xUnlock(%d)\n", eLock));
   if( eLock==SQLITE_LOCK_NONE ){
     pFile->szDb = -1;
   }
@@ -694,6 +730,7 @@ static int kvvfsUnlock(sqlite3_file *pProtoFile, int eLock){
 ** Check if another file-handle holds a RESERVED lock on an kvvfs-file.
 */
 static int kvvfsCheckReservedLock(sqlite3_file *pProtoFile, int *pResOut){
+  KVVFS_LOG(("xCheckReservedLock\n"));
   *pResOut = 0;
   return SQLITE_OK;
 }
@@ -732,6 +769,7 @@ static int kvvfsOpen(
 ){
   KVVfsFile *pFile = (KVVfsFile*)pProtoFile;
   KVVfsVfs *pVfs = (KVVfsVfs*)pProtoVfs;
+  KVVFS_LOG(("xOpen(\"%s\")\n", zName));
   pFile->aJrnl = 0;
   pFile->nJrnl = 0;
   pFile->isJournal = sqlite3_strglob("*-journal", zName)==0;
@@ -771,11 +809,15 @@ static int kvvfsAccess(
   int *pResOut
 ){
   KVVfsVfs *pVfs = (KVVfsVfs*)pProtoVfs;
+  KVVFS_LOG(("xAccess(\"%s\")\n", zPath));
   if( sqlite3_strglob("*-journal", zPath)==0 ){
     *pResOut = kvstorageRead(pVfs->pStore, "journal", 0, 0)>0;
+  }else if( sqlite3_strglob("*-wal", zPath)==0 ){
+    *pResOut = 0;
   }else{
     *pResOut = 1;
   }
+  KVVFS_LOG(("xAccess returns %d\n",*pResOut));
   return SQLITE_OK;
 }
 
@@ -791,6 +833,7 @@ static int kvvfsFullPathname(
   char *zOut
 ){
   size_t nPath = strlen(zPath);
+  KVVFS_LOG(("xFullPathname(\"%s\")\n", zPath));
   if( nOut<nPath+1 ) nPath = nOut - 1;
   memcpy(zOut, zPath, nPath);
   zOut[nPath] = 0;
index 95ed97d626e44b60df818705d3eb52d8752c99aa..0db47233212872ba63896db56e03dbaa0c787a57 100644 (file)
--- a/manifest
+++ b/manifest
@@ -1,5 +1,5 @@
-C Compiles\sand\sloads\sas\san\sextension.\s\sStarts\sto\srun\sbut\squickly\shits\sissues.
-D 2022-09-08T17:12:12.069
+C Simple\sreading\sand\swriting\snow\sworks.
+D 2022-09-09T11:41:54.677
 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1
 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea
 F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724
@@ -335,7 +335,7 @@ F ext/misc/uint.c 053fed3bce2e89583afcd4bf804d75d659879bbcedac74d0fa9ed548839a03
 F ext/misc/unionvtab.c 36237f0607ca954ac13a4a0e2d2ac40c33bc6e032a5f55f431713061ef1625f9
 F ext/misc/urifuncs.c f71360d14fa9e7626b563f1f781c6148109462741c5235ac63ae0f8917b9c751
 F ext/misc/uuid.c 5bb2264c1b64d163efa46509544fd7500cb8769cb7c16dd52052da8d961505cf
-F ext/misc/vfskv.c 8664e43778e5d7cda6abd2e57ec374e7e0eb424ff4da093847bac7aa154c7789
+F ext/misc/vfskv.c 8c0d3af0b68fdee50ff46b64bec28d693805bd5fc5e26605e198915e9392d6e3
 F ext/misc/vfslog.c 3932ab932eeb2601dbc4447cb14d445aaa9fbe43b863ef5f014401c3420afd20
 F ext/misc/vfsstat.c 474d08efc697b8eba300082cb1eb74a5f0f3df31ed257db1cb07e72ab0e53dfb
 F ext/misc/vtablog.c 5538acd0c8ddaae372331bee11608d76973436b77d6a91e8635cfc9432fba5ae
@@ -2001,8 +2001,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93
 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc
 F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e
 F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0
-P a329939c32e33d8d00066ba3d4327f07cca1b4e67de0b8d85f1e7f98afe40954
-R 3c0347d709594b19afa07b9bd97b3623
+P 2e38726f46918b28b5c638967f960a20afd3fe84ad245f3bea39a1d64980435b
+R d99171917ec9b13f78c647c76b29147e
 U drh
-Z c98d8a23118cf655e6b32d119c295b3c
+Z d5b6013a0fd62dab0ae32db15d37fb8f
 # Remove this line to create a well-formed Fossil manifest.
index 0963ec3e9b8c1af683163a19f534d6f92a2edaea..43d0014bd4869c89f160b06695013f8700780146 100644 (file)
@@ -1 +1 @@
-2e38726f46918b28b5c638967f960a20afd3fe84ad245f3bea39a1d64980435b
\ No newline at end of file
+00845ac9ef2616e90f5f6a20da3dd040fa8bdcfe73f72fe9c06039561150a82d
\ No newline at end of file