/ Check-in [b743429d]
Login

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

Overview
Comment:VACUUM now uses a temporary file in the official TEMP folder instead of a file in the same directory as the original database. (CVS 3470)
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: b743429dd54e2dcae213ec1993e9e916a9ba678d
User & Date: drh 2006-10-10 13:07:36
Context
2006-10-10
17:37
Copy fts1/ to fts2/, changing reference from fts1 to fts2. For future reference, the source versions copied were:

README.txt r1.1 fts1.c r1.37 fts1.h r1.2 fts1_hash.c r1.1 fts1_hash.h r1.1 fts1_porter.c r1.1 fts1_tokenizer.h r1.4 fts1_tokenizer1.c r1.6 (CVS 3471) check-in: d0d1e7cd user: shess tags: trunk

13:07
VACUUM now uses a temporary file in the official TEMP folder instead of a file in the same directory as the original database. (CVS 3470) check-in: b743429d user: drh tags: trunk
2006-10-09
00:38
Version 3.3.8 (CVS 3469) check-in: 0658bb9e user: drh tags: trunk
Changes
Hide Diffs Side-by-Side Diffs Show Whitespace Changes Patch

Changes to src/vacuum.c.

    10     10   **
    11     11   *************************************************************************
    12     12   ** This file contains code used to implement the VACUUM command.
    13     13   **
    14     14   ** Most of the code in this file may be omitted by defining the
    15     15   ** SQLITE_OMIT_VACUUM macro.
    16     16   **
    17         -** $Id: vacuum.c,v 1.63 2006/09/21 11:02:18 drh Exp $
           17  +** $Id: vacuum.c,v 1.64 2006/10/10 13:07:36 drh Exp $
    18     18   */
    19     19   #include "sqliteInt.h"
    20     20   #include "vdbeInt.h"
    21     21   #include "os.h"
    22     22   
    23     23   #ifndef SQLITE_OMIT_VACUUM
    24         -/*
    25         -** Generate a random name of 20 character in length.
    26         -*/
    27         -static void randomName(unsigned char *zBuf){
    28         -  static const unsigned char zChars[] =
    29         -    "abcdefghijklmnopqrstuvwxyz"
    30         -    "0123456789";
    31         -  int i;
    32         -  sqlite3Randomness(20, zBuf);
    33         -  for(i=0; i<20; i++){
    34         -    zBuf[i] = zChars[ zBuf[i]%(sizeof(zChars)-1) ];
    35         -  }
    36         -}
    37         -
    38     24   /*
    39     25   ** Execute zSql on database db. Return an error code.
    40     26   */
    41     27   static int execSql(sqlite3 *db, const char *zSql){
    42     28     sqlite3_stmt *pStmt;
    43     29     if( SQLITE_OK!=sqlite3_prepare(db, zSql, -1, &pStmt, 0) ){
    44     30       return sqlite3_errcode(db);
................................................................................
    88     74   }
    89     75   
    90     76   /*
    91     77   ** This routine implements the OP_Vacuum opcode of the VDBE.
    92     78   */
    93     79   int sqlite3RunVacuum(char **pzErrMsg, sqlite3 *db){
    94     80     int rc = SQLITE_OK;     /* Return code from service routines */
    95         -  const char *zFilename;  /* full pathname of the database file */
    96         -  int nFilename;          /* number of characters  in zFilename[] */
    97         -  char *zTemp = 0;        /* a temporary file in same directory as zFilename */
    98     81     Btree *pMain;           /* The database being vacuumed */
    99         -  Btree *pTemp;
   100         -  char *zSql = 0;
           82  +  Btree *pTemp;           /* The temporary database we vacuum into */
           83  +  char *zSql = 0;         /* SQL statements */
   101     84     int saved_flags;       /* Saved value of the db->flags */
   102     85     Db *pDb = 0;           /* Database to detach at end of vacuum */
           86  +  char zTemp[SQLITE_TEMPNAME_SIZE+1];  /* Name of the TEMP file */
   103     87   
   104     88     /* Save the current value of the write-schema flag before setting it. */
   105     89     saved_flags = db->flags;
   106     90     db->flags |= SQLITE_WriteSchema | SQLITE_IgnoreChecks;
   107     91   
   108     92     if( !db->autoCommit ){
   109     93       sqlite3SetString(pzErrMsg, "cannot VACUUM from within a transaction", 
   110     94          (char*)0);
   111     95       rc = SQLITE_ERROR;
   112     96       goto end_of_vacuum;
   113     97     }
   114         -
   115         -  /* Get the full pathname of the database file and create a
   116         -  ** temporary filename in the same directory as the original file.
   117         -  */
   118     98     pMain = db->aDb[0].pBt;
   119         -  zFilename = sqlite3BtreeGetFilename(pMain);
   120         -  assert( zFilename );
   121         -  if( zFilename[0]=='\0' ){
   122         -    /* The in-memory database. Do nothing. Return directly to avoid causing
   123         -    ** an error trying to DETACH the vacuum_db (which never got attached)
   124         -    ** in the exit-handler.
   125         -    */
   126         -    return SQLITE_OK;
   127         -  }
   128         -  nFilename = strlen(zFilename);
   129         -  zTemp = sqliteMalloc( nFilename+100 );
   130         -  if( zTemp==0 ){
   131         -    rc = SQLITE_NOMEM;
   132         -    goto end_of_vacuum;
   133         -  }
   134         -  strcpy(zTemp, zFilename);
   135         -
   136         -  /* The randomName() procedure in the following loop uses an excellent
   137         -  ** source of randomness to generate a name from a space of 1.3e+31 
   138         -  ** possibilities.  So unless the directory already contains on the order
   139         -  ** of 1.3e+31 files, the probability that the following loop will
   140         -  ** run more than once or twice is vanishingly small.  We are certain
   141         -  ** enough that this loop will always terminate (and terminate quickly)
   142         -  ** that we don't even bother to set a maximum loop count.
   143         -  */
   144         -  do {
   145         -    zTemp[nFilename] = '-';
   146         -    randomName((unsigned char*)&zTemp[nFilename+1]);
   147         -  } while( sqlite3OsFileExists(zTemp) );
           99  +  sqlite3OsTempFileName(zTemp);
   148    100   
   149    101     /* Attach the temporary database as 'vacuum_db'. The synchronous pragma
   150    102     ** can be set to 'off' for this file, as it is not recovered if a crash
   151    103     ** occurs anyway. The integrity of the database is maintained by a
   152    104     ** (possibly synchronous) transaction opened on the main database before
   153    105     ** sqlite3BtreeCopyFile() is called.
   154    106     **
................................................................................
   303    255       sqlite3MallocDisallow();
   304    256       sqlite3BtreeClose(pDb->pBt);
   305    257       sqlite3MallocAllow();
   306    258       pDb->pBt = 0;
   307    259       pDb->pSchema = 0;
   308    260     }
   309    261   
   310         -  if( zTemp ){
   311         -    sqlite3OsDelete(zTemp);
   312         -    sqliteFree(zTemp);
   313         -  }
   314    262     sqliteFree( zSql );
   315    263     sqlite3ResetInternalSchema(db, 0);
   316    264   
   317    265     return rc;
   318    266   }
   319    267   #endif  /* SQLITE_OMIT_VACUUM */