SQLite

Check-in [92f4188f90]
Login

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

Overview
Comment:Enhancements to test_quota.c: Remove the external dependency on sqlite3_win32_utf8_to_msbc(). Add an extra parameter to quota_fflush() that will also do an fsync (or the equivalent).
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | quota-stdio
Files: files | file ages | folders
SHA1: 92f4188f90e3cdd71f1457a6e0eb22615e4a54f4
User & Date: drh 2011-12-13 23:26:10.637
Context
2011-12-14
00:04
Harden the utf8-to-mbcs converter in the quota module against failures. (check-in: 1cda511deb user: drh tags: quota-stdio)
2011-12-13
23:26
Enhancements to test_quota.c: Remove the external dependency on sqlite3_win32_utf8_to_msbc(). Add an extra parameter to quota_fflush() that will also do an fsync (or the equivalent). (check-in: 92f4188f90 user: drh tags: quota-stdio)
2011-12-12
19:47
Make sure the quota logic is usable as C++. (check-in: f4534bd302 user: drh tags: quota-stdio)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/test_quota.c.
400
401
402
403
404
405
406






407
408
409
410
411
412
413
414






415












416
417
418
419
420
421
422
423
# endif
#else
# ifndef SQLITE_OS_WIN
#  define SQLITE_OS_WIN 0
# endif
#endif








/*
** Translate UTF8 to MBCS for use in fopen() calls.  Return a pointer to the
** translated text..  Call quota_mbcs_free() to deallocate any memory
** used to store the returned pointer when done.
*/
static char *quota_utf8_to_mbcs(const char *zUtf8){
#if SQLITE_OS_WIN






  extern char *sqlite3_win32_utf8_to_mbcs(const char*);












  return sqlite3_win32_utf8_to_mbcs(zUtf8);
#else
  return (char*)zUtf8;  /* No-op on unix */
#endif  
}

/*
** Deallocate any memory allocated by quota_utf8_to_mbcs().







>
>
>
>
>
>








>
>
>
>
>
>
|
>
>
>
>
>
>
>
>
>
>
>
>
|







400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
# endif
#else
# ifndef SQLITE_OS_WIN
#  define SQLITE_OS_WIN 0
# endif
#endif

#if SQLITE_OS_UNIX
# include <unistd.h>
#endif
#if SQLITE_OS_WIN
# include <windows.h>
#endif

/*
** Translate UTF8 to MBCS for use in fopen() calls.  Return a pointer to the
** translated text..  Call quota_mbcs_free() to deallocate any memory
** used to store the returned pointer when done.
*/
static char *quota_utf8_to_mbcs(const char *zUtf8){
#if SQLITE_OS_WIN
  int n;             /* Bytes in zUtf8 */
  int nWide;         /* number of UTF-16 characters */
  int nMbcs;         /* Bytes of MBCS */
  LPWSTR zTmpWide;   /* The UTF16 text */
  char *zMbcs;       /* The MBCS text */
  int codepage;      /* Code page used by fopen() */

  n = strlen(zUtf8);
  nWide = MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, NULL, 0);
  zTmpWide = sqlite3_malloc( nWide*sizeof(zTmpWide[0]) );
  if( zTmpWide==0 ) return 0;
  MultiByteToWideChar(CP_UTF8, 0, zUtf8, -1, zTmpWide, nWide);
  codepage = AreFileApisANSI() ? CP_ACP : CP_OEMCP;
  nMbcs = WideCharToMultiByte(codepage, 0, zTmpWide, nWide, 0, 0, 0, 0);
  zMbcs = sqlite3_malloc( nMbcs+1 );
  if( zMbcs ){
    WideCharToMultiByte(codepage, 0, zTmpWide, nWide, zMbcs, nMbcs, 0, 0);
  }
  sqlite3_free(zTmpWide);
  return zMbcs;
#else
  return (char*)zUtf8;  /* No-op on unix */
#endif  
}

/*
** Deallocate any memory allocated by quota_utf8_to_mbcs().
1056
1057
1058
1059
1060
1061
1062
1063

1064









1065
1066
1067
1068
1069
1070
1071
  sqlite3_free(p);
  return rc;
}

/*
** Flush memory buffers for a quota_FILE to disk.
*/
int sqlite3_quota_fflush(quota_FILE *p){

  return fflush(p->f);









}

/*
** Seek on a quota_FILE stream.
*/
int sqlite3_quota_fseek(quota_FILE *p, long offset, int whence){
  return fseek(p->f, offset, whence);







|
>
|
>
>
>
>
>
>
>
>
>







1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
  sqlite3_free(p);
  return rc;
}

/*
** Flush memory buffers for a quota_FILE to disk.
*/
int sqlite3_quota_fflush(quota_FILE *p, int doFsync){
  int rc;
  rc = fflush(p->f);
  if( rc==0 && doFsync ){
#if SQLITE_OS_UNIX
    rc = fsync(fileno(p->f));
#endif
#if SQLITE_OS_WIN
    rc = 0==FlushFileBuffers((HANDLE)_fileno(p->f));
#endif
  }
  return rc;
}

