interplane.Get Omega
Read in your language

sqlite

[Ported from github.com/sqlite/sqlite] Fix bugs in and improve the interface to the ".check" command of the CLI.

Brought by drakestapleton

branch master Branches Tags History

sqlite / src / test_delete.c

160 lines · 4646 bytes Raw Blame History
1/*
2** 2016 September 10
3**
4** The author disclaims copyright to this source code. In place of
5** a legal notice, here is a blessing:
6**
7** May you do good and not evil.
8** May you find forgiveness for yourself and forgive others.
9** May you share freely, never taking more than you give.
10**
11*************************************************************************
12** This file contains test code to delete an SQLite database and all
13** of its associated files. Associated files include:
14**
15** * The journal file.
16** * The wal file.
17** * The SQLITE_ENABLE_8_3_NAMES version of the db, journal or wal files.
18** * Files created by the test_multiplex.c module to extend any of the
19** above.
20*/
21
22#ifndef SQLITE_OS_WIN
23# define SQLITE_OS_WIN 0
24#endif
25
26#if !SQLITE_OS_WIN
27# include <unistd.h>
28# include <errno.h>
29#endif
30#include <string.h>
31#include <assert.h>
32#include "sqlite3.h"
33
34/* The following #defines are copied from test_multiplex.c */
35#ifndef MX_CHUNK_NUMBER
36# define MX_CHUNK_NUMBER 299
37#endif
38#ifndef SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET
39# define SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET 400
40#endif
41#ifndef SQLITE_MULTIPLEX_WAL_8_3_OFFSET
42# define SQLITE_MULTIPLEX_WAL_8_3_OFFSET 700
43#endif
44
45/*
46** This routine is a copy of (most of) the code from SQLite function
47** sqlite3FileSuffix3(). It modifies the filename in buffer z in the
48** same way as SQLite does when in 8.3 filenames mode.
49*/
50static void sqlite3Delete83Name(char *z){
51 int i, sz;
52 sz = (int)strlen(z);
53 for(i=sz-1; i>0 && z[i]!='/' && z[i]!='.'; i--){}
54 if( z[i]=='.' && (sz>i+4) ) memmove(&z[i+1], &z[sz-3], 4);
55}
56
57/*
58** zFile is a filename. Assuming no error occurs, if this file exists,
59** set *pbExists to true and unlink it. Or, if the file does not exist,
60** set *pbExists to false before returning.
61**
62** If an error occurs, non-zero is returned. Or, if no error occurs, zero.
63*/
64static int sqlite3DeleteUnlinkIfExists(
65 sqlite3_vfs *pVfs,
66 const char *zFile,
67 int *pbExists
68){
69 int rc = SQLITE_ERROR;
70#if SQLITE_OS_WIN
71 if( pVfs ){
72 if( pbExists ) *pbExists = 1;
73 rc = pVfs->xDelete(pVfs, zFile, 0);
74 if( rc==SQLITE_IOERR_DELETE_NOENT ){
75 if( pbExists ) *pbExists = 0;
76 rc = SQLITE_OK;
77 }
78 }
79#else
80 assert( pVfs==0 );
81 rc = access(zFile, F_OK);
82 if( rc ){
83 if( errno==ENOENT ){
84 if( pbExists ) *pbExists = 0;
85 rc = SQLITE_OK;
86 }
87 }else{
88 if( pbExists ) *pbExists = 1;
89 rc = unlink(zFile);
90 }
91#endif
92 return rc;
93}
94
95/*
96** Delete the database file identified by the string argument passed to this
97** function. The string must contain a filename, not an SQLite URI.
98*/
99SQLITE_API int sqlite3_delete_database(
100 const char *zFile /* File to delete */
101){
102 char *zBuf; /* Buffer to sprintf() filenames to */
103 int nBuf; /* Size of buffer in bytes */
104 int rc = 0; /* System error code */
105 int i; /* Iterate through azFmt[] and aMFile[] */
106
107 const char *azFmt[] = { "%s", "%s-journal", "%s-wal", "%s-shm" };
108
109 struct MFile {
110 const char *zFmt;
111 int iOffset;
112 int b83;
113 } aMFile[] = {
114 { "%s%03d", 0, 0 },
115 { "%s-journal%03d", 0, 0 },
116 { "%s-wal%03d", 0, 0 },
117 { "%s%03d", 0, 1 },
118 { "%s-journal%03d", SQLITE_MULTIPLEX_JOURNAL_8_3_OFFSET, 1 },
119 { "%s-wal%03d", SQLITE_MULTIPLEX_WAL_8_3_OFFSET, 1 },
120 };
121
122#if SQLITE_OS_WIN
123 sqlite3_vfs *pVfs = sqlite3_vfs_find("win32");
124#else
125 sqlite3_vfs *pVfs = 0;
126#endif
127
128 /* Allocate a buffer large enough for any of the files that need to be
129 ** deleted. */
130 nBuf = (int)strlen(zFile) + 100;
131 zBuf = (char*)sqlite3_malloc(nBuf);
132 if( zBuf==0 ) return SQLITE_NOMEM;
133
134 /* Delete both the regular and 8.3 filenames versions of the database,
135 ** journal, wal and shm files. */
136 for(i=0; rc==0 && i<sizeof(azFmt)/sizeof(azFmt[0]); i++){
137 sqlite3_snprintf(nBuf, zBuf, azFmt[i], zFile);
138 rc = sqlite3DeleteUnlinkIfExists(pVfs, zBuf, 0);
139 if( rc==0 && i!=0 ){
140 sqlite3Delete83Name(zBuf);
141 rc = sqlite3DeleteUnlinkIfExists(pVfs, zBuf, 0);
142 }
143 }
144
145 /* Delete any multiplexor files */
146 for(i=0; rc==0 && i<sizeof(aMFile)/sizeof(aMFile[0]); i++){
147 struct MFile *p = &aMFile[i];
148 int iChunk;
149 for(iChunk=1; iChunk<=MX_CHUNK_NUMBER; iChunk++){
150 int bExists;
151 sqlite3_snprintf(nBuf, zBuf, p->zFmt, zFile, iChunk+p->iOffset);
152 if( p->b83 ) sqlite3Delete83Name(zBuf);
153 rc = sqlite3DeleteUnlinkIfExists(pVfs, zBuf, &bExists);
154 if( bExists==0 || rc!=0 ) break;
155 }
156 }
157
158 sqlite3_free(zBuf);
159 return (rc ? SQLITE_ERROR : SQLITE_OK);
160}