From 31bda5000136d77f516cf2080257835fb44deaef Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 20 Mar 2012 17:05:11 +0100 Subject: * apt-pkg/acquire-worker.cc: - check return of write() as gcc recommends * apt-pkg/acquire.cc: - check return of write() as gcc recommends * apt-pkg/cdrom.cc: - check return of chdir() and link() as gcc recommends * apt-pkg/clean.cc: - check return of chdir() as gcc recommends * apt-pkg/contrib/netrc.cc: - check return of asprintf() as gcc recommends --- apt-pkg/contrib/netrc.cc | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/netrc.cc b/apt-pkg/contrib/netrc.cc index cb7d36088..56e59d84b 100644 --- a/apt-pkg/contrib/netrc.cc +++ b/apt-pkg/contrib/netrc.cc @@ -68,8 +68,7 @@ int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL) if (!home) return -1; - asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC); - if(!netrcfile) + if (asprintf (&netrcfile, "%s%s%s", home, DIR_CHAR, NETRC) == -1 || netrcfile == NULL) return -1; else netrc_alloc = true; -- cgit v1.2.3 From 7efb8c8ef10c1d0b9479c24a6a5b4e96fc0e6286 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 5 Apr 2012 15:18:03 +0200 Subject: detect zlib correctly. We still don't allow to build without it to remain compatible with users accessing it directly, but this prepares for a drop of this strict requirement in the future --- apt-pkg/contrib/fileutl.cc | 32 +++++++++++++------------------- 1 file changed, 13 insertions(+), 19 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 1808489d7..691657cb4 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -44,14 +44,8 @@ #include #include -// FIXME: Compressor Fds have some speed disadvantages and are a bit buggy currently, -// so while the current implementation satisfies the testcases it is not a real option -// to disable it for now -#define APT_USE_ZLIB 1 -#if APT_USE_ZLIB -#include -#else -#pragma message "Usage of zlib is DISABLED!" +#ifdef HAVE_ZLIB + #include #endif #ifdef WORDS_BIGENDIAN @@ -65,7 +59,7 @@ using namespace std; class FileFdPrivate { public: -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB gzFile gz; #else void* gz; @@ -1016,7 +1010,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C d->compressor = compressor; if (compressor.Name == "." || compressor.Binary.empty() == true) return true; -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB else if (compressor.Name == "gzip") { if ((Mode & ReadWrite) == ReadWrite) @@ -1137,7 +1131,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) *((char *)To) = '\0'; do { -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB if (d->gz != NULL) Res = gzread(d->gz,To,Size); else @@ -1149,7 +1143,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) if (errno == EINTR) continue; Flags |= Fail; -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB if (d->gz != NULL) { int err; @@ -1190,7 +1184,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) char* FileFd::ReadLine(char *To, unsigned long long const Size) { *To = '\0'; -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB if (d->gz != NULL) return gzgets(d->gz, To, Size); #endif @@ -1221,7 +1215,7 @@ bool FileFd::Write(const void *From,unsigned long long Size) errno = 0; do { -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB if (d->gz != NULL) Res = gzwrite(d->gz,From,Size); else @@ -1289,7 +1283,7 @@ bool FileFd::Seek(unsigned long long To) return true; } int res; -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB if (d->gz) res = gzseek(d->gz,To,SEEK_SET); else @@ -1325,7 +1319,7 @@ bool FileFd::Skip(unsigned long long Over) } int res; -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB if (d->gz != NULL) res = gzseek(d->gz,Over,SEEK_CUR); else @@ -1373,7 +1367,7 @@ unsigned long long FileFd::Tell() return d->seekpos; off_t Res; -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB if (d->gz != NULL) Res = gztell(d->gz); else @@ -1427,7 +1421,7 @@ unsigned long long FileFd::Size() size = Tell(); Seek(oldSeek); } -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB // only check gzsize if we are actually a gzip file, just checking for // "gz" is not sufficient as uncompressed files could be opened with // gzopen in "direct" mode as well @@ -1500,7 +1494,7 @@ bool FileFd::Close() bool Res = true; if ((Flags & AutoClose) == AutoClose) { -#if APT_USE_ZLIB +#ifdef HAVE_ZLIB if (d != NULL && d->gz != NULL) { int const e = gzclose(d->gz); // gzdclose() on empty files always fails with "buffer error" here, ignore that -- cgit v1.2.3 From 8dd623dbd616ee23dc96a2c99a4415b153dd7290 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 5 Apr 2012 19:02:08 +0200 Subject: if we have zlib builtin insert add a dummy gzip compressor for FileFD --- apt-pkg/contrib/fileutl.cc | 1 - 1 file changed, 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 691657cb4..30d0b6662 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -829,7 +829,6 @@ bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, if (Compress == Auto && (Mode & WriteOnly) == WriteOnly) return _error->Error("Autodetection on %s only works in ReadOnly openmode!", FileName.c_str()); - // FIXME: Denote inbuilt compressors somehow - as we don't need to have the binaries for them std::vector const compressors = APT::Configuration::getCompressors(); std::vector::const_iterator compressor = compressors.begin(); if (Compress == Auto) -- cgit v1.2.3 From c4997486bffc76e2581e9072bff05eba0feeb29c Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 5 Apr 2012 20:51:36 +0200 Subject: - add libbz2-dev as new build-dependency - remove the libz-dev alternative from zlib1g-dev build-dependency - do the same for bz2 builtin if available * apt-pkg/contrib/fileutl.cc: - use libz2 library for (de)compression instead of the bzip2 binary as the first is a dependency of dpkg and the later just priority:optional so we gain 'easier' access to bz2-compressed Translation files this way --- apt-pkg/contrib/fileutl.cc | 107 +++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 98 insertions(+), 9 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 30d0b6662..536571fee 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -47,6 +47,9 @@ #ifdef HAVE_ZLIB #include #endif +#ifdef HAVE_BZ2 + #include +#endif #ifdef WORDS_BIGENDIAN #include @@ -63,6 +66,11 @@ class FileFdPrivate { gzFile gz; #else void* gz; +#endif +#ifdef HAVE_BZ2 + BZFILE* bz2; +#else + void* bz2; #endif int compressed_fd; pid_t compressor_pid; @@ -70,7 +78,8 @@ class FileFdPrivate { APT::Configuration::Compressor compressor; unsigned int openmode; unsigned long long seekpos; - FileFdPrivate() : gz(NULL), compressed_fd(-1), compressor_pid(-1), pipe(false), + FileFdPrivate() : gz(NULL), bz2(NULL), + compressed_fd(-1), compressor_pid(-1), pipe(false), openmode(0), seekpos(0) {}; }; @@ -1017,13 +1026,29 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C else if ((Mode & WriteOnly) == WriteOnly) d->gz = gzdopen(iFd, "w"); else - d->gz = gzdopen (iFd, "r"); + d->gz = gzdopen(iFd, "r"); if (d->gz == NULL) return false; Flags |= Compressed; return true; } #endif +#ifdef HAVE_BZ2 + else if (compressor.Name == "bzip2") + { + if ((Mode & ReadWrite) == ReadWrite) + d->bz2 = BZ2_bzdopen(iFd, "r+"); + else if ((Mode & WriteOnly) == WriteOnly) + d->bz2 = BZ2_bzdopen(iFd, "w"); + else + d->bz2 = BZ2_bzdopen(iFd, "r"); + if (d->bz2 == NULL) + return false; + Flags |= Compressed; + return true; + } +#endif + if ((Mode & ReadWrite) == ReadWrite) return _error->Error("ReadWrite mode is not supported for file %s", FileName.c_str()); @@ -1132,7 +1157,12 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) { #ifdef HAVE_ZLIB if (d->gz != NULL) - Res = gzread(d->gz,To,Size); + Res = gzread(d->gz,To,Size); + else +#endif +#ifdef HAVE_BZ2 + if (d->bz2 != NULL) + Res = BZ2_bzread(d->bz2,To,Size); else #endif Res = read(iFd,To,Size); @@ -1150,6 +1180,15 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) if (err != Z_ERRNO) return _error->Error("gzread: %s (%d: %s)", _("Read error"), err, errmsg); } +#endif +#ifdef HAVE_BZ2 + if (d->bz2 != NULL) + { + int err; + char const * const errmsg = BZ2_bzerror(d->bz2, &err); + if (err != BZ_IO_ERROR) + return _error->Error("BZ2_bzread: %s (%d: %s)", _("Read error"), err, errmsg); + } #endif return _error->Errno("read",_("Read error")); } @@ -1218,6 +1257,11 @@ bool FileFd::Write(const void *From,unsigned long long Size) if (d->gz != NULL) Res = gzwrite(d->gz,From,Size); else +#endif +#ifdef HAVE_BZ2 + if (d->bz2 != NULL) + Res = BZ2_bzwrite(d->bz2,(void*)From,Size); + else #endif Res = write(iFd,From,Size); if (Res < 0 && errno == EINTR) @@ -1225,6 +1269,24 @@ bool FileFd::Write(const void *From,unsigned long long Size) if (Res < 0) { Flags |= Fail; +#ifdef HAVE_ZLIB + if (d->gz != NULL) + { + int err; + char const * const errmsg = gzerror(d->gz, &err); + if (err != Z_ERRNO) + return _error->Error("gzwrite: %s (%d: %s)", _("Write error"), err, errmsg); + } +#endif +#ifdef HAVE_BZ2 + if (d->bz2 != NULL) + { + int err; + char const * const errmsg = BZ2_bzerror(d->bz2, &err); + if (err != BZ_IO_ERROR) + return _error->Error("BZ2_bzwrite: %s (%d: %s)", _("Write error"), err, errmsg); + } +#endif return _error->Errno("write",_("Write error")); } @@ -1246,7 +1308,11 @@ bool FileFd::Write(const void *From,unsigned long long Size) /* */ bool FileFd::Seek(unsigned long long To) { - if (d->pipe == true) + if (d->pipe == true +#ifdef HAVE_BZ2 + || d->bz2 != NULL +#endif + ) { // Our poor man seeking in pipes is costly, so try to avoid it unsigned long long seekpos = Tell(); @@ -1257,6 +1323,10 @@ bool FileFd::Seek(unsigned long long To) if ((d->openmode & ReadOnly) != ReadOnly) return _error->Error("Reopen is only implemented for read-only files!"); +#ifdef HAVE_BZ2 + if (d->bz2 != NULL) + BZ2_bzclose(d->bz2); +#endif close(iFd); iFd = 0; if (TemporaryFileName.empty() == false) @@ -1303,7 +1373,11 @@ bool FileFd::Seek(unsigned long long To) /* */ bool FileFd::Skip(unsigned long long Over) { - if (d->pipe == true) + if (d->pipe == true +#ifdef HAVE_BZ2 + || d->bz2 != NULL +#endif + ) { d->seekpos += Over; char buffer[1024]; @@ -1339,11 +1413,13 @@ bool FileFd::Skip(unsigned long long Over) /* */ bool FileFd::Truncate(unsigned long long To) { - if (d->gz != NULL) +#if defined HAVE_ZLIB || defined HAVE_BZ2 + if (d->gz != NULL || d->bz2 != NULL) { Flags |= Fail; - return _error->Error("Truncating gzipped files is not implemented (%s)", FileName.c_str()); + return _error->Error("Truncating compressed files is not implemented (%s)", FileName.c_str()); } +#endif if (ftruncate(iFd,To) != 0) { Flags |= Fail; @@ -1362,7 +1438,11 @@ unsigned long long FileFd::Tell() // seeking around, but not all users of FileFd use always Seek() and co // so d->seekpos isn't always true and we can just use it as a hint if // we have nothing else, but not always as an authority… - if (d->pipe == true) + if (d->pipe == true +#ifdef HAVE_BZ2 + || d->bz2 != NULL +#endif + ) return d->seekpos; off_t Res; @@ -1409,7 +1489,11 @@ unsigned long long FileFd::Size() // for compressor pipes st_size is undefined and at 'best' zero, // so we 'read' the content and 'seek' back - see there - if (d->pipe == true) + if (d->pipe == true +#ifdef HAVE_BZ2 + || (d->bz2 && size > 0) +#endif + ) { unsigned long long const oldSeek = Tell(); char ignore[1000]; @@ -1500,6 +1584,11 @@ bool FileFd::Close() if (e != 0 && e != Z_BUF_ERROR) Res &= _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str()); } else +#endif +#ifdef HAVE_BZ2 + if (d != NULL && d->bz2 != NULL) + BZ2_bzclose(d->bz2); + else #endif if (iFd > 0 && close(iFd) != 0) Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str()); -- cgit v1.2.3 From d68d65ad637526e46ea77ab83e07470d26df15fc Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 11 Apr 2012 13:25:28 +0200 Subject: use a static FileFd::Write overload to reduce duplication of write()-retry code --- apt-pkg/contrib/fileutl.cc | 22 ++++++++++++++++++++++ apt-pkg/contrib/fileutl.h | 1 + 2 files changed, 23 insertions(+) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 536571fee..9e3611b26 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1301,6 +1301,28 @@ bool FileFd::Write(const void *From,unsigned long long Size) Flags |= Fail; return _error->Error(_("write, still have %llu to write but couldn't"), Size); +} +bool FileFd::Write(int Fd, const void *From, unsigned long long Size) +{ + int Res; + errno = 0; + do + { + Res = write(Fd,From,Size); + if (Res < 0 && errno == EINTR) + continue; + if (Res < 0) + return _error->Errno("write",_("Write error")); + + From = (char *)From + Res; + Size -= Res; + } + while (Res > 0 && Size > 0); + + if (Size == 0) + return true; + + return _error->Error(_("write, still have %llu to write but couldn't"), Size); } /*}}}*/ // FileFd::Seek - Seek in the file /*{{{*/ diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h index 1ca41cb7d..426664d3a 100644 --- a/apt-pkg/contrib/fileutl.h +++ b/apt-pkg/contrib/fileutl.h @@ -78,6 +78,7 @@ class FileFd bool Read(void *To,unsigned long long Size,unsigned long long *Actual = 0); char* ReadLine(char *To, unsigned long long const Size); bool Write(const void *From,unsigned long long Size); + bool static Write(int Fd, const void *From, unsigned long long Size); bool Seek(unsigned long long To); bool Skip(unsigned long long To); bool Truncate(unsigned long long To); -- cgit v1.2.3 From 0b4895d3d223c97c6ba1ece9f7b88479debb7fb7 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 19 Apr 2012 11:05:41 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - redirect stderr from compressors to /dev/null --- apt-pkg/contrib/fileutl.cc | 6 ++++++ 1 file changed, 6 insertions(+) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 9e3611b26..e9d1ba1ce 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1098,6 +1098,12 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C dup2(d->compressed_fd,STDIN_FILENO); dup2(Pipe[1],STDOUT_FILENO); } + int const nullfd = open("/dev/null", O_WRONLY); + if (nullfd != -1) + { + dup2(nullfd,STDERR_FILENO); + close(nullfd); + } SetCloseExec(STDOUT_FILENO,false); SetCloseExec(STDIN_FILENO,false); -- cgit v1.2.3 From 5149632eb5af0831a8728e8bb5d5c483d6e3f147 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 20 Apr 2012 09:53:24 +0200 Subject: * apt-pkg/contrib/sha2_internal.cc: - do not use the input data directly but memcpy it instead as it could be unaligned as in the http-transport which causes a sigbus error on sparc (Closes: #669061) --- apt-pkg/contrib/sha2_internal.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/sha2_internal.cc b/apt-pkg/contrib/sha2_internal.cc index 6d27e8f2b..83b5a98d3 100644 --- a/apt-pkg/contrib/sha2_internal.cc +++ b/apt-pkg/contrib/sha2_internal.cc @@ -552,7 +552,9 @@ void SHA256_Update(SHA256_CTX* context, const sha2_byte *data, size_t len) { } while (len >= SHA256_BLOCK_LENGTH) { /* Process as many complete blocks as we can */ - SHA256_Transform(context, (sha2_word32*)data); + sha2_byte buffer[SHA256_BLOCK_LENGTH]; + MEMCPY_BCOPY(buffer, data, SHA256_BLOCK_LENGTH); + SHA256_Transform(context, (sha2_word32*)buffer); context->bitcount += SHA256_BLOCK_LENGTH << 3; len -= SHA256_BLOCK_LENGTH; data += SHA256_BLOCK_LENGTH; @@ -879,7 +881,9 @@ void SHA512_Update(SHA512_CTX* context, const sha2_byte *data, size_t len) { } while (len >= SHA512_BLOCK_LENGTH) { /* Process as many complete blocks as we can */ - SHA512_Transform(context, (sha2_word64*)data); + sha2_byte buffer[SHA512_BLOCK_LENGTH]; + MEMCPY_BCOPY(buffer, data, SHA512_BLOCK_LENGTH); + SHA512_Transform(context, (sha2_word64*)buffer); ADDINC128(context->bitcount, SHA512_BLOCK_LENGTH << 3); len -= SHA512_BLOCK_LENGTH; data += SHA512_BLOCK_LENGTH; -- cgit v1.2.3 From 5076b3c220bb6e9e99d79d390e919da132b67215 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 21 Apr 2012 18:26:49 +0200 Subject: * apt-pkg/contrib/strutl.cc: - remove the message size limit from ioprintf and strprintf --- apt-pkg/contrib/strutl.cc | 57 ++++++++++++++++++++++++++++++----------------- 1 file changed, 37 insertions(+), 20 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/strutl.cc b/apt-pkg/contrib/strutl.cc index 99efa8d98..ca096d736 100644 --- a/apt-pkg/contrib/strutl.cc +++ b/apt-pkg/contrib/strutl.cc @@ -23,6 +23,7 @@ #include #include +#include #include #include #include @@ -1168,34 +1169,50 @@ unsigned long RegexChoice(RxChoiceList *Rxs,const char **ListBegin, return Hits; } /*}}}*/ -// ioprintf - C format string outputter to C++ iostreams /*{{{*/ +// {str,io}printf - C format string outputter to C++ strings/iostreams /*{{{*/ // --------------------------------------------------------------------- /* This is used to make the internationalization strings easier to translate and to allow reordering of parameters */ -void ioprintf(ostream &out,const char *format,...) +static bool iovprintf(ostream &out, const char *format, + va_list &args, ssize_t &size) { + char *S = (char*)malloc(size); + ssize_t const n = vsnprintf(S, size, format, args); + if (n > -1 && n < size) { + out << S; + free(S); + return true; + } else { + if (n > -1) + size = n + 1; + else + size *= 2; + } + free(S); + return false; +} +void ioprintf(ostream &out,const char *format,...) { va_list args; - va_start(args,format); - - // sprintf the description - char S[4096]; - vsnprintf(S,sizeof(S),format,args); - out << S; + ssize_t size = 400; + while (true) { + va_start(args,format); + if (iovprintf(out, format, args, size) == true) + return; + va_end(args); + } } - /*}}}*/ -// strprintf - C format string outputter to C++ strings /*{{{*/ -// --------------------------------------------------------------------- -/* This is used to make the internationalization strings easier to translate - and to allow reordering of parameters */ -void strprintf(string &out,const char *format,...) +void strprintf(string &out,const char *format,...) { va_list args; - va_start(args,format); - - // sprintf the description - char S[4096]; - vsnprintf(S,sizeof(S),format,args); - out = string(S); + ssize_t size = 400; + std::ostringstream outstr; + while (true) { + va_start(args,format); + if (iovprintf(outstr, format, args, size) == true) + break; + va_end(args); + } + out = outstr.str(); } /*}}}*/ // safe_snprintf - Safer snprintf /*{{{*/ -- cgit v1.2.3 From 2f4162708d9db7c71590370cc998d46e8386c757 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 22 Apr 2012 12:28:10 +0200 Subject: * apt-pkg/contrib/configuration.cc: - add a more versatile Dump() method --- apt-pkg/contrib/configuration.cc | 72 +++++++++++++++++++++++++++++++++++----- apt-pkg/contrib/configuration.h | 2 ++ 2 files changed, 66 insertions(+), 8 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index 36866a35a..ff80dfaf8 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -482,24 +482,80 @@ bool Configuration::ExistsAny(const char *Name) const /* Dump the entire configuration space */ void Configuration::Dump(ostream& str) { - /* Write out all of the configuration directives by walking the + Dump(str, NULL, "%f \"%v\";\n", true); +} +void Configuration::Dump(ostream& str, char const * const root, + char const * const formatstr, bool const emptyValue) +{ + const Configuration::Item* Top = Tree(root); + if (Top == 0) + return; + const Configuration::Item* const Root = (root == NULL) ? NULL : Top; + std::vector const format = VectorizeString(formatstr, '%'); + + /* Write out all of the configuration directives by walking the configuration tree */ - const Configuration::Item *Top = Tree(0); - for (; Top != 0;) - { - str << Top->FullTag() << " \"" << Top->Value << "\";" << endl; - + do { + if (emptyValue == true || Top->Value.empty() == emptyValue) + { + std::vector::const_iterator f = format.begin(); + str << *f; + for (++f; f != format.end(); ++f) + { + if (f->empty() == true) + { + ++f; + str << '%' << *f; + continue; + } + char const type = (*f)[0]; + if (type == 'f') + str << Top->FullTag(); + else if (type == 't') + str << Top->Tag; + else if (type == 'v') + str << Top->Value; + else if (type == 'F') + str << QuoteString(Top->FullTag(), "=\"\n"); + else if (type == 'T') + str << QuoteString(Top->Tag, "=\"\n"); + else if (type == 'V') + str << QuoteString(Top->Value, "=\"\n"); + else if (type == 'n') + str << "\n"; + else if (type == 'N') + str << "\t"; + else + str << '%' << type; + str << f->c_str() + 1; + } + } + if (Top->Child != 0) { Top = Top->Child; continue; } - + while (Top != 0 && Top->Next == 0) Top = Top->Parent; if (Top != 0) Top = Top->Next; - } + + if (Root != NULL) + { + const Configuration::Item* I = Top; + while(I != 0) + { + if (I == Root) + break; + else + I = I->Parent; + } + if (I == 0) + break; + } + } while (Top != 0); } /*}}}*/ diff --git a/apt-pkg/contrib/configuration.h b/apt-pkg/contrib/configuration.h index 4c2e75041..ea94c2fe6 100644 --- a/apt-pkg/contrib/configuration.h +++ b/apt-pkg/contrib/configuration.h @@ -103,6 +103,8 @@ class Configuration inline void Dump() { Dump(std::clog); }; void Dump(std::ostream& str); + void Dump(std::ostream& str, char const * const root, + char const * const format, bool const emptyValue); Configuration(const Item *Root); Configuration(); -- cgit v1.2.3 From ec76891fa17c77f8fd28b9b6e11ef2d7547afe7e Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 23 Apr 2012 19:11:11 +0200 Subject: normalize a bit by replacing // and /./ with / in FindFile --- apt-pkg/contrib/configuration.cc | 68 ++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 30 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index ff80dfaf8..ce02f1bd2 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -171,48 +171,56 @@ string Configuration::Find(const char *Name,const char *Default) const string Configuration::FindFile(const char *Name,const char *Default) const { const Item *RootItem = Lookup("RootDir"); - std::string rootDir = (RootItem == 0) ? "" : RootItem->Value; - if(rootDir.size() > 0 && rootDir[rootDir.size() - 1] != '/') - rootDir.push_back('/'); + std::string result = (RootItem == 0) ? "" : RootItem->Value; + if(result.empty() == false && result[result.size() - 1] != '/') + result.push_back('/'); const Item *Itm = Lookup(Name); if (Itm == 0 || Itm->Value.empty() == true) { - if (Default == 0) - return rootDir; - else - return rootDir + Default; + if (Default != 0) + result.append(Default); } - - string val = Itm->Value; - while (Itm->Parent != 0) + else { - if (Itm->Parent->Value.empty() == true) + string val = Itm->Value; + while (Itm->Parent != 0) { - Itm = Itm->Parent; - continue; - } + if (Itm->Parent->Value.empty() == true) + { + Itm = Itm->Parent; + continue; + } - // Absolute - if (val.length() >= 1 && val[0] == '/') - break; + // Absolute + if (val.length() >= 1 && val[0] == '/') + break; - // ~/foo or ./foo - if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/') - break; - - // ../foo - if (val.length() >= 3 && val[0] == '.' && val[1] == '.' && val[2] == '/') - break; - - if (Itm->Parent->Value.end()[-1] != '/') - val.insert(0, "/"); + // ~/foo or ./foo + if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/') + break; + + // ../foo + if (val.length() >= 3 && val[0] == '.' && val[1] == '.' && val[2] == '/') + break; + + if (Itm->Parent->Value.end()[-1] != '/') + val.insert(0, "/"); - val.insert(0, Itm->Parent->Value); - Itm = Itm->Parent; + val.insert(0, Itm->Parent->Value); + Itm = Itm->Parent; + } + result.append(val); } - return rootDir + val; + // do some normalisation by removing // and /./ from the path + size_t found = string::npos; + while ((found = result.find("/./")) != string::npos) + result.replace(found, 3, "/"); + while ((found = result.find("//")) != string::npos) + result.replace(found, 2, "/"); + + return result; } /*}}}*/ // Configuration::FindDir - Find a directory name /*{{{*/ -- cgit v1.2.3 From af13d1437cbcb383de89f126b316c02587e4b691 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 23 Apr 2012 19:33:32 +0200 Subject: /dev/null is a special absolute path as it has no subdirectories --- apt-pkg/contrib/configuration.cc | 9 +++++++++ 1 file changed, 9 insertions(+) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc index ce02f1bd2..4de17e3e1 100644 --- a/apt-pkg/contrib/configuration.cc +++ b/apt-pkg/contrib/configuration.cc @@ -194,7 +194,11 @@ string Configuration::FindFile(const char *Name,const char *Default) const // Absolute if (val.length() >= 1 && val[0] == '/') + { + if (val.compare(0, 9, "/dev/null") == 0) + val.erase(9); break; + } // ~/foo or ./foo if (val.length() >= 2 && (val[0] == '~' || val[0] == '.') && val[1] == '/') @@ -230,7 +234,12 @@ string Configuration::FindDir(const char *Name,const char *Default) const { string Res = FindFile(Name,Default); if (Res.end()[-1] != '/') + { + size_t const found = Res.rfind("/dev/null"); + if (found != string::npos && found == Res.size() - 9) + return Res; // /dev/null returning return Res + '/'; + } return Res; } /*}}}*/ -- cgit v1.2.3 From afb093cd9bc9d0b2abb6a87d1b98785183a6452d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 9 May 2012 19:16:39 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - check that the fd which are closed are valid --- apt-pkg/contrib/fileutl.cc | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index e9d1ba1ce..1cf225329 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1065,7 +1065,10 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C // We don't need the file open - instead let the compressor open it // as he properly knows better how to efficiently read from 'his' file if (FileName.empty() == false) + { close(iFd); + iFd = -1; + } } // Create a data pipe @@ -1133,7 +1136,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C close(Pipe[0]); else close(Pipe[1]); - if (Comp == true || FileName.empty() == true) + if ((Comp == true || FileName.empty() == true) && d->compressed_fd != -1) close(d->compressed_fd); return true; @@ -1355,8 +1358,9 @@ bool FileFd::Seek(unsigned long long To) if (d->bz2 != NULL) BZ2_bzclose(d->bz2); #endif - close(iFd); - iFd = 0; + if (iFd != -1) + close(iFd); + iFd = -1; if (TemporaryFileName.empty() == false) iFd = open(TemporaryFileName.c_str(), O_RDONLY); else if (FileName.empty() == false) -- cgit v1.2.3 From ba667cf74b94846c0696ae7a27e53b28492554b6 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 9 May 2012 22:05:18 +0200 Subject: ensure that we do init d only once and especially not with its own content as this causes some "interesting" hickups resulting in segfaults as it seems (Closes: #554387, #670979) --- apt-pkg/contrib/fileutl.cc | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 1cf225329..14c0b561f 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -905,8 +905,6 @@ bool FileFd::Open(string FileName,unsigned int const Mode,CompressMode Compress, bool FileFd::Open(string FileName,unsigned int const Mode,APT::Configuration::Compressor const &compressor, unsigned long const Perms) { Close(); - d = new FileFdPrivate; - d->openmode = Mode; Flags = AutoClose; if ((Mode & WriteOnly) != WriteOnly && (Mode & (Atomic | Create | Empty | Exclusive)) != 0) @@ -1000,8 +998,6 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, CompressMode Compre bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration::Compressor const &compressor, bool AutoClose) { Close(); - d = new FileFdPrivate; - d->openmode = Mode; Flags = (AutoClose) ? FileFd::AutoClose : 0; iFd = Fd; this->FileName = ""; @@ -1015,7 +1011,12 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration: } bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor) { - d->compressor = compressor; + if (d == NULL) + { + d = new FileFdPrivate(); + d->openmode = Mode; + d->compressor = compressor; + } if (compressor.Name == "." || compressor.Binary.empty() == true) return true; #ifdef HAVE_ZLIB -- cgit v1.2.3 From adbd7eb4db58ce38965bf6340b8b059a07fdb5bc Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 10 May 2012 00:14:50 +0200 Subject: collect zombie (de)compressor processes on reopen --- apt-pkg/contrib/fileutl.cc | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 14c0b561f..4dc087983 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1022,6 +1022,11 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C #ifdef HAVE_ZLIB else if (compressor.Name == "gzip") { + if (d->gz != NULL) + { + gzclose(d->gz); + d->gz = NULL; + } if ((Mode & ReadWrite) == ReadWrite) d->gz = gzdopen(iFd, "r+"); else if ((Mode & WriteOnly) == WriteOnly) @@ -1037,6 +1042,11 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C #ifdef HAVE_BZ2 else if (compressor.Name == "bzip2") { + if (d->bz2 != NULL) + { + BZ2_bzclose(d->bz2); + d->bz2 = NULL; + } if ((Mode & ReadWrite) == ReadWrite) d->bz2 = BZ2_bzdopen(iFd, "r+"); else if ((Mode & WriteOnly) == WriteOnly) @@ -1050,14 +1060,17 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C } #endif + // collect zombies here in case we reopen + if (d->compressor_pid > 0) + ExecWait(d->compressor_pid, "FileFdCompressor", true); if ((Mode & ReadWrite) == ReadWrite) return _error->Error("ReadWrite mode is not supported for file %s", FileName.c_str()); bool const Comp = (Mode & WriteOnly) == WriteOnly; - // Handle 'decompression' of empty files if (Comp == false) { + // Handle 'decompression' of empty files struct stat Buf; fstat(iFd, &Buf); if (Buf.st_size == 0 && S_ISFIFO(Buf.st_mode) == false) -- cgit v1.2.3 From fbb89d946da063ec6e1c4b0034cb479c10a37375 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 10 May 2012 01:54:21 +0200 Subject: ensure that in error conditions the Fail flag is set --- apt-pkg/contrib/fileutl.cc | 49 +++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 4dc087983..7a24b6bb0 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1065,7 +1065,10 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C ExecWait(d->compressor_pid, "FileFdCompressor", true); if ((Mode & ReadWrite) == ReadWrite) + { + Flags |= Fail; return _error->Error("ReadWrite mode is not supported for file %s", FileName.c_str()); + } bool const Comp = (Mode & WriteOnly) == WriteOnly; if (Comp == false) @@ -1088,7 +1091,10 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C // Create a data pipe int Pipe[2] = {-1,-1}; if (pipe(Pipe) != 0) + { + Flags |= Fail; return _error->Errno("pipe",_("Failed to create subprocess IPC")); + } for (int J = 0; J != 2; J++) SetCloseExec(Pipe[J],true); @@ -1367,7 +1373,10 @@ bool FileFd::Seek(unsigned long long To) return Skip(To - seekpos); if ((d->openmode & ReadOnly) != ReadOnly) + { + Flags |= Fail; return _error->Error("Reopen is only implemented for read-only files!"); + } #ifdef HAVE_BZ2 if (d->bz2 != NULL) BZ2_bzclose(d->bz2); @@ -1385,11 +1394,17 @@ bool FileFd::Seek(unsigned long long To) if (lseek(d->compressed_fd, 0, SEEK_SET) != 0) iFd = d->compressed_fd; if (iFd <= 0) + { + Flags |= Fail; return _error->Error("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!"); + } } if (OpenInternDescriptor(d->openmode, d->compressor) == false) + { + Flags |= Fail; return _error->Error("Seek on file %s because it couldn't be reopened", FileName.c_str()); + } if (To != 0) return Skip(To); @@ -1431,7 +1446,10 @@ bool FileFd::Skip(unsigned long long Over) { unsigned long long toread = std::min((unsigned long long) sizeof(buffer), Over); if (Read(buffer, toread) == false) + { + Flags |= Fail; return _error->Error("Unable to seek ahead %llu",Over); + } Over -= toread; } return true; @@ -1499,7 +1517,10 @@ unsigned long long FileFd::Tell() #endif Res = lseek(iFd,0,SEEK_CUR); if (Res == (off_t)-1) + { + Flags |= Fail; _error->Errno("lseek","Failed to determine the current file position"); + } d->seekpos = Res; return Res; } @@ -1511,7 +1532,10 @@ unsigned long long FileFd::FileSize() { struct stat Buf; if (d->pipe == false && fstat(iFd,&Buf) != 0) + { + Flags |= Fail; return _error->Errno("fstat","Unable to determine the file size"); + } // for compressor pipes st_size is undefined and at 'best' zero if (d->pipe == true || S_ISFIFO(Buf.st_mode)) @@ -1520,7 +1544,10 @@ unsigned long long FileFd::FileSize() // in theory the Open-methods should take care of it already d->pipe = true; if (stat(FileName.c_str(), &Buf) != 0) + { + Flags |= Fail; return _error->Errno("stat","Unable to determine the file size"); + } } return Buf.st_size; @@ -1562,10 +1589,16 @@ unsigned long long FileFd::Size() * bits of the file */ // FIXME: Size for gz-files is limited by 32bit… no largefile support if (lseek(iFd, -4, SEEK_END) < 0) - return _error->Errno("lseek","Unable to seek to end of gzipped file"); + { + Flags |= Fail; + return _error->Errno("lseek","Unable to seek to end of gzipped file"); + } size = 0L; if (read(iFd, &size, 4) != 4) - return _error->Errno("read","Unable to read original size of gzipped file"); + { + Flags |= Fail; + return _error->Errno("read","Unable to read original size of gzipped file"); + } #ifdef WORDS_BIGENDIAN uint32_t tmp_size = size; @@ -1575,7 +1608,10 @@ unsigned long long FileFd::Size() #endif if (lseek(iFd, oldPos, SEEK_SET) < 0) - return _error->Errno("lseek","Unable to seek in gzipped file"); + { + Flags |= Fail; + return _error->Errno("lseek","Unable to seek in gzipped file"); + } return size; } @@ -1592,6 +1628,7 @@ time_t FileFd::ModificationTime() struct stat Buf; if (d->pipe == false && fstat(iFd,&Buf) != 0) { + Flags |= Fail; _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); return 0; } @@ -1604,6 +1641,7 @@ time_t FileFd::ModificationTime() d->pipe = true; if (stat(FileName.c_str(), &Buf) != 0) { + Flags |= Fail; _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); return 0; } @@ -1663,6 +1701,8 @@ bool FileFd::Close() d = NULL; } + if (Res == false) + Flags |= Fail; return Res; } /*}}}*/ @@ -1673,7 +1713,10 @@ bool FileFd::Sync() { #ifdef _POSIX_SYNCHRONIZED_IO if (fsync(iFd) != 0) + { + Flags |= Fail; return _error->Errno("sync",_("Problem syncing the file")); + } #endif return true; } -- cgit v1.2.3 From ff477ee133fb448622cc86f658f1a9b23577e413 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Thu, 10 May 2012 03:09:18 +0200 Subject: ensure that d is set before accessing it --- apt-pkg/contrib/fileutl.cc | 91 +++++++++++++++++++++++++--------------------- 1 file changed, 50 insertions(+), 41 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 7a24b6bb0..c8e685a5a 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1011,16 +1011,18 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration: } bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor) { + if (compressor.Name == "." || compressor.Binary.empty() == true) + return true; + if (d == NULL) { d = new FileFdPrivate(); d->openmode = Mode; d->compressor = compressor; } - if (compressor.Name == "." || compressor.Binary.empty() == true) - return true; + #ifdef HAVE_ZLIB - else if (compressor.Name == "gzip") + if (compressor.Name == "gzip") { if (d->gz != NULL) { @@ -1040,7 +1042,7 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C } #endif #ifdef HAVE_BZ2 - else if (compressor.Name == "bzip2") + if (compressor.Name == "bzip2") { if (d->bz2 != NULL) { @@ -1185,12 +1187,12 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) do { #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) Res = gzread(d->gz,To,Size); else #endif #ifdef HAVE_BZ2 - if (d->bz2 != NULL) + if (d != NULL && d->bz2 != NULL) Res = BZ2_bzread(d->bz2,To,Size); else #endif @@ -1202,7 +1204,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) continue; Flags |= Fail; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) { int err; char const * const errmsg = gzerror(d->gz, &err); @@ -1211,7 +1213,7 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) } #endif #ifdef HAVE_BZ2 - if (d->bz2 != NULL) + if (d != NULL && d->bz2 != NULL) { int err; char const * const errmsg = BZ2_bzerror(d->bz2, &err); @@ -1224,7 +1226,8 @@ bool FileFd::Read(void *To,unsigned long long Size,unsigned long long *Actual) To = (char *)To + Res; Size -= Res; - d->seekpos += Res; + if (d != NULL) + d->seekpos += Res; if (Actual != 0) *Actual += Res; } @@ -1252,7 +1255,7 @@ char* FileFd::ReadLine(char *To, unsigned long long const Size) { *To = '\0'; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) return gzgets(d->gz, To, Size); #endif @@ -1283,12 +1286,12 @@ bool FileFd::Write(const void *From,unsigned long long Size) do { #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) Res = gzwrite(d->gz,From,Size); else #endif #ifdef HAVE_BZ2 - if (d->bz2 != NULL) + if (d != NULL && d->bz2 != NULL) Res = BZ2_bzwrite(d->bz2,(void*)From,Size); else #endif @@ -1299,7 +1302,7 @@ bool FileFd::Write(const void *From,unsigned long long Size) { Flags |= Fail; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) { int err; char const * const errmsg = gzerror(d->gz, &err); @@ -1308,7 +1311,7 @@ bool FileFd::Write(const void *From,unsigned long long Size) } #endif #ifdef HAVE_BZ2 - if (d->bz2 != NULL) + if (d != NULL && d->bz2 != NULL) { int err; char const * const errmsg = BZ2_bzerror(d->bz2, &err); @@ -1321,7 +1324,8 @@ bool FileFd::Write(const void *From,unsigned long long Size) From = (char *)From + Res; Size -= Res; - d->seekpos += Res; + if (d != NULL) + d->seekpos += Res; } while (Res > 0 && Size > 0); @@ -1359,11 +1363,11 @@ bool FileFd::Write(int Fd, const void *From, unsigned long long Size) /* */ bool FileFd::Seek(unsigned long long To) { - if (d->pipe == true + if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 - || d->bz2 != NULL + || d->bz2 != NULL #endif - ) + )) { // Our poor man seeking in pipes is costly, so try to avoid it unsigned long long seekpos = Tell(); @@ -1414,7 +1418,7 @@ bool FileFd::Seek(unsigned long long To) } int res; #ifdef HAVE_ZLIB - if (d->gz) + if (d != NULL && d->gz) res = gzseek(d->gz,To,SEEK_SET); else #endif @@ -1425,7 +1429,8 @@ bool FileFd::Seek(unsigned long long To) return _error->Error("Unable to seek to %llu", To); } - d->seekpos = To; + if (d != NULL) + d->seekpos = To; return true; } /*}}}*/ @@ -1434,11 +1439,11 @@ bool FileFd::Seek(unsigned long long To) /* */ bool FileFd::Skip(unsigned long long Over) { - if (d->pipe == true + if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 - || d->bz2 != NULL + || d->bz2 != NULL #endif - ) + )) { d->seekpos += Over; char buffer[1024]; @@ -1457,7 +1462,7 @@ bool FileFd::Skip(unsigned long long Over) int res; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) res = gzseek(d->gz,Over,SEEK_CUR); else #endif @@ -1467,7 +1472,8 @@ bool FileFd::Skip(unsigned long long Over) Flags |= Fail; return _error->Error("Unable to seek ahead %llu",Over); } - d->seekpos = res; + if (d != NULL) + d->seekpos = res; return true; } @@ -1478,7 +1484,7 @@ bool FileFd::Skip(unsigned long long Over) bool FileFd::Truncate(unsigned long long To) { #if defined HAVE_ZLIB || defined HAVE_BZ2 - if (d->gz != NULL || d->bz2 != NULL) + if (d != NULL && (d->gz != NULL || d->bz2 != NULL)) { Flags |= Fail; return _error->Error("Truncating compressed files is not implemented (%s)", FileName.c_str()); @@ -1502,16 +1508,16 @@ unsigned long long FileFd::Tell() // seeking around, but not all users of FileFd use always Seek() and co // so d->seekpos isn't always true and we can just use it as a hint if // we have nothing else, but not always as an authority… - if (d->pipe == true + if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 - || d->bz2 != NULL + || d->bz2 != NULL #endif - ) + )) return d->seekpos; off_t Res; #ifdef HAVE_ZLIB - if (d->gz != NULL) + if (d != NULL && d->gz != NULL) Res = gztell(d->gz); else #endif @@ -1521,7 +1527,8 @@ unsigned long long FileFd::Tell() Flags |= Fail; _error->Errno("lseek","Failed to determine the current file position"); } - d->seekpos = Res; + if (d != NULL) + d->seekpos = Res; return Res; } /*}}}*/ @@ -1531,18 +1538,19 @@ unsigned long long FileFd::Tell() unsigned long long FileFd::FileSize() { struct stat Buf; - if (d->pipe == false && fstat(iFd,&Buf) != 0) + if ((d == NULL || d->pipe == false) && fstat(iFd,&Buf) != 0) { Flags |= Fail; return _error->Errno("fstat","Unable to determine the file size"); } // for compressor pipes st_size is undefined and at 'best' zero - if (d->pipe == true || S_ISFIFO(Buf.st_mode)) + if ((d != NULL && d->pipe == true) || S_ISFIFO(Buf.st_mode)) { // we set it here, too, as we get the info here for free // in theory the Open-methods should take care of it already - d->pipe = true; + if (d != NULL) + d->pipe = true; if (stat(FileName.c_str(), &Buf) != 0) { Flags |= Fail; @@ -1562,11 +1570,11 @@ unsigned long long FileFd::Size() // for compressor pipes st_size is undefined and at 'best' zero, // so we 'read' the content and 'seek' back - see there - if (d->pipe == true + if (d != NULL && (d->pipe == true #ifdef HAVE_BZ2 - || (d->bz2 && size > 0) + || (d->bz2 && size > 0) #endif - ) + )) { unsigned long long const oldSeek = Tell(); char ignore[1000]; @@ -1581,7 +1589,7 @@ unsigned long long FileFd::Size() // only check gzsize if we are actually a gzip file, just checking for // "gz" is not sufficient as uncompressed files could be opened with // gzopen in "direct" mode as well - else if (d->gz && !gzdirect(d->gz) && size > 0) + else if (d != NULL && d->gz && !gzdirect(d->gz) && size > 0) { off_t const oldPos = lseek(iFd,0,SEEK_CUR); /* unfortunately zlib.h doesn't provide a gzsize(), so we have to do @@ -1626,7 +1634,7 @@ unsigned long long FileFd::Size() time_t FileFd::ModificationTime() { struct stat Buf; - if (d->pipe == false && fstat(iFd,&Buf) != 0) + if ((d == NULL || d->pipe == false) && fstat(iFd,&Buf) != 0) { Flags |= Fail; _error->Errno("fstat","Unable to determine the modification time of file %s", FileName.c_str()); @@ -1634,11 +1642,12 @@ time_t FileFd::ModificationTime() } // for compressor pipes st_size is undefined and at 'best' zero - if (d->pipe == true || S_ISFIFO(Buf.st_mode)) + if ((d != NULL && d->pipe == true) || S_ISFIFO(Buf.st_mode)) { // we set it here, too, as we get the info here for free // in theory the Open-methods should take care of it already - d->pipe = true; + if (d != NULL) + d->pipe = true; if (stat(FileName.c_str(), &Buf) != 0) { Flags |= Fail; -- cgit v1.2.3 From 500400fedd6e57eb0162a0e41fdd9ba05a2cae3d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 16 May 2012 15:55:32 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - ensure that we close compressed fds, wait for forks and such even if the FileFd itself is set to not autoclose the given Fd --- apt-pkg/contrib/fileutl.cc | 65 +++++++++++++++++++++++++++++----------------- 1 file changed, 41 insertions(+), 24 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index c8e685a5a..c7f78cdfb 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -81,6 +81,31 @@ class FileFdPrivate { FileFdPrivate() : gz(NULL), bz2(NULL), compressed_fd(-1), compressor_pid(-1), pipe(false), openmode(0), seekpos(0) {}; + bool CloseDown(std::string const &FileName) + { + bool Res = true; +#ifdef HAVE_ZLIB + if (gz != NULL) { + int const e = gzclose(gz); + gz = NULL; + // gzdclose() on empty files always fails with "buffer error" here, ignore that + if (e != 0 && e != Z_BUF_ERROR) + Res &= _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str()); + } +#endif +#ifdef HAVE_BZ2 + if (bz2 != NULL) { + BZ2_bzclose(bz2); + bz2 = NULL; + } +#endif + if (compressor_pid > 0) + ExecWait(compressor_pid, "FileFdCompressor", true); + compressor_pid = -1; + + return Res; + } + ~FileFdPrivate() { CloseDown(""); } }; // RunScripts - Run a set of scripts from a configuration subtree /*{{{*/ @@ -1171,6 +1196,12 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C FileFd::~FileFd() { Close(); + if (d != NULL) + { + d->CloseDown(FileName); + delete d; + d = NULL; + } } /*}}}*/ // FileFd::Read - Read a bit of the file /*{{{*/ @@ -1397,7 +1428,7 @@ bool FileFd::Seek(unsigned long long To) if (d->compressed_fd > 0) if (lseek(d->compressed_fd, 0, SEEK_SET) != 0) iFd = d->compressed_fd; - if (iFd <= 0) + if (iFd < 0) { Flags |= Fail; return _error->Error("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!"); @@ -1670,21 +1701,15 @@ bool FileFd::Close() bool Res = true; if ((Flags & AutoClose) == AutoClose) { -#ifdef HAVE_ZLIB - if (d != NULL && d->gz != NULL) { - int const e = gzclose(d->gz); - // gzdclose() on empty files always fails with "buffer error" here, ignore that - if (e != 0 && e != Z_BUF_ERROR) - Res &= _error->Errno("close",_("Problem closing the gzip file %s"), FileName.c_str()); - } else -#endif -#ifdef HAVE_BZ2 - if (d != NULL && d->bz2 != NULL) - BZ2_bzclose(d->bz2); - else -#endif - if (iFd > 0 && close(iFd) != 0) - Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str()); + if ((Flags & Compressed) != Compressed && iFd > 0 && close(iFd) != 0) + Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str()); + + if (d != NULL) + { + Res &= d->CloseDown(FileName); + delete d; + d = NULL; + } } if ((Flags & Replace) == Replace && iFd >= 0) { @@ -1702,14 +1727,6 @@ bool FileFd::Close() if (unlink(FileName.c_str()) != 0) Res &= _error->WarningE("unlnk",_("Problem unlinking the file %s"), FileName.c_str()); - if (d != NULL) - { - if (d->compressor_pid > 0) - ExecWait(d->compressor_pid, "FileFdCompressor", true); - delete d; - d = NULL; - } - if (Res == false) Flags |= Fail; return Res; -- cgit v1.2.3 From 6bae2c5108a95267fedcc8f2312e91488ebece8d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Mon, 21 May 2012 19:12:25 +0200 Subject: =?UTF-8?q?*=20apt-pkg/contrib/mmap.cc:=20=20=20-=20have=20a=20dum?= =?UTF-8?q?my=20SyncToFd=20around=20in=20case=20of=20ReadOnly=20access=20t?= =?UTF-8?q?o=20a=20=20=20=20=20compressed=20file=20as=20we=20otherwise=20o?= =?UTF-8?q?n=20Close()=20do=20not=20delete[]=20the=20=20=20=20=20char=20bu?= =?UTF-8?q?ffer=20but=20munmap()=20it=E2=80=A6=20(Closes:=20#673815)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apt-pkg/contrib/mmap.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 160718ea5..2d12b6fe9 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -84,6 +84,7 @@ bool MMap::Map(FileFd &Fd) if ((Flags & ReadOnly) != ReadOnly) return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str()); Base = new unsigned char[iSize]; + SyncToFd = new FileFd(); if (Fd.Seek(0L) == false || Fd.Read(Base, iSize) == false) return _error->Error("Compressed file %s can't be read into mmap", Fd.Name().c_str()); return true; -- cgit v1.2.3 From d3aac32ece65fd5424a94402f46db065298c07a8 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 22 May 2012 18:33:43 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - dup() given compressed fd in OpenDescriptor if AutoClose is disabled as otherwise gzclose() and co will close it --- apt-pkg/contrib/fileutl.cc | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index c7f78cdfb..119cd1974 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1024,7 +1024,20 @@ bool FileFd::OpenDescriptor(int Fd, unsigned int const Mode, APT::Configuration: { Close(); Flags = (AutoClose) ? FileFd::AutoClose : 0; - iFd = Fd; + if (AutoClose == false && ( +#ifdef HAVE_ZLIB + compressor.Name == "gzip" || +#endif +#ifdef HAVE_BZ2 + compressor.Name == "bzip2" || +#endif + false)) + { + // Need to duplicate fd here or gzclose for cleanup will close the fd as well + iFd = dup(Fd); + } + else + iFd = Fd; this->FileName = ""; if (OpenInternDescriptor(Mode, compressor) == false) { @@ -1183,8 +1196,6 @@ bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::C close(Pipe[0]); else close(Pipe[1]); - if ((Comp == true || FileName.empty() == true) && d->compressed_fd != -1) - close(d->compressed_fd); return true; } @@ -1712,7 +1723,7 @@ bool FileFd::Close() } } - if ((Flags & Replace) == Replace && iFd >= 0) { + if ((Flags & Replace) == Replace) { if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str()); -- cgit v1.2.3 From ae2be086c6996e6ed02b7d828fdcac38248a964d Mon Sep 17 00:00:00 2001 From: Daniel Hartwig Date: Sat, 9 Jun 2012 22:49:37 +0200 Subject: * apt-pkg/contrib/cmdline.cc: - apply patch from Daniel Hartwig to fix a segfault in case the LongOpt is empty (Closes: #676331) --- apt-pkg/contrib/cmndline.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/cmndline.cc b/apt-pkg/contrib/cmndline.cc index 159f330a1..b8c7f7984 100644 --- a/apt-pkg/contrib/cmndline.cc +++ b/apt-pkg/contrib/cmndline.cc @@ -92,8 +92,9 @@ bool CommandLine::Parse(int argc,const char **argv) // Match up to a = against the list Args *A; const char *OptEnd = strchrnul(Opt, '='); - for (A = ArgList; A->end() == false && - stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); + for (A = ArgList; A->end() == false && + (A->LongOpt == 0 || stringcasecmp(Opt,OptEnd,A->LongOpt) != 0); + ++A); // Failed, look for a word after the first - (no-foo) bool PreceedMatch = false; -- cgit v1.2.3 From 7a6d9076595b2acc164d6f057f609c96c359ea08 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 9 Jun 2012 22:55:51 +0200 Subject: fix segfault with empty LongOpt in --no-* branch --- apt-pkg/contrib/cmndline.cc | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/cmndline.cc b/apt-pkg/contrib/cmndline.cc index b8c7f7984..75d02cad4 100644 --- a/apt-pkg/contrib/cmndline.cc +++ b/apt-pkg/contrib/cmndline.cc @@ -106,7 +106,8 @@ bool CommandLine::Parse(int argc,const char **argv) Opt++; for (A = ArgList; A->end() == false && - stringcasecmp(Opt,OptEnd,A->LongOpt) != 0; A++); + (A->LongOpt == 0 || stringcasecmp(Opt,OptEnd,A->LongOpt) != 0); + ++A); // Failed again.. if (A->end() == true && OptEnd - Opt != 1) -- cgit v1.2.3 From 7899a1339ced1e8fa10730e5619349b9fd782fac Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 16 Jun 2012 14:35:15 +0200 Subject: * apt-pkg/contrib/mmap.cc: - Fix the Fallback option to work correctly, by not calling realloc() on a map mapped by mmap(), and by using malloc and friends instead of new[]. --- apt-pkg/contrib/mmap.cc | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 2d12b6fe9..131cefd6e 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -83,7 +83,7 @@ bool MMap::Map(FileFd &Fd) { if ((Flags & ReadOnly) != ReadOnly) return _error->Error("Compressed file %s can only be mapped readonly", Fd.Name().c_str()); - Base = new unsigned char[iSize]; + Base = malloc(iSize); SyncToFd = new FileFd(); if (Fd.Seek(0L) == false || Fd.Read(Base, iSize) == false) return _error->Error("Compressed file %s can't be read into mmap", Fd.Name().c_str()); @@ -91,17 +91,17 @@ bool MMap::Map(FileFd &Fd) } // Map it. - Base = mmap(0,iSize,Prot,Map,Fd.Fd(),0); + Base = (Flags & Fallback) ? MAP_FAILED : mmap(0,iSize,Prot,Map,Fd.Fd(),0); if (Base == (void *)-1) { - if (errno == ENODEV || errno == EINVAL) + if (errno == ENODEV || errno == EINVAL || (Flags & Fallback)) { // The filesystem doesn't support this particular kind of mmap. // So we allocate a buffer and read the whole file into it. if ((Flags & ReadOnly) == ReadOnly) { // for readonly, we don't need sync, so make it simple - Base = new unsigned char[iSize]; + Base = malloc(iSize); return Fd.Read(Base, iSize); } // FIXME: Writing to compressed fd's ? @@ -109,7 +109,7 @@ bool MMap::Map(FileFd &Fd) if (dupped_fd == -1) return _error->Errno("mmap", _("Couldn't duplicate file descriptor %i"), Fd.Fd()); - Base = new unsigned char[iSize]; + Base = calloc(iSize, 1); SyncToFd = new FileFd (dupped_fd); if (!SyncToFd->Seek(0L) || !SyncToFd->Read(Base, iSize)) return false; @@ -135,7 +135,7 @@ bool MMap::Close(bool DoSync) if (SyncToFd != NULL) { - delete[] (char *)Base; + free(Base); delete SyncToFd; SyncToFd = NULL; } @@ -283,8 +283,7 @@ DynamicMMap::DynamicMMap(unsigned long Flags,unsigned long const &WorkSpace, } #endif // fallback to a static allocated space - Base = new unsigned char[WorkSpace]; - memset(Base,0,WorkSpace); + Base = calloc(WorkSpace, 1); iSize = 0; } /*}}}*/ @@ -300,7 +299,7 @@ DynamicMMap::~DynamicMMap() #ifdef _POSIX_MAPPED_FILES munmap(Base, WorkSpace); #else - delete [] (unsigned char *)Base; + free(Base); #endif return; } -- cgit v1.2.3 From 61a406f0fc9c7b1807acb2ff146db6acd18e879c Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 16 Jun 2012 14:38:09 +0200 Subject: Zero out the new memory allocated with realloc(). --- apt-pkg/contrib/mmap.cc | 2 ++ 1 file changed, 2 insertions(+) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 131cefd6e..77b4502f5 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -461,6 +461,8 @@ bool DynamicMMap::Grow() { return false; Base = realloc(Base, newSize); + /* Set new memory to 0 */ + memset((char*)Base + WorkSpace, 0, newSize - WorkSpace); if (Base == NULL) return false; } -- cgit v1.2.3 From e3026ce49a0839dfbe494af7e60d91bc7204f88e Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Sat, 16 Jun 2012 15:23:06 +0200 Subject: Fix previous commit: Do not call memset() if realloc() returned NULL. --- apt-pkg/contrib/mmap.cc | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 77b4502f5..593bb063b 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -461,10 +461,11 @@ bool DynamicMMap::Grow() { return false; Base = realloc(Base, newSize); - /* Set new memory to 0 */ - memset((char*)Base + WorkSpace, 0, newSize - WorkSpace); if (Base == NULL) return false; + else + /* Set new memory to 0 */ + memset((char*)Base + WorkSpace, 0, newSize - WorkSpace); } Pools =(Pool*) Base + poolOffset; -- cgit v1.2.3 From 4bd60a02b45241039d4ca7b5cfaa005e552f3d0d Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 20 Jul 2012 13:12:25 +0200 Subject: * apt-pkg/contrib/mmap.cc: - refer to APT::Cache-Start in case the growing failed as if -Limit is really the offender it will be noted in a previous error message. --- apt-pkg/contrib/mmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 593bb063b..f0ab49265 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -328,7 +328,7 @@ unsigned long DynamicMMap::RawAllocate(unsigned long long Size,unsigned long Aln if(!Grow()) { _error->Fatal(_("Dynamic MMap ran out of room. Please increase the size " - "of APT::Cache-Limit. Current value: %lu. (man 5 apt.conf)"), WorkSpace); + "of APT::Cache-Start. Current value: %lu. (man 5 apt.conf)"), WorkSpace); return 0; } } -- cgit v1.2.3 From 7b15b702b1f908595a2ae484117746587f8e03aa Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 24 Jul 2012 16:19:56 +0200 Subject: trigger the usage of the fallback code for kfreebsd also in the second (filebased) constructor of DynamicMMap (Closes: #677704) --- apt-pkg/contrib/mmap.cc | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index f0ab49265..aaa9da44f 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -216,7 +216,17 @@ DynamicMMap::DynamicMMap(FileFd &F,unsigned long Flags,unsigned long const &Work { if (_error->PendingError() == true) return; - + + // disable Moveable if we don't grow + if (Grow == 0) + this->Flags &= ~Moveable; + +#ifndef __linux__ + // kfreebsd doesn't have mremap, so we use the fallback + if ((this->Flags & Moveable) == Moveable) + this->Flags |= Fallback; +#endif + unsigned long long EndOfFile = Fd->Size(); if (EndOfFile > WorkSpace) WorkSpace = EndOfFile; -- cgit v1.2.3 From fbda0ee9d9f5ecd34d38f0e416837e8ef3a10f68 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Tue, 24 Jul 2012 17:22:25 +0200 Subject: for filesystems not supporting mmap'ing a file we need to use a SyncToFd dummy just as we did for compressed files in 0.9.5 --- apt-pkg/contrib/mmap.cc | 1 + 1 file changed, 1 insertion(+) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index aaa9da44f..3e6ef3520 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -102,6 +102,7 @@ bool MMap::Map(FileFd &Fd) { // for readonly, we don't need sync, so make it simple Base = malloc(iSize); + SyncToFd = new FileFd(); return Fd.Read(Base, iSize); } // FIXME: Writing to compressed fd's ? -- cgit v1.2.3 From 3ac981df108470b42b89d0d4a816a5e3ba58c501 Mon Sep 17 00:00:00 2001 From: Pino Toscano Date: Sat, 4 Aug 2012 10:20:30 +0200 Subject: apt-pkg/contrib/mmap.cc: - guard only the msync call with _POSIX_SYNCHRONIZED_IO rather than also the fallback code as it breaks APT on hurd since 0.9.7.3 as the fallback is now always used on non-linux (Closes: #683354) --- apt-pkg/contrib/mmap.cc | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index 3e6ef3520..df7cd2a59 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -156,11 +156,10 @@ bool MMap::Close(bool DoSync) /* This is done in syncronous mode - the docs indicate that this will not return till all IO is complete */ bool MMap::Sync() -{ +{ if ((Flags & UnMapped) == UnMapped) return true; - -#ifdef _POSIX_SYNCHRONIZED_IO + if ((Flags & ReadOnly) != ReadOnly) { if (SyncToFd != NULL) @@ -170,11 +169,12 @@ bool MMap::Sync() } else { +#ifdef _POSIX_SYNCHRONIZED_IO if (msync((char *)Base, iSize, MS_SYNC) < 0) return _error->Errno("msync", _("Unable to synchronize mmap")); +#endif } } -#endif return true; } /*}}}*/ @@ -185,8 +185,7 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) { if ((Flags & UnMapped) == UnMapped) return true; - -#ifdef _POSIX_SYNCHRONIZED_IO + unsigned long long PSize = sysconf(_SC_PAGESIZE); if ((Flags & ReadOnly) != ReadOnly) { @@ -198,11 +197,12 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) } else { +#ifdef _POSIX_SYNCHRONIZED_IO if (msync((char *)Base+(unsigned long long)(Start/PSize)*PSize,Stop - Start,MS_SYNC) < 0) return _error->Errno("msync", _("Unable to synchronize mmap")); +#endif } } -#endif return true; } /*}}}*/ -- cgit v1.2.3 From e3348f474f0251a4be0cb0cb7ae8a07ad746311b Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 4 Aug 2012 10:24:47 +0200 Subject: move the sysconf call behind the _POSIX_SYNCHRONIZED_IO guard there it is used for the msync call --- apt-pkg/contrib/mmap.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/mmap.cc b/apt-pkg/contrib/mmap.cc index df7cd2a59..a176da636 100644 --- a/apt-pkg/contrib/mmap.cc +++ b/apt-pkg/contrib/mmap.cc @@ -186,7 +186,6 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) if ((Flags & UnMapped) == UnMapped) return true; - unsigned long long PSize = sysconf(_SC_PAGESIZE); if ((Flags & ReadOnly) != ReadOnly) { if (SyncToFd != 0) @@ -198,6 +197,7 @@ bool MMap::Sync(unsigned long Start,unsigned long Stop) else { #ifdef _POSIX_SYNCHRONIZED_IO + unsigned long long const PSize = sysconf(_SC_PAGESIZE); if (msync((char *)Base+(unsigned long long)(Start/PSize)*PSize,Stop - Start,MS_SYNC) < 0) return _error->Errno("msync", _("Unable to synchronize mmap")); #endif -- cgit v1.2.3 From b019fbab5548a70ac4f274f0aceb8a3b04cb1025 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 4 Aug 2012 10:39:27 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - remove _POSIX_SYNCHRONIZED_IO guard in FileFd::Sync() around fsync as this guard is only needed for fdatasync and not defined on hurd --- apt-pkg/contrib/fileutl.cc | 2 -- 1 file changed, 2 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 119cd1974..90e49cbfa 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -1748,13 +1748,11 @@ bool FileFd::Close() /* */ bool FileFd::Sync() { -#ifdef _POSIX_SYNCHRONIZED_IO if (fsync(iFd) != 0) { Flags |= Fail; return _error->Errno("sync",_("Problem syncing the file")); } -#endif return true; } /*}}}*/ -- cgit v1.2.3 From b8c50bd0415bd4ea16a8f09356d882f267976d40 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 13 Oct 2012 17:25:07 +0200 Subject: * apt-pkg/contrib/netrc.cc: - remove the 64 char limit for login/password in internal usage --- apt-pkg/contrib/netrc.cc | 43 +++++++++++++++++++++++++++---------------- 1 file changed, 27 insertions(+), 16 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/netrc.cc b/apt-pkg/contrib/netrc.cc index 56e59d84b..950d21dad 100644 --- a/apt-pkg/contrib/netrc.cc +++ b/apt-pkg/contrib/netrc.cc @@ -44,11 +44,11 @@ enum { #define NETRC DOT_CHAR "netrc" /* returns -1 on failure, 0 if the host is found, 1 is the host isn't found */ -int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL) +static int parsenetrc_string (char *host, std::string &login, std::string &password, char *netrcfile = NULL) { FILE *file; int retcode = 1; - int specific_login = (login[0] != 0); + int specific_login = (login.empty() == false); char *home = NULL; bool netrc_alloc = false; @@ -88,7 +88,7 @@ int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL) while (!done && fgets(netrcbuffer, sizeof (netrcbuffer), file)) { tok = strtok_r (netrcbuffer, " \t\n", &tok_buf); while (!done && tok) { - if(login[0] && password[0]) { + if(login.empty() == false && password.empty() == false) { done = true; break; } @@ -120,13 +120,13 @@ int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL) /* we are now parsing sub-keywords concerning "our" host */ if (state_login) { if (specific_login) - state_our_login = !strcasecmp (login, tok); + state_our_login = !strcasecmp (login.c_str(), tok); else - strncpy (login, tok, LOGINSIZE - 1); + login = tok; state_login = 0; } else if (state_password) { if (state_our_login || !specific_login) - strncpy (password, tok, PASSWORDSIZE - 1); + password = tok; state_password = 0; } else if (!strcasecmp ("login", tok)) state_login = 1; @@ -152,6 +152,18 @@ int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL) return retcode; } +// for some unknown reason this method is exported so keep a compatible interface for now … +int parsenetrc (char *host, char *login, char *password, char *netrcfile = NULL) +{ + std::string login_string, password_string; + int const ret = parsenetrc_string(host, login_string, password_string, netrcfile); + if (ret < 0) + return ret; + strncpy(login, login_string.c_str(), LOGINSIZE - 1); + strncpy(password, password_string.c_str(), PASSWORDSIZE - 1); + return ret; +} + void maybe_add_auth (URI &Uri, string NetRCFile) { @@ -162,21 +174,20 @@ void maybe_add_auth (URI &Uri, string NetRCFile) { if (NetRCFile.empty () == false) { - char login[64] = ""; - char password[64] = ""; + std::string login, password; char *netrcfile = strdup(NetRCFile.c_str()); // first check for a generic host based netrc entry char *host = strdup(Uri.Host.c_str()); - if (host && parsenetrc (host, login, password, netrcfile) == 0) + if (host && parsenetrc_string(host, login, password, netrcfile) == 0) { if (_config->FindB("Debug::Acquire::netrc", false) == true) std::clog << "host: " << host << " user: " << login - << " pass-size: " << strlen(password) + << " pass-size: " << password.size() << std::endl; - Uri.User = string (login); - Uri.Password = string (password); + Uri.User = login; + Uri.Password = password; free(netrcfile); free(host); return; @@ -187,15 +198,15 @@ void maybe_add_auth (URI &Uri, string NetRCFile) // a lookup uri.startswith(host) in the netrc file parser (because // of the "/" char *hostpath = strdup(string(Uri.Host+Uri.Path).c_str()); - if (hostpath && parsenetrc (hostpath, login, password, netrcfile) == 0) + if (hostpath && parsenetrc_string(hostpath, login, password, netrcfile) == 0) { if (_config->FindB("Debug::Acquire::netrc", false) == true) std::clog << "hostpath: " << hostpath << " user: " << login - << " pass-size: " << strlen(password) + << " pass-size: " << password.size() << std::endl; - Uri.User = string (login); - Uri.Password = string (password); + Uri.User = login; + Uri.Password = password; } free(netrcfile); free(hostpath); -- cgit v1.2.3 From 7f18595b3ef9a348719969889097adb4f45d44f0 Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 13 Oct 2012 18:56:23 +0200 Subject: remove 256 char line limit by using getline() (POSIX.1-2008) --- apt-pkg/contrib/netrc.cc | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib') diff --git a/apt-pkg/contrib/netrc.cc b/apt-pkg/contrib/netrc.cc index 950d21dad..c5f9630c4 100644 --- a/apt-pkg/contrib/netrc.cc +++ b/apt-pkg/contrib/netrc.cc @@ -79,13 +79,14 @@ static int parsenetrc_string (char *host, std::string &login, std::string &passw char *tok; char *tok_buf; bool done = false; - char netrcbuffer[256]; + char *netrcbuffer = NULL; + size_t netrcbuffer_size = 0; int state = NOTHING; char state_login = 0; /* Found a login keyword */ char state_password = 0; /* Found a password keyword */ - while (!done && fgets(netrcbuffer, sizeof (netrcbuffer), file)) { + while (!done && getline(&netrcbuffer, &netrcbuffer_size, file) != -1) { tok = strtok_r (netrcbuffer, " \t\n", &tok_buf); while (!done && tok) { if(login.empty() == false && password.empty() == false) { @@ -142,8 +143,9 @@ static int parsenetrc_string (char *host, std::string &login, std::string &passw tok = strtok_r (NULL, " \t\n", &tok_buf); } /* while(tok) */ - } /* while fgets() */ + } /* while getline() */ + free(netrcbuffer); fclose(file); } -- cgit v1.2.3