/*
** Seek on a quota_FILE stream.
*/
int sqlite3_quota_fseek(quota_FILE *p, long offset, int whence){
  return fseek(p->f, offset, whence);
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515

1516
1517
1518
1519
1520
1521



1522
1523
1524
1525
1526
1527
1528
1529
  p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  rc = sqlite3_quota_fclose(p);
  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  return TCL_OK;
}

/*
** tclcmd: sqlite3_quota_fflush HANDLE
*/
static int test_quota_fflush(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  quota_FILE *p;
  int rc;


  if( objc!=2 ){
    Tcl_WrongNumArgs(interp, 1, objv, "HANDLE");
    return TCL_ERROR;
  }
  p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));



  rc = sqlite3_quota_fflush(p);
  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  return TCL_OK;
}

/*
** tclcmd: sqlite3_quota_fseek HANDLE OFFSET WHENCE
*/







|









>

|
|



>
>
>
|







1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
  p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  rc = sqlite3_quota_fclose(p);
  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  return TCL_OK;
}

/*
** tclcmd: sqlite3_quota_fflush HANDLE ?HARDSYNC?
*/
static int test_quota_fflush(
  void * clientData,
  Tcl_Interp *interp,
  int objc,
  Tcl_Obj *CONST objv[]
){
  quota_FILE *p;
  int rc;
  int doSync = 0;

  if( objc!=2 && objc!=3 ){
    Tcl_WrongNumArgs(interp, 1, objv, "HANDLE ?HARDSYNC?");
    return TCL_ERROR;
  }
  p = sqlite3TestTextToPtr(Tcl_GetString(objv[1]));
  if( objc==3 ){
    if( Tcl_GetBooleanFromObj(interp, objv[2], &doSync) ) return TCL_ERROR;
  }
  rc = sqlite3_quota_fflush(p, doSync);
  Tcl_SetObjResult(interp, Tcl_NewIntObj(rc));
  return TCL_OK;
}

/*
** tclcmd: sqlite3_quota_fseek HANDLE OFFSET WHENCE
*/
Changes to src/test_quota.h.
154
155
156
157
158
159
160
161





162
163
164
165
166
167
168
169
170
171
** the sum of sizes of all files from going over quota.
*/
size_t sqlite3_quota_fread(void*, size_t, size_t, quota_FILE*);
size_t sqlite3_quota_fwrite(void*, size_t, size_t, quota_FILE*);

/*
** Flush all written content held in memory buffers out to disk.
** This is the equivalent of fflush() in the standard library - not





** an fsync().
*/
int sqlite3_quota_fflush(quota_FILE*);

/*
** Close a quota_FILE object and free all associated resources.  The
** file remains under quota management.
*/
int sqlite3_quota_fclose(quota_FILE*);








|
>
>
>
>
>
|

|







154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
** the sum of sizes of all files from going over quota.
*/
size_t sqlite3_quota_fread(void*, size_t, size_t, quota_FILE*);
size_t sqlite3_quota_fwrite(void*, size_t, size_t, quota_FILE*);

/*
** Flush all written content held in memory buffers out to disk.
** This is the equivalent of fflush() in the standard library.
**
** If the hardSync parameter is true (non-zero) then this routine
** also forces OS buffers to disk - the equivalent of fsync().
**
** This routine return zero on success and non-zero if something goes
** wrong.
*/
int sqlite3_quota_fflush(quota_FILE*, int hardSync);

/*
** Close a quota_FILE object and free all associated resources.  The
** file remains under quota management.
*/
int sqlite3_quota_fclose(quota_FILE*);

Changes to test/quota2.test.
173
174
175
176
177
178
179
180








181
182
183
184
185
186
187
  sqlite3_quota_set */quota2b/* 0 quota_check
  set ::h1 [sqlite3_quota_fopen quota2a/x1/a.txt a]
  sqlite3_quota_fwrite $::h1 10 10 $bigtext
} {10}
do_test quota2-3.2 {
  standard_path [sqlite3_quota_dump]
} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
do_test quota2-3.3 {








  sqlite3_quota_fflush $::h1
  standard_path [sqlite3_quota_dump]
} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
do_test quota2-3.4 {
  sqlite3_quota_fclose $::h1
  standard_path [sqlite3_quota_dump]
} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 0 0}}}







|
>
>
>
>
>
>
>
>







173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
  sqlite3_quota_set */quota2b/* 0 quota_check
  set ::h1 [sqlite3_quota_fopen quota2a/x1/a.txt a]
  sqlite3_quota_fwrite $::h1 10 10 $bigtext
} {10}
do_test quota2-3.2 {
  standard_path [sqlite3_quota_dump]
} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
do_test quota2-3.3a {
  sqlite3_quota_fflush $::h1 0
  standard_path [sqlite3_quota_dump]
} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
do_test quota2-3.3b {
  sqlite3_quota_fflush $::h1 1
  standard_path [sqlite3_quota_dump]
} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
do_test quota2-3.3c {
  sqlite3_quota_fflush $::h1
  standard_path [sqlite3_quota_dump]
} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 1 0}}}
do_test quota2-3.4 {
  sqlite3_quota_fclose $::h1
  standard_path [sqlite3_quota_dump]
} {{*/quota2a/* 4000 100 {PWD/quota2a/x1/a.txt 100 0 0}}}