3 # The author disclaims copyright to this source code. In place of
4 # a legal notice, here is a blessing:
6 # May you do good and not evil.
7 # May you find forgiveness for yourself and forgive others.
8 # May you share freely, never taking more than you give.
10 #***********************************************************************
13 set testdir [file dirname $argv0]
14 source $testdir/tester.tcl
16 # If SQLITE_CURDIR is not defined, omit this file.
22 source $testdir/malloc_common.tcl
25 sqlite3_quota_initialize "" 1
27 foreach dir {quota2a/x1 quota2a/x2 quota2a quota2b quota2c} {
30 foreach dir {quota2a quota2a/x1 quota2a/x2 quota2b quota2c} {
34 # The standard_path procedure converts a pathname into a standard format
35 # that is the same across platforms.
37 unset -nocomplain ::quota_pwd ::quota_mapping
38 set ::quota_pwd [string map {\\ /} [get_pwd]]
39 set ::quota_mapping [list $::quota_pwd PWD]
40 proc standard_path {x} {
41 set x [string map {\\ /} $x]
42 return [string map $::quota_mapping $x]
45 # The quota_check procedure is a callback from the quota handler.
46 # It has three arguments which are (1) the full pathname of the file
47 # that has gone over quota, (2) the quota limit, (3) the requested
48 # new quota size to cover the last write. These three values are
49 # appended to the global variable $::quota. The filename is processed
50 # to convert every \ character into / and to change the name of the
51 # working directory to PWD.
53 # The quota is increased to the request if the ::quota_request_ok
54 # global variable is true.
57 set ::quota_request_ok 0
59 proc quota_check {filename limitvar size} {
61 lappend ::quota [standard_path $filename] [set limit] $size
62 if {$::quota_request_ok} {set limit $size}
65 sqlite3_quota_set */quota2a/* 4000 quota_check
66 sqlite3_quota_set */quota2b/* 5000 quota_check
68 unset -nocomplain bigtext
69 for {set i 1} {$i<=1000} {incr i} {
71 append bigtext [format "%06d\n" $i]
73 append bigtext [format "%06d " $i]
80 set ::h1 [sqlite3_quota_fopen quota2a/xyz.txt w+b]
81 sqlite3_quota_fwrite $::h1 1 7000 $bigtext
85 } {PWD/quota2a/xyz.txt 4000 7000}
86 do_test quota2-1.2.1 {
87 sqlite3_quota_file_size $::h1
89 do_test quota2-1.2.2 {
90 sqlite3_quota_fflush $::h1 1
91 sqlite3_quota_file_truesize $::h1
94 sqlite3_quota_rewind $::h1
95 set ::x [sqlite3_quota_fread $::h1 1001 7]
99 string match $::x [string range $::bigtext 0 3002]
102 sqlite3_quota_fseek $::h1 0 SEEK_END
103 sqlite3_quota_ftell $::h1
106 sqlite3_quota_fseek $::h1 -100 SEEK_END
107 sqlite3_quota_ftell $::h1
110 sqlite3_quota_fseek $::h1 -100 SEEK_CUR
111 sqlite3_quota_ftell $::h1
114 sqlite3_quota_fseek $::h1 50 SEEK_CUR
115 sqlite3_quota_ftell $::h1
118 sqlite3_quota_fseek $::h1 50 SEEK_SET
119 sqlite3_quota_ftell $::h1
121 do_test quota2-1.10 {
122 sqlite3_quota_rewind $::h1
123 sqlite3_quota_ftell $::h1
125 do_test quota2-1.11 {
126 standard_path [sqlite3_quota_dump]
127 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 1 0}}}
128 do_test quota2-1.12 {
129 sqlite3_quota_ftruncate $::h1 3500
130 sqlite3_quota_file_size $::h1
132 do_test quota2-1.13 {
133 sqlite3_quota_file_truesize $::h1
135 do_test quota2-1.14 {
136 standard_path [sqlite3_quota_dump]
137 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 3500 {PWD/quota2a/xyz.txt 3500 1 0}}}
138 do_test quota2-1.15 {
139 sqlite3_quota_fseek $::h1 0 SEEK_END
140 sqlite3_quota_ftell $::h1
142 do_test quota2-1.16 {
143 sqlite3_quota_fwrite $::h1 1 7000 $bigtext
145 do_test quota2-1.17 {
146 sqlite3_quota_ftell $::h1
148 do_test quota2-1.18 {
149 sqlite3_quota_file_size $::h1
151 do_test quota2-1.19 {
152 sqlite3_quota_fflush $::h1 1
153 sqlite3_quota_file_truesize $::h1
155 do_test quota2-1.20 {
156 sqlite3_quota_fclose $::h1
157 standard_path [sqlite3_quota_dump]
158 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 4000 {PWD/quota2a/xyz.txt 4000 0 0}}}
159 do_test quota2-1.21 {
160 sqlite3_quota_remove quota2a/xyz.txt
161 standard_path [sqlite3_quota_dump]
162 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
168 set ::h1 [sqlite3_quota_fopen quota2c/xyz.txt w+b]
169 sqlite3_quota_fwrite $::h1 1 7000 $bigtext
174 do_test quota2-2.3.1 {
175 sqlite3_quota_rewind $::h1
176 sqlite3_quota_file_available $::h1
178 do_test quota2-2.3.2 {
179 set ::x [sqlite3_quota_fread $::h1 1001 7]
182 do_test quota2-2.3.3 {
183 sqlite3_quota_file_available $::h1
186 string match $::x [string range $::bigtext 0 6005]
189 sqlite3_quota_fseek $::h1 0 SEEK_END
190 sqlite3_quota_ftell $::h1
193 sqlite3_quota_fseek $::h1 -100 SEEK_END
194 sqlite3_quota_ftell $::h1
196 do_test quota2-2.6.1 {
197 sqlite3_quota_file_available $::h1
200 sqlite3_quota_fseek $::h1 -100 SEEK_CUR
201 sqlite3_quota_ftell $::h1
203 do_test quota2-2.7.1 {
204 sqlite3_quota_file_available $::h1
207 sqlite3_quota_fseek $::h1 50 SEEK_CUR
208 sqlite3_quota_ftell $::h1
210 do_test quota2-2.8.1 {
211 sqlite3_quota_file_available $::h1
214 sqlite3_quota_fseek $::h1 50 SEEK_SET
215 sqlite3_quota_ftell $::h1
217 do_test quota2-2.9.1 {
218 sqlite3_quota_file_available $::h1
220 do_test quota2-2.10 {
221 sqlite3_quota_rewind $::h1
222 sqlite3_quota_ftell $::h1
224 do_test quota2-2.10.1 {
225 sqlite3_quota_file_available $::h1
227 do_test quota2-2.10.2 {
228 sqlite3_quota_ferror $::h1
230 do_test quota2-2.11 {
231 standard_path [sqlite3_quota_dump]
232 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
233 do_test quota2-2.12 {
234 sqlite3_quota_fclose $::h1
235 standard_path [sqlite3_quota_dump]
236 } {{*/quota2b/* 5000 0} {*/quota2a/* 4000 0}}
239 sqlite3_quota_set */quota2b/* 0 quota_check
240 set ::h1 [sqlite3_quota_fopen quota2a/x1/a.txt a]
241 sqlite3_quota_fwrite $::h1 10 10 $bigtext
244 standard_path [sqlite3_quota_dump]
245 } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
246 do_test quota2-3.3a {
247 sqlite3_quota_fflush $::h1 0
248 standard_path [sqlite3_quota_dump]
249 } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
250 do_test quota2-3.3b {
251 sqlite3_quota_fflush $::h1 1
252 standard_path [sqlite3_quota_dump]
253 } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
254 do_test quota2-3.3c {
255 sqlite3_quota_fflush $::h1
256 standard_path [sqlite3_quota_dump]
257 } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
259 sqlite3_quota_fclose $::h1
260 standard_path [sqlite3_quota_dump]
261 } {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 0 0}}}
263 set ::h2 [sqlite3_quota_fopen quota2a/x2/b.txt a]
264 sqlite3_quota_fwrite $::h2 10 20 $bigtext
265 standard_path [sqlite3_quota_dump]
266 } {{*/quota2a/* 4000 300 {PWD/quota2a/x2/b.txt 200 1 0} {PWD/quota2a/x1/a.txt 100 0 0}}}
268 set ::h3 [sqlite3_quota_fopen quota2a/x1/c.txt a]
269 sqlite3_quota_fwrite $::h3 10 50 $bigtext
270 standard_path [sqlite3_quota_dump]
271 } {{*/quota2a/* 4000 800 {PWD/quota2a/x1/c.txt 500 1 0} {PWD/quota2a/x2/b.txt 200 1 0} {PWD/quota2a/x1/a.txt 100 0 0}}}
273 file exists quota2a/x1/a.txt
276 file exists quota2a/x2/b.txt
279 file exists quota2a/x1/c.txt
281 do_test quota2-3.10 {
282 sqlite3_quota_remove quota2a/x1
283 standard_path [sqlite3_quota_dump]
284 } {{*/quota2a/* 4000 700 {PWD/quota2a/x1/c.txt 500 1 1} {PWD/quota2a/x2/b.txt 200 1 0}}}
285 do_test quota2-3.11 {
286 sqlite3_quota_fclose $::h2
287 sqlite3_quota_fclose $::h3
288 standard_path [sqlite3_quota_dump]
289 } {{*/quota2a/* 4000 200 {PWD/quota2a/x2/b.txt 200 0 0}}}
290 do_test quota2-3.12 {
291 file exists quota2a/x1/a.txt
293 do_test quota2-3.13 {
294 file exists quota2a/x2/b.txt
296 do_test quota2-3.14 {
297 file exists quota2a/x1/c.txt
300 catch { sqlite3_quota_shutdown }
301 catch { unset quota_request_ok }