From b3793d41d420a895ef5be4521a56535cc79f0d4a Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sun, 4 Apr 2010 10:34:45 +0200 Subject: remove the ABI compatible stub for GetListOfFilesInDir --- apt-pkg/contrib/fileutl.cc | 5 ----- 1 file changed, 5 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index da32983f1..75adce305 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -201,11 +201,6 @@ bool FileExists(string File) // --------------------------------------------------------------------- /* If an extension is given only files with this extension are included in the returned vector, otherwise every "normal" file is included. */ -std::vector GetListOfFilesInDir(string const &Dir, string const &Ext, - bool const &SortList) -{ - return GetListOfFilesInDir(Dir, Ext, SortList, false); -} std::vector GetListOfFilesInDir(string const &Dir, string const &Ext, bool const &SortList, bool const &AllowNoExt) { -- cgit v1.2.3 From 1cd1c398d18b78f4aa9d882a5de5385f4538e0be Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Wed, 7 Apr 2010 16:38:18 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - add a parent-guarded "mkdir -p" as CreateDirectory() * apt-pkg/acquire.{cc,h}: - add a delayed constructor with Setup() for success reporting - check for and create directories in Setup if needed instead of error out unfriendly in the Constructor (Closes: #523920, #525783) - optional handle a lock file in Setup() * cmdline/apt-get.cc: - remove the lock file handling and let Acquire take care of it instead --- apt-pkg/contrib/fileutl.cc | 52 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 75adce305..16f7ce929 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -18,6 +18,7 @@ /*}}}*/ // Include Files /*{{{*/ #include +#include #include #include #include @@ -197,6 +198,57 @@ bool FileExists(string File) return true; } /*}}}*/ +// DirectoryExists - Check if a directory exists and is really one /*{{{*/ +// --------------------------------------------------------------------- +/* */ +bool DirectoryExists(string const &Path) +{ + struct stat Buf; + if (stat(Path.c_str(),&Buf) != 0) + return false; + return ((Buf.st_mode & S_IFDIR) != 0); +} + /*}}}*/ +// CreateDirectory - poor man's mkdir -p guarded by a parent directory /*{{{*/ +// --------------------------------------------------------------------- +/* This method will create all directories needed for path in good old + mkdir -p style but refuses to do this if Parent is not a prefix of + this Path. Example: /var/cache/ and /var/cache/apt/archives are given, + so it will create apt/archives if /var/cache exists - on the other + hand if the parent is /var/lib the creation will fail as this path + is not a parent of the path to be generated. */ +bool CreateDirectory(string const &Parent, string const &Path) +{ + if (Parent.empty() == true || Path.empty() == true) + return false; + + if (DirectoryExists(Path) == true) + return true; + + if (DirectoryExists(Parent) == false) + return false; + + // we are not going to create directories "into the blue" + if (Path.find(Parent, 0) != 0) + return false; + + vector const dirs = VectorizeString(Path.substr(Parent.size()), '/'); + string progress = Parent; + for (vector::const_iterator d = dirs.begin(); d != dirs.end(); ++d) + { + if (d->empty() == true) + continue; + + progress.append("/").append(*d); + if (DirectoryExists(progress) == true) + continue; + + if (mkdir(progress.c_str(), 0755) != 0) + return false; + } + return true; +} + /*}}}*/ // GetListOfFilesInDir - returns a vector of files in the given dir /*{{{*/ // --------------------------------------------------------------------- /* If an extension is given only files with this extension are included -- cgit v1.2.3 From a3a03f5d7436c870edac9c0eb92e85e1fcaf6bca Mon Sep 17 00:00:00 2001 From: "martin@piware.de" <> Date: Wed, 9 Jun 2010 14:08:20 +0200 Subject: * apt-pkg/contrib/fileutl.{h,cc}: - Add support for transparent reading of gzipped files. - Link against zlib (in apt-pkg/makefile) and add zlib build dependency. --- apt-pkg/contrib/fileutl.cc | 47 ++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 41 insertions(+), 6 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index da32983f1..11a9e7f7b 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -11,6 +11,7 @@ Most of this source is placed in the Public Domain, do with it what you will It was originally written by Jason Gunthorpe . + FileFd gzip support added by Martin Pitt The exception is RunScripts() it is under the GPLv2 @@ -603,6 +604,13 @@ bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms) { case ReadOnly: iFd = open(FileName.c_str(),O_RDONLY); + if (iFd > 0 && FileName.compare(FileName.size()-3, 3, ".gz") == 0) { + gz = gzdopen (iFd, "r"); + if (gz == NULL) { + close (iFd); + iFd = -1; + } + } break; case WriteEmpty: @@ -658,7 +666,10 @@ bool FileFd::Read(void *To,unsigned long Size,unsigned long *Actual) do { - Res = read(iFd,To,Size); + if (gz != NULL) + Res = gzread(gz,To,Size); + else + Res = read(iFd,To,Size); if (Res < 0 && errno == EINTR) continue; if (Res < 0) @@ -697,7 +708,10 @@ bool FileFd::Write(const void *From,unsigned long Size) errno = 0; do { - Res = write(iFd,From,Size); + if (gz != NULL) + Res = gzwrite(gz,From,Size); + else + Res = write(iFd,From,Size); if (Res < 0 && errno == EINTR) continue; if (Res < 0) @@ -723,7 +737,12 @@ bool FileFd::Write(const void *From,unsigned long Size) /* */ bool FileFd::Seek(unsigned long To) { - if (lseek(iFd,To,SEEK_SET) != (signed)To) + int res; + if (gz) + res = gzseek(gz,To,SEEK_SET); + else + res = lseek(iFd,To,SEEK_SET); + if (res != (signed)To) { Flags |= Fail; return _error->Error("Unable to seek to %lu",To); @@ -737,7 +756,12 @@ bool FileFd::Seek(unsigned long To) /* */ bool FileFd::Skip(unsigned long Over) { - if (lseek(iFd,Over,SEEK_CUR) < 0) + int res; + if (gz) + res = gzseek(gz,Over,SEEK_CUR); + else + res = lseek(iFd,Over,SEEK_CUR); + if (res < 0) { Flags |= Fail; return _error->Error("Unable to seek ahead %lu",Over); @@ -751,6 +775,11 @@ bool FileFd::Skip(unsigned long Over) /* */ bool FileFd::Truncate(unsigned long To) { + if (gz) + { + Flags |= Fail; + return _error->Error("Truncating gzipped files is not implemented (%s)", FileName.c_str()); + } if (ftruncate(iFd,To) != 0) { Flags |= Fail; @@ -765,7 +794,11 @@ bool FileFd::Truncate(unsigned long To) /* */ unsigned long FileFd::Tell() { - off_t Res = lseek(iFd,0,SEEK_CUR); + off_t Res; + if (gz) + Res = gztell(gz); + else + Res = lseek(iFd,0,SEEK_CUR); if (Res == (off_t)-1) _error->Errno("lseek","Failed to determine the current file position"); return Res; @@ -776,6 +809,7 @@ unsigned long FileFd::Tell() /* */ unsigned long FileFd::Size() { + //TODO: For gz, do we need the actual file size here or the uncompressed length? struct stat Buf; if (fstat(iFd,&Buf) != 0) return _error->Errno("fstat","Unable to determine the file size"); @@ -789,9 +823,10 @@ bool FileFd::Close() { bool Res = true; if ((Flags & AutoClose) == AutoClose) - if (iFd >= 0 && close(iFd) != 0) + if ((gz != NULL && gzclose(gz) != 0) || (gz == NULL && iFd > 0 && close(iFd) != 0)) Res &= _error->Errno("close",_("Problem closing the file")); iFd = -1; + gz = NULL; if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && FileName.empty() == false) -- cgit v1.2.3 From c4fc2fd7fa0fc63fd8cd6bc9b73492e6baf0222a Mon Sep 17 00:00:00 2001 From: "martin@piware.de" <> Date: Thu, 24 Jun 2010 21:27:27 +0200 Subject: Switch FileFd to not transparently gunzip, since that breaks code which expects the compressed contents to stay (such as the copy backend, or when using file:// repositories. Instead, introduce a new ReadOnlyGzip mode and use that where needed --- apt-pkg/contrib/fileutl.cc | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 11a9e7f7b..2b91a46f7 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -604,12 +604,16 @@ bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms) { case ReadOnly: iFd = open(FileName.c_str(),O_RDONLY); + break; + + case ReadOnlyGzip: + iFd = open(FileName.c_str(),O_RDONLY); if (iFd > 0 && FileName.compare(FileName.size()-3, 3, ".gz") == 0) { - gz = gzdopen (iFd, "r"); - if (gz == NULL) { - close (iFd); - iFd = -1; - } + gz = gzdopen (iFd, "r"); + if (gz == NULL) { + close (iFd); + iFd = -1; + } } break; -- cgit v1.2.3 From 1408e219e76e577fb84ef1c48e58337b6569feec Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Sat, 26 Jun 2010 18:40:15 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - show notice about ignored file instead of being always silent - add a Dir::Ignore-Files-Silently list option to control the notice * --- apt-pkg/contrib/fileutl.cc | 4 ++++ 1 file changed, 4 insertions(+) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 16f7ce929..e95ddf562 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -281,6 +281,7 @@ std::vector GetListOfFilesInDir(string const &Dir, std::vector c } std::vector List; + Configuration::MatchAgainstConfig SilentIgnore("Dir::Ignore-Files-Silently"); DIR *D = opendir(Dir.c_str()); if (D == 0) { @@ -306,6 +307,7 @@ std::vector GetListOfFilesInDir(string const &Dir, std::vector c { if (Debug == true) std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl; + _error->Notice("Ignoring file '%s' in directory '%s' as it has no filename extension", Ent->d_name, Dir.c_str()); continue; } } @@ -313,6 +315,8 @@ std::vector GetListOfFilesInDir(string const &Dir, std::vector c { if (Debug == true) std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl; + if (SilentIgnore.Match(Ent->d_name) == false) + _error->Notice("Ignoring file '%s' in directory '%s' as it has an invalid filename extension", Ent->d_name, Dir.c_str()); continue; } } -- cgit v1.2.3 From 3010fb0e069d2fd4c7a6ade4559bfb659bf8f2fb Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 29 Jun 2010 17:23:24 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - Make FileFd replace files atomically in WriteTemp mode (for cache, etc). --- apt-pkg/contrib/fileutl.cc | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 16f7ce929..0b0739cda 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -27,6 +27,7 @@ #include #include +#include #include #include @@ -654,10 +655,11 @@ bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms) case WriteEmpty: { - struct stat Buf; - if (lstat(FileName.c_str(),&Buf) == 0 && S_ISLNK(Buf.st_mode)) - unlink(FileName.c_str()); - iFd = open(FileName.c_str(),O_RDWR | O_CREAT | O_TRUNC,Perms); + Flags |= Replace; + char *name = strdup((FileName + ".XXXXXX").c_str()); + TemporaryFileName = string(mktemp(name)); + iFd = open(TemporaryFileName.c_str(),O_RDWR | O_CREAT | O_EXCL,Perms); + free(name); break; } @@ -839,11 +841,19 @@ bool FileFd::Close() if (iFd >= 0 && close(iFd) != 0) Res &= _error->Errno("close",_("Problem closing the file")); iFd = -1; - + + if ((Flags & Replace) == Replace) { + FileName = TemporaryFileName; // for the unlink() below. + if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) + Res &= _error->Errno("rename",_("Problem renaming the file")); + } + if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && FileName.empty() == false) if (unlink(FileName.c_str()) != 0) Res &= _error->WarningE("unlnk",_("Problem unlinking the file")); + + return Res; } /*}}}*/ -- cgit v1.2.3 From fd3b761e8cba6ed626639b50b1221246098c7b3a Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Tue, 29 Jun 2010 17:28:33 +0200 Subject: Fix the atomic replace. --- apt-pkg/contrib/fileutl.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 0b0739cda..0b62d1bd8 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -843,9 +843,9 @@ bool FileFd::Close() iFd = -1; if ((Flags & Replace) == Replace) { - FileName = TemporaryFileName; // for the unlink() below. if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) Res &= _error->Errno("rename",_("Problem renaming the file")); + FileName = TemporaryFileName; // for the unlink() below. } if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && -- cgit v1.2.3 From d13c2d3f7b10e558301a05948e91ac4a60160793 Mon Sep 17 00:00:00 2001 From: "martin@piware.de" <> Date: Tue, 6 Jul 2010 12:48:06 +0200 Subject: FileFd(): Drop file name extension check in ReadOnlyGzip mode Drop the ".gz" extension check in FileFd::Open() in ReadOnlyGzip mode, to not depend on a particular file extension. This allows rewriting the gzip method using internal decompression (on ".decomp" files). This requires a zlib bug workaround in FileFd::Close(): When opening an empty file with gzdopen(), gzclose() fails with Z_BUF_ERROR. Do not count this as a failure. --- apt-pkg/contrib/fileutl.cc | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 2b91a46f7..0d2d3f356 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -608,7 +608,7 @@ bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms) case ReadOnlyGzip: iFd = open(FileName.c_str(),O_RDONLY); - if (iFd > 0 && FileName.compare(FileName.size()-3, 3, ".gz") == 0) { + if (iFd > 0) { gz = gzdopen (iFd, "r"); if (gz == NULL) { close (iFd); @@ -827,8 +827,16 @@ bool FileFd::Close() { bool Res = true; if ((Flags & AutoClose) == AutoClose) - if ((gz != NULL && gzclose(gz) != 0) || (gz == NULL && iFd > 0 && close(iFd) != 0)) - Res &= _error->Errno("close",_("Problem closing the file")); + { + if (gz != NULL) { + int e = gzclose(gz); + // gzdopen() 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")); + } else + if (iFd > 0 && close(iFd) != 0) + Res &= _error->Errno("close",_("Problem closing the file")); + } iFd = -1; gz = NULL; -- cgit v1.2.3 From 62d073d937742baf8621a11c3094e0320aa846cd Mon Sep 17 00:00:00 2001 From: David Kalnischkies Date: Fri, 9 Jul 2010 21:46:42 +0200 Subject: check the state of the FileFd before renaming as otherwise the rename will be tried twice e.g. in an "apt-get update" run and every other piece of code closing the FileFd manual before the destructor will do it again. --- apt-pkg/contrib/fileutl.cc | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 62d42e4da..8f7791a8a 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -843,19 +843,21 @@ bool FileFd::Close() bool Res = true; if ((Flags & AutoClose) == AutoClose) if (iFd >= 0 && close(iFd) != 0) - Res &= _error->Errno("close",_("Problem closing the file")); - iFd = -1; + Res &= _error->Errno("close",_("Problem closing the file %s"), FileName.c_str()); - if ((Flags & Replace) == Replace) { + if ((Flags & Replace) == Replace && iFd >= 0) { if (rename(TemporaryFileName.c_str(), FileName.c_str()) != 0) - Res &= _error->Errno("rename",_("Problem renaming the file")); + Res &= _error->Errno("rename",_("Problem renaming the file %s to %s"), TemporaryFileName.c_str(), FileName.c_str()); + FileName = TemporaryFileName; // for the unlink() below. } - + + iFd = -1; + if ((Flags & Fail) == Fail && (Flags & DelOnFail) == DelOnFail && FileName.empty() == false) if (unlink(FileName.c_str()) != 0) - Res &= _error->WarningE("unlnk",_("Problem unlinking the file")); + Res &= _error->WarningE("unlnk",_("Problem unlinking the file %s"), FileName.c_str()); return Res; -- cgit v1.2.3 From 144c096976b2ce05fe64e5761c0df5854f6c0c09 Mon Sep 17 00:00:00 2001 From: Julian Andres Klode Date: Fri, 23 Jul 2010 16:13:15 +0200 Subject: * apt-pkg/contrib/fileutl.cc: - Add FileFd::OpenDescriptor() (needed for python-apt's #383617). --- apt-pkg/contrib/fileutl.cc | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'apt-pkg/contrib/fileutl.cc') diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc index 49b2f3828..2a3b8a87d 100644 --- a/apt-pkg/contrib/fileutl.cc +++ b/apt-pkg/contrib/fileutl.cc @@ -700,6 +700,24 @@ bool FileFd::Open(string FileName,OpenMode Mode, unsigned long Perms) SetCloseExec(iFd,true); return true; } + +bool FileFd::OpenDescriptor(int Fd, OpenMode Mode, bool AutoClose) +{ + Close(); + Flags = (AutoClose) ? FileFd::AutoClose : 0; + iFd = Fd; + if (Mode == ReadOnlyGzip) { + gz = gzdopen (iFd, "r"); + if (gz == NULL) { + if (AutoClose) + close (iFd); + return _error->Errno("gzdopen",_("Could not open file descriptor %d"), + Fd); + } + } + this->FileName = ""; + return true; +} /*}}}*/ // FileFd::~File - Closes the file /*{{{*/ // --------------------------------------------------------------------- -- cgit v1.2.3