/ Check-in [8bfbdec6]
Login
SQLite training in Houston TX on 2019-11-05 (details)
Part of the 2019 Tcl Conference

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Simplifications to the pager_delmaster() implementation.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 8bfbdec647d29f0eb103ba7deb9116c44193f2e1
User & Date: drh 2010-06-23 15:04:27
Context
2010-06-23
15:18
Fix the xCurrentTimeInt64 interface on windows. It was off by a factor of 10. check-in: 51ef43b9 user: drh tags: trunk
15:04
Simplifications to the pager_delmaster() implementation. check-in: 8bfbdec6 user: drh tags: trunk
2010-06-22
21:15
Disable code used only by the codec when the codec is not deployed. check-in: 2c90276e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Ignore Whitespace Patch

Changes to src/pager.c.

  1792   1792   static int pager_delmaster(Pager *pPager, const char *zMaster){
  1793   1793     sqlite3_vfs *pVfs = pPager->pVfs;
  1794   1794     int rc;                   /* Return code */
  1795   1795     sqlite3_file *pMaster;    /* Malloc'd master-journal file descriptor */
  1796   1796     sqlite3_file *pJournal;   /* Malloc'd child-journal file descriptor */
  1797   1797     char *zMasterJournal = 0; /* Contents of master journal file */
  1798   1798     i64 nMasterJournal;       /* Size of master journal file */
         1799  +  char *zJournal;           /* Pointer to one journal within MJ file */
         1800  +  char *zMasterPtr;         /* Space to hold MJ filename from a journal file */
         1801  +  int nMasterPtr;           /* Amount of space allocated to zMasterPtr[] */
  1799   1802   
  1800   1803     /* Allocate space for both the pJournal and pMaster file descriptors.
  1801   1804     ** If successful, open the master journal file for reading.
  1802   1805     */
  1803   1806     pMaster = (sqlite3_file *)sqlite3MallocZero(pVfs->szOsFile * 2);
  1804   1807     pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
  1805   1808     if( !pMaster ){
................................................................................
  1806   1809       rc = SQLITE_NOMEM;
  1807   1810     }else{
  1808   1811       const int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
  1809   1812       rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
  1810   1813     }
  1811   1814     if( rc!=SQLITE_OK ) goto delmaster_out;
  1812   1815   
         1816  +  /* Load the entire master journal file into space obtained from
         1817  +  ** sqlite3_malloc() and pointed to by zMasterJournal.   Also obtain
         1818  +  ** sufficient space (in zMasterPtr) to hold the names of master
         1819  +  ** journal files extracted from regular rollback-journals.
         1820  +  */
  1813   1821     rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
  1814   1822     if( rc!=SQLITE_OK ) goto delmaster_out;
         1823  +  nMasterPtr = pVfs->mxPathname+1;
         1824  +  zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
         1825  +  if( !zMasterJournal ){
         1826  +    rc = SQLITE_NOMEM;
         1827  +    goto delmaster_out;
         1828  +  }
         1829  +  zMasterPtr = &zMasterJournal[nMasterJournal+1];
         1830  +  rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
         1831  +  if( rc!=SQLITE_OK ) goto delmaster_out;
         1832  +  zMasterJournal[nMasterJournal] = 0;
  1815   1833   
  1816         -  if( nMasterJournal>0 ){
  1817         -    char *zJournal;
  1818         -    char *zMasterPtr = 0;
  1819         -    int nMasterPtr = pVfs->mxPathname+1;
  1820         -
  1821         -    /* Load the entire master journal file into space obtained from
  1822         -    ** sqlite3_malloc() and pointed to by zMasterJournal. 
  1823         -    */
  1824         -    zMasterJournal = sqlite3Malloc((int)nMasterJournal + nMasterPtr + 1);
  1825         -    if( !zMasterJournal ){
  1826         -      rc = SQLITE_NOMEM;
         1834  +  zJournal = zMasterJournal;
         1835  +  while( (zJournal-zMasterJournal)<nMasterJournal ){
         1836  +    int exists;
         1837  +    rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
         1838  +    if( rc!=SQLITE_OK ){
  1827   1839         goto delmaster_out;
  1828   1840       }
  1829         -    zMasterPtr = &zMasterJournal[nMasterJournal+1];
  1830         -    rc = sqlite3OsRead(pMaster, zMasterJournal, (int)nMasterJournal, 0);
  1831         -    if( rc!=SQLITE_OK ) goto delmaster_out;
  1832         -    zMasterJournal[nMasterJournal] = 0;
         1841  +    if( exists ){
         1842  +      /* One of the journals pointed to by the master journal exists.
         1843  +      ** Open it and check if it points at the master journal. If
         1844  +      ** so, return without deleting the master journal file.
         1845  +      */
         1846  +      int c;
         1847  +      int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
         1848  +      rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
         1849  +      if( rc!=SQLITE_OK ){
         1850  +        goto delmaster_out;
         1851  +      }
  1833   1852   
  1834         -    zJournal = zMasterJournal;
  1835         -    while( (zJournal-zMasterJournal)<nMasterJournal ){
  1836         -      int exists;
  1837         -      rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS, &exists);
         1853  +      rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
         1854  +      sqlite3OsClose(pJournal);
  1838   1855         if( rc!=SQLITE_OK ){
  1839   1856           goto delmaster_out;
  1840   1857         }
  1841         -      if( exists ){
  1842         -        /* One of the journals pointed to by the master journal exists.
  1843         -        ** Open it and check if it points at the master journal. If
  1844         -        ** so, return without deleting the master journal file.
  1845         -        */
  1846         -        int c;
  1847         -        int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
  1848         -        rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
  1849         -        if( rc!=SQLITE_OK ){
  1850         -          goto delmaster_out;
  1851         -        }
  1852   1858   
  1853         -        rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
  1854         -        sqlite3OsClose(pJournal);
  1855         -        if( rc!=SQLITE_OK ){
  1856         -          goto delmaster_out;
  1857         -        }
  1858         -
  1859         -        c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
  1860         -        if( c ){
  1861         -          /* We have a match. Do not delete the master journal file. */
  1862         -          goto delmaster_out;
  1863         -        }
         1859  +      c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
         1860  +      if( c ){
         1861  +        /* We have a match. Do not delete the master journal file. */
         1862  +        goto delmaster_out;
  1864   1863         }
  1865         -      zJournal += (sqlite3Strlen30(zJournal)+1);
  1866   1864       }
         1865  +    zJournal += (sqlite3Strlen30(zJournal)+1);
  1867   1866     }
  1868   1867    
  1869   1868     sqlite3OsClose(pMaster);
  1870   1869     rc = sqlite3OsDelete(pVfs, zMaster, 0);
  1871   1870   
  1872   1871   delmaster_out:
  1873         -  if( zMasterJournal ){
  1874         -    sqlite3_free(zMasterJournal);
  1875         -  }  
         1872  +  sqlite3_free(zMasterJournal);
  1876   1873     if( pMaster ){
  1877   1874       sqlite3OsClose(pMaster);
  1878   1875       assert( !isOpen(pJournal) );
  1879   1876       sqlite3_free(pMaster);
  1880   1877     }
  1881   1878     return rc;
  1882   1879   }