From 128011a25846785a472f7c60c082693583f83df5 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 9 Jan 2018 10:29:34 +0000 Subject: [PATCH] Fix a zipfile problem with adding new directories to an archive. FossilOrigin-Name: 5fed67033c9dd4492bf8cfcf98874284581f448d8cc84fa5470dde239f218375 --- ext/misc/zipfile.c | 70 +++++++++++++++++++++++++++++++--------------- manifest | 14 +++++----- manifest.uuid | 2 +- test/zipfile.test | 22 +++++++++++++++ 4 files changed, 78 insertions(+), 30 deletions(-) diff --git a/ext/misc/zipfile.c b/ext/misc/zipfile.c index f9acdf014a..93b2a6d8bc 100644 --- a/ext/misc/zipfile.c +++ b/ext/misc/zipfile.c @@ -1152,11 +1152,16 @@ static int zipfileAppendEntry( return rc; } -static int zipfileGetMode(ZipfileTab *pTab, sqlite3_value *pVal, u32 *pMode){ +static int zipfileGetMode( + ZipfileTab *pTab, + sqlite3_value *pVal, + u32 defaultMode, /* Value to use if pVal IS NULL */ + u32 *pMode +){ const char *z = (const char*)sqlite3_value_text(pVal); u32 mode = 0; if( z==0 ){ - mode = S_IFREG + 0644; /* -rw-r--r-- */ + mode = defaultMode; }else if( z[0]>=0 && z[0]<=9 ){ mode = (unsigned int)sqlite3_value_int(pVal); }else{ @@ -1208,6 +1213,10 @@ static int zipfileUpdate( u8 *pFree = 0; /* Free this */ ZipfileCDS cds; /* New Central Directory Structure entry */ + int bIsDir = 0; + + int mNull; + assert( pTab->zFile ); assert( pTab->pWriteFd ); @@ -1223,20 +1232,17 @@ static int zipfileUpdate( if( nVal==1 ) return SQLITE_OK; } - zPath = (const char*)sqlite3_value_text(apVal[2]); - nPath = (int)strlen(zPath); - rc = zipfileGetMode(pTab, apVal[3], &mode); - if( rc!=SQLITE_OK ) return rc; - if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){ - mTime = (sqlite3_int64)time(0); - }else{ - mTime = sqlite3_value_int64(apVal[4]); + mNull = (sqlite3_value_type(apVal[5])==SQLITE_NULL ? 0x0 : 0x8) /* sz */ + + (sqlite3_value_type(apVal[6])==SQLITE_NULL ? 0x0 : 0x4) /* rawdata */ + + (sqlite3_value_type(apVal[7])==SQLITE_NULL ? 0x0 : 0x2) /* data */ + + (sqlite3_value_type(apVal[8])==SQLITE_NULL ? 0x0 : 0x1); /* method */ + if( mNull==0x00 ){ + /* All four are NULL - this must be a directory */ + bIsDir = 1; } - - if( sqlite3_value_type(apVal[5])==SQLITE_NULL /* sz */ - && sqlite3_value_type(apVal[6])==SQLITE_NULL /* rawdata */ - && sqlite3_value_type(apVal[7])!=SQLITE_NULL /* data */ - ){ + else if( mNull==0x2 || mNull==0x3 ){ + /* Value specified for "data", and possibly "method". This must be + ** a regular file or a symlink. */ const u8 *aIn = sqlite3_value_blob(apVal[7]); int nIn = sqlite3_value_bytes(apVal[7]); int bAuto = sqlite3_value_type(apVal[8])==SQLITE_NULL; @@ -1257,12 +1263,10 @@ static int zipfileUpdate( } } } - }else - if( sqlite3_value_type(apVal[5])!=SQLITE_NULL /* sz */ - && sqlite3_value_type(apVal[6])!=SQLITE_NULL /* rawdata */ - && sqlite3_value_type(apVal[7])==SQLITE_NULL /* data */ - && sqlite3_value_type(apVal[8])!=SQLITE_NULL /* method */ - ){ + } + else if( mNull==0x0D ){ + /* Values specified for "sz", "rawdata" and "method". In other words, + ** pre-compressed data is being inserted. */ pData = sqlite3_value_blob(apVal[6]); nData = sqlite3_value_bytes(apVal[6]); sz = sqlite3_value_int(apVal[5]); @@ -1273,10 +1277,32 @@ static int zipfileUpdate( ); rc = SQLITE_ERROR; } - }else{ + } + else{ rc = SQLITE_CONSTRAINT; } + if( rc==SQLITE_OK ){ + rc = zipfileGetMode(pTab, apVal[3], + (bIsDir ? (S_IFDIR + 0755) : (S_IFREG + 0644)), &mode + ); + if( rc==SQLITE_OK && (bIsDir == ((mode & S_IFDIR)==0)) ){ + /* The "mode" attribute is a directory, but data has been specified. + ** Or vice-versa - no data but "mode" is a file or symlink. */ + rc = SQLITE_CONSTRAINT; + } + } + + if( rc==SQLITE_OK ){ + zPath = (const char*)sqlite3_value_text(apVal[2]); + nPath = (int)strlen(zPath); + if( sqlite3_value_type(apVal[4])==SQLITE_NULL ){ + mTime = (sqlite3_int64)time(0); + }else{ + mTime = sqlite3_value_int64(apVal[4]); + } + } + if( rc==SQLITE_OK ){ /* Create the new CDS record. */ memset(&cds, 0, sizeof(cds)); diff --git a/manifest b/manifest index 750572561e..309f6238dc 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\sproblem\sin\szipfile.c\sfound\sby\s-fsanitize. -D 2018-01-09T07:16:51.597 +C Fix\sa\szipfile\sproblem\swith\sadding\snew\sdirectories\sto\san\sarchive. +D 2018-01-09T10:29:34.950 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 12b6daa4bdb03fa87da27cbc205ff88ace645475b5be79414a3038b68ade14cb @@ -303,7 +303,7 @@ F ext/misc/vfsstat.c bf10ef0bc51e1ad6756629e1edb142f7a8db1178 F ext/misc/vtablog.c 31d0d8f4406795679dcd3a67917c213d3a2a5fb3ea5de35f6e773491ed7e13c9 F ext/misc/vtshim.c 1976e6dd68dd0d64508c91a6dfab8e75f8aaf6cd F ext/misc/wholenumber.c 784b12543d60702ebdd47da936e278aa03076212 -F ext/misc/zipfile.c b694574b715dc6ea353c90e0fa31f30faa2121e59bd9e1ae40f874300114a84f +F ext/misc/zipfile.c eaab322dd4b24199b0c6bdfbf866d1d04f7e93f46b4b5ca8436edadece54b03f F ext/rbu/rbu.c ea7d1b7eb44c123a2a619332e19fe5313500705c4a58aaa1887905c0d83ffc2e F ext/rbu/rbu1.test 43836fac8c7179a358eaf38a8a1ef3d6e6285842 F ext/rbu/rbu10.test 1846519a438697f45e9dcb246908af81b551c29e1078d0304fae83f1fed7e9ee @@ -1598,7 +1598,7 @@ F test/wordcount.c cb589cec469a1d90add05b1f8cee75c7210338d87a5afd65260ed5c0f4bbf F test/writecrash.test f1da7f7adfe8d7f09ea79b42e5ca6dcc41102f27f8e334ad71539501ddd910cc F test/zeroblob.test 3857870fe681b8185654414a9bccfde80b62a0fa F test/zerodamage.test e59a56443d6298ecf7435f618f0b27654f0c849e -F test/zipfile.test ad4278e1ebb1c7bc0fcd7f9b47df18916b9e8f841165119865a5a6a095a2d0ba +F test/zipfile.test de2ee377705999bcd7391fb742d6b833ffab4f21dab1d4f484098a66d60eb4fb F tool/GetFile.cs a15e08acb5dd7539b75ba23501581d7c2b462cb5 F tool/GetTclKit.bat 8995df40c4209808b31f24de0b58f90930239a234f7591e3675d45bfbb990c5d F tool/Replace.cs 02c67258801c2fb5f63231e0ac0f220b4b36ba91 @@ -1697,7 +1697,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P cba0206a15f30313e16a08634995ebfd5d325d83affb859a215e72509f539b4e -R 8cb58dd5bd00b76b67672fc7e3030d3c +P 4fe697fa6c2b45aec60c33eff1ce2ea97b8a2ca124ef0c0059930269d25cdb2e +R e226ce112b5f76c5d01abec530de4ed7 U dan -Z bb6663f307bb629de0b63290f8475ee1 +Z 162125bc358610b2bc72c2a7b07e618b diff --git a/manifest.uuid b/manifest.uuid index 02e21911d2..1a39c77410 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -4fe697fa6c2b45aec60c33eff1ce2ea97b8a2ca124ef0c0059930269d25cdb2e \ No newline at end of file +5fed67033c9dd4492bf8cfcf98874284581f448d8cc84fa5470dde239f218375 \ No newline at end of file diff --git a/test/zipfile.test b/test/zipfile.test index 4e0c9b18d1..4cb6630e09 100644 --- a/test/zipfile.test +++ b/test/zipfile.test @@ -79,5 +79,27 @@ do_execsql_test 1.6.0 { SELECT name FROM zz; } {f.txt h.txt i.txt} +#------------------------------------------------------------------------- +db close +forcedelete test.zip +reset_db +load_static_extension db zipfile +do_execsql_test 2.1 { + CREATE VIRTUAL TABLE zzz USING zipfile('test.zip'); + INSERT INTO zzz(name, mode) VALUES('dirname', 'drwxr-xr-x'); + SELECT name, mode, data FROM zzz; +} {dirname 16877 {}} +do_execsql_test 2.2 { + INSERT INTO zzz(name, data) VALUES('dirname2', NULL); + INSERT INTO zzz(name, data) VALUES('dirname2/file1.txt', 'abcdefghijklmnop'); + SELECT name, mode, data FROM zzz; +} { + dirname 16877 {} + dirname2 16877 {} + dirname2/file1.txt 33188 abcdefghijklmnop +} + + + finish_test -- 2.47.2