SQLite Archiver

Check-in [2bf2fef18d]
Login

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

Overview
Comment:Recurse into directories whose names begin with "." as long as they are not named exactly "." or "..". Correctly handle zero-length files.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 2bf2fef18d5245fce1088b1a2e4fee2ec6daa252
User & Date: drh 2014-03-14 00:30:42.317
Context
2014-03-14
00:43
Add a README file. check-in: 6b775f1f03 user: drh tags: trunk
00:30
Recurse into directories whose names begin with "." as long as they are not named exactly "." or "..". Correctly handle zero-length files. check-in: 2bf2fef18d user: drh tags: trunk
00:22
Add a missing fclose() to fix a file-descriptor leak. check-in: c3c0cdcaca user: drh tags: trunk
Changes
Unified Diff Ignore Whitespace Patch
Changes to sar.c.
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  in = fopen(zFilename, "rb");
  if( in==0 ) errorMsg("cannot open \"%s\" for reading\n", zFilename);
  fseek(in, 0, SEEK_END);
  nIn = ftell(in);
  rewind(in);
  zIn = sqlite3_malloc( nIn+1 );
  if( zIn==0 ) errorMsg("cannot malloc for %d bytes\n", nIn+1);
  if( fread(zIn, nIn, 1, in)!=1 ){
    errorMsg("unable to read %d bytes of file %s\n", nIn, zFilename);
  }
  fclose(in);
  nCompr = 13 + nIn + (nIn+999)/1000;
  zCompr = sqlite3_malloc( nCompr+1 );
  if( zCompr==0 ) errorMsg("cannot malloc for %d bytes\n", nCompr+1);
  rc = compress(zCompr, &nCompr, zIn, nIn);







|







139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
  in = fopen(zFilename, "rb");
  if( in==0 ) errorMsg("cannot open \"%s\" for reading\n", zFilename);
  fseek(in, 0, SEEK_END);
  nIn = ftell(in);
  rewind(in);
  zIn = sqlite3_malloc( nIn+1 );
  if( zIn==0 ) errorMsg("cannot malloc for %d bytes\n", nIn+1);
  if( nIn>0 && fread(zIn, nIn, 1, in)!=1 ){
    errorMsg("unable to read %d bytes of file %s\n", nIn, zFilename);
  }
  fclose(in);
  nCompr = 13 + nIn + (nIn+999)/1000;
  zCompr = sqlite3_malloc( nCompr+1 );
  if( zCompr==0 ) errorMsg("cannot malloc for %d bytes\n", nCompr+1);
  rc = compress(zCompr, &nCompr, zIn, nIn);
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  pOut = sqlite3_malloc( sz+1 );
  if( pOut==0 ) errorMsg("cannot allocate %d bytes\n", sz+1);
  nOut = sz;
  rc = uncompress(pOut, &nOut, pCompr, nCompr);
  if( rc!=Z_OK ) errorMsg("uncompress failed for %s\n", zFilename);
  out = fopen(zFilename, "wb");
  if( out==0 ) errorMsg("cannot open for writing: %s\n", zFilename);
  if( fwrite(pOut, nOut, 1, out)!=1 ){
    errorMsg("failed to write: %s\n", zFilename);
  }
  sqlite3_free(pOut);
  fclose(out);
  rc = chmod(zFilename, iMode&0777);
  if( rc ) errorMsg("cannot change mode to %03o: %s\n", iMode, zFilename);
  







|







209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
  pOut = sqlite3_malloc( sz+1 );
  if( pOut==0 ) errorMsg("cannot allocate %d bytes\n", sz+1);
  nOut = sz;
  rc = uncompress(pOut, &nOut, pCompr, nCompr);
  if( rc!=Z_OK ) errorMsg("uncompress failed for %s\n", zFilename);
  out = fopen(zFilename, "wb");
  if( out==0 ) errorMsg("cannot open for writing: %s\n", zFilename);
  if( nOut>0 && fwrite(pOut, nOut, 1, out)!=1 ){
    errorMsg("failed to write: %s\n", zFilename);
  }
  sqlite3_free(pOut);
  fclose(out);
  rc = chmod(zFilename, iMode&0777);
  if( rc ) errorMsg("cannot change mode to %03o: %s\n", iMode, zFilename);
  
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291

292

293
294
295
296
297
298
299
  sqlite3_bind_int(pStmt, 2, x.st_mode);
  sqlite3_bind_int64(pStmt, 3, x.st_mtime);
  if( S_ISREG(x.st_mode) ){
    char *zContent = read_file(zFilename, &szOrig, &szCompr);
    sqlite3_bind_int(pStmt, 4, szOrig);
    sqlite3_bind_blob(pStmt, 5, zContent, szCompr, sqlite3_free);
    if( verboseFlag ){
      int pct = (100*(sqlite3_int64)szCompr)/szOrig;
      printf("  added: %s (deflate %d%%)\n", zFilename, 100-pct);
    } 
  }else{
    sqlite3_bind_int(pStmt, 4, 0);
    sqlite3_bind_null(pStmt, 5);
    if( verboseFlag ) printf("  added: %s\n", zFilename);
  }
  rc = sqlite3_step(pStmt);
  if( rc!=SQLITE_DONE ){
    errorMsg("Insert failed for %s: %s\n", zFilename, sqlite3_errmsg(db));
  }
  sqlite3_reset(pStmt);
  if( S_ISDIR(x.st_mode) ){
    DIR *d;
    struct dirent *pEntry;
    char *zSubpath;
    d = opendir(zFilename);
    if( d ){
      while( (pEntry = readdir(d))!=0 ){

        if( pEntry->d_name[0]=='.' ) continue;

        char *zSubpath = sqlite3_mprintf("%s/%s", zFilename, pEntry->d_name);
        add_file(zSubpath, verboseFlag);
        sqlite3_free(zSubpath);
      }
    }
  }
}







|



















>
|
>







265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
  sqlite3_bind_int(pStmt, 2, x.st_mode);
  sqlite3_bind_int64(pStmt, 3, x.st_mtime);
  if( S_ISREG(x.st_mode) ){
    char *zContent = read_file(zFilename, &szOrig, &szCompr);
    sqlite3_bind_int(pStmt, 4, szOrig);
    sqlite3_bind_blob(pStmt, 5, zContent, szCompr, sqlite3_free);
    if( verboseFlag ){
      int pct = szOrig ? (100*(sqlite3_int64)szCompr)/szOrig : 0;
      printf("  added: %s (deflate %d%%)\n", zFilename, 100-pct);
    } 
  }else{
    sqlite3_bind_int(pStmt, 4, 0);
    sqlite3_bind_null(pStmt, 5);
    if( verboseFlag ) printf("  added: %s\n", zFilename);
  }
  rc = sqlite3_step(pStmt);
  if( rc!=SQLITE_DONE ){
    errorMsg("Insert failed for %s: %s\n", zFilename, sqlite3_errmsg(db));
  }
  sqlite3_reset(pStmt);
  if( S_ISDIR(x.st_mode) ){
    DIR *d;
    struct dirent *pEntry;
    char *zSubpath;
    d = opendir(zFilename);
    if( d ){
      while( (pEntry = readdir(d))!=0 ){
        if( strcmp(pEntry->d_name,".")==0 || strcmp(pEntry->d_name,"..")==0 ){
          continue;
        }
        char *zSubpath = sqlite3_mprintf("%s/%s", zFilename, pEntry->d_name);
        add_file(zSubpath, verboseFlag);
        sqlite3_free(zSubpath);
      }
    }
  }
}