diff options
| -rw-r--r-- | ftparchive/cachedb.h | 7 | ||||
| -rw-r--r-- | ftparchive/override.cc | 12 | ||||
| -rw-r--r-- | ftparchive/writer.cc | 33 | ||||
| -rwxr-xr-x | test/integration/test-apt-ftparchive-cachedb | 73 |
4 files changed, 97 insertions, 28 deletions
diff --git a/ftparchive/cachedb.h b/ftparchive/cachedb.h index 98817b9ec..0002c6059 100644 --- a/ftparchive/cachedb.h +++ b/ftparchive/cachedb.h @@ -34,7 +34,7 @@ class CacheDB // Database state/access DBT Key; DBT Data; - char TmpKey[600]; + std::string TmpKey; DB *Dbp; bool DBLoaded; bool ReadOnly; @@ -45,8 +45,9 @@ class CacheDB { memset(&Key,0,sizeof(Key)); memset(&Data,0,sizeof(Data)); - Key.data = TmpKey; - Key.size = snprintf(TmpKey,sizeof(TmpKey),"%s:%s",FileName.c_str(), Type); + TmpKey.assign(FileName).append(":").append(Type); + Key.data = TmpKey.data(); + Key.size = TmpKey.size(); } void InitQueryStats() { diff --git a/ftparchive/override.cc b/ftparchive/override.cc index 02d1566c8..a6768d9d9 100644 --- a/ftparchive/override.cc +++ b/ftparchive/override.cc @@ -36,9 +36,10 @@ bool Override::ReadOverride(string const &File,bool const &Source) if (F == 0) return _error->Errno("fopen",_("Unable to open %s"),File.c_str()); - char Line[1000]; + char *Line = nullptr; + size_t LineSize = 0; unsigned long long Counter = 0; - while (fgets(Line,sizeof(Line),F) != 0) + while (getline(&Line, &LineSize, F) != -1) { Counter++; Item Itm; @@ -124,6 +125,7 @@ bool Override::ReadOverride(string const &File,bool const &Source) if (ferror(F)) _error->Errno("fgets",_("Failed to read the override file %s"),File.c_str()); + free(Line); fclose(F); return true; } @@ -140,9 +142,10 @@ bool Override::ReadExtraOverride(string const &File,bool const &/*Source*/) if (F == 0) return _error->Errno("fopen",_("Unable to open %s"),File.c_str()); - char Line[1000]; + char *Line = nullptr; + size_t LineSize = 0; unsigned long long Counter = 0; - while (fgets(Line,sizeof(Line),F) != 0) + while (getline(&Line, &LineSize, F) != -1) { Counter++; @@ -198,6 +201,7 @@ bool Override::ReadExtraOverride(string const &File,bool const &/*Source*/) if (ferror(F)) _error->Errno("fgets",_("Failed to read the override file %s"),File.c_str()); + free(Line); fclose(F); return true; } diff --git a/ftparchive/writer.cc b/ftparchive/writer.cc index a5b889c15..3c5237915 100644 --- a/ftparchive/writer.cc +++ b/ftparchive/writer.cc @@ -242,34 +242,26 @@ bool FTWScanner::LoadFileList(string const &Dir, string const &File) if (List == 0) return _error->Errno("fopen",_("Failed to open %s"),File.c_str()); - /* We are a tad tricky here.. We prefix the buffer with the directory - name, that way if we need a full path with just use line.. Sneaky and - fully evil. */ - char Line[1000]; - char *FileStart; - if (Dir.empty() == true || Dir.end()[-1] != '/') - FileStart = Line + snprintf(Line,sizeof(Line),"%s/",Dir.c_str()); - else - FileStart = Line + snprintf(Line,sizeof(Line),"%s",Dir.c_str()); - while (fgets(FileStart,sizeof(Line) - (FileStart - Line),List) != 0) + std::string FilePathBuf; + char *Line = nullptr; + size_t LineLength = 0; + while (getline(&Line, &LineLength, List) != -1) { - char *FileName = _strstrip(FileStart); + const char *FileName = _strstrip(Line); if (FileName[0] == 0) continue; + // Relative path, prepend directory to make absolute if (FileName[0] != '/') { - if (FileName != FileStart) - memmove(FileStart,FileName,strlen(FileStart)); - FileName = Line; + if (Dir.empty() == true || Dir.end()[-1] != '/') + strprintf(FilePathBuf, "%s/%s", Dir.c_str(), FileName); + else + strprintf(FilePathBuf, "%s%s", Dir.c_str(), FileName); + + FileName = FilePathBuf.c_str(); } -#if 0 - struct stat St; - int Flag = FTW_F; - if (stat(FileName,&St) != 0) - Flag = FTW_NS; -#endif if (FileMatchesPatterns(FileName, Patterns) == false) continue; @@ -277,6 +269,7 @@ bool FTWScanner::LoadFileList(string const &Dir, string const &File) break; } + free(Line); fclose(List); return true; } diff --git a/test/integration/test-apt-ftparchive-cachedb b/test/integration/test-apt-ftparchive-cachedb index 73d762cc8..f6e71ccdb 100755 --- a/test/integration/test-apt-ftparchive-cachedb +++ b/test/integration/test-apt-ftparchive-cachedb @@ -1,6 +1,11 @@ #!/bin/sh set -e +db_dump=db_dump +if command -v db_dump-5 >/dev/null 2>&1; then + db_dump=db_dump-5 +fi + ensure_correct_packages_file() { testequal "Package: foo Architecture: i386 @@ -33,6 +38,8 @@ mkdir -p aptarchive/dists/test/main/binary-i386 mkdir -p aptarchive/pool/main mkdir aptarchive-overrides +touch aptarchive-overrides/bin-override +touch aptarchive-overrides/extra-override mkdir aptarchive-cache cat > ftparchive.conf <<"EOF" Dir { @@ -60,7 +67,9 @@ TreeDefault { Tree "dists/test" { Sections "main"; Architectures "i386"; - + BinOverride "bin-override"; + ExtraOverride "extra-override"; + //FileList "file-list"; }; EOF @@ -94,6 +103,67 @@ ensure_correct_contents_file testsuccessequal ' Misses in Cache: 0 dists/test/Contents-i386: New 402 B Misses in Cache: 0' grep Misses stats-out.txt +msgmsg "Test overrides" + +manyX=$(head -c2000 /dev/zero | tr '\0' 'X') + +echo "foo priority${manyX} overrideSection" > aptarchive-overrides/bin-override +echo "foo Extra ${manyX}trailer" > aptarchive-overrides/extra-override + +testsuccess aptftparchive generate ftparchive.conf -o APT::FTPArchive::ShowCacheMisses=1 +testsuccessequal "Priority: priority${manyX}" grep ^Priority ./aptarchive/dists/test/main/binary-i386/Packages +testsuccessequal "Section: overrideSection" grep ^Section ./aptarchive/dists/test/main/binary-i386/Packages +testsuccessequal "Extra: ${manyX}trailer" grep ^Extra ./aptarchive/dists/test/main/binary-i386/Packages + + +msgmsg "Test long paths" +x128=$(head -c 128 /dev/zero | tr '\0' 'X') +longPath=${x128}/${x128}/${x128}/${x128}/${x128}/${x128}/${x128}/${x128} +mkdir -p aptarchive/pool/main/$longPath +mv aptarchive/pool/main/foo_1_i386.deb aptarchive/pool/main/$longPath/foo_1_i386.deb + +# before +testsuccess $db_dump -f dump -p aptarchive-cache/packages-main-i386.db +testsuccess grep "^ ./aptarchive/pool/main/foo_1_i386.deb:cl$" dump +testsuccess grep "^ ./aptarchive/pool/main/foo_1_i386.deb:cn$" dump +testsuccess grep "^ ./aptarchive/pool/main/foo_1_i386.deb:st$" dump +testsuccess aptftparchive generate ftparchive.conf -o APT::FTPArchive::ShowCacheMisses=1 + +# file is still found +testsuccessequal "Filename: pool/main/${longPath}/foo_1_i386.deb" grep ^Filename ./aptarchive/dists/test/main/binary-i386/Packages +testsuccessequal "Priority: priority${manyX}" grep ^Priority ./aptarchive/dists/test/main/binary-i386/Packages +testsuccessequal "Section: overrideSection" grep ^Section ./aptarchive/dists/test/main/binary-i386/Packages +testsuccessequal "Extra: ${manyX}trailer" grep ^Extra ./aptarchive/dists/test/main/binary-i386/Packages + +# file is in the database +testsuccess $db_dump -f dump -p aptarchive-cache/packages-main-i386.db +testsuccess grep "^ ./aptarchive/pool/main/${longPath}/foo_1_i386.deb:cl$" dump +testsuccess grep "^ ./aptarchive/pool/main/${longPath}/foo_1_i386.deb:cn$" dump +testsuccess grep "^ ./aptarchive/pool/main/${longPath}/foo_1_i386.deb:st"$ dump + +msgmsg "Test file lists" + +# Check that the empty file list produces no packages +rm aptarchive-cache/packages-main-i386.db +testsuccess sed -i s#//FileList#FileList# ftparchive.conf +testsuccess aptftparchive generate ftparchive.conf -o APT::FTPArchive::ShowCacheMisses=1 +testfileequal ./aptarchive/dists/test/main/binary-i386/Packages "" + +# Add the packages and run our checks again +echo "pool/main/${longPath}/foo_1_i386.deb" > file-list +testsuccess aptftparchive generate ftparchive.conf -o APT::FTPArchive::ShowCacheMisses=1 +testsuccessequal "Filename: pool/main/${longPath}/foo_1_i386.deb" grep ^Filename ./aptarchive/dists/test/main/binary-i386/Packages +testsuccessequal "Priority: priority${manyX}" grep ^Priority ./aptarchive/dists/test/main/binary-i386/Packages +testsuccessequal "Section: overrideSection" grep ^Section ./aptarchive/dists/test/main/binary-i386/Packages +testsuccessequal "Extra: ${manyX}trailer" grep ^Extra ./aptarchive/dists/test/main/binary-i386/Packages + +# file is in the database +testsuccess $db_dump -f dump -p aptarchive-cache/packages-main-i386.db +testsuccess grep "^ ./aptarchive/pool/main/${longPath}/foo_1_i386.deb:cl$" dump +testsuccess grep "^ ./aptarchive/pool/main/${longPath}/foo_1_i386.deb:cn$" dump +testsuccess grep "^ ./aptarchive/pool/main/${longPath}/foo_1_i386.deb:st"$ dump + +msgmsg "Test Clean" # and clean rm -rf aptarchive/pool/main/* testsuccessequal "packages-main-i386.db" aptftparchive clean ftparchive.conf @@ -101,3 +171,4 @@ testsuccess aptftparchive clean ftparchive.conf -o Debug::APT::FTPArchive::Clean cp rootdir/tmp/testsuccess.output clean-out.txt testsuccessequal "0 Number of unique keys in the tree" grep unique clean-out.txt testsuccessequal "packages-main-i386.db" grep packages-main-i386.db clean-out.txt + |
