summaryrefslogtreecommitdiff
path: root/apt-pkg/contrib
diff options
context:
space:
mode:
authorDavid Kalnischkies <kalnischkies@gmail.com>2010-01-16 23:09:42 +0100
committerDavid Kalnischkies <kalnischkies@gmail.com>2010-01-16 23:09:42 +0100
commite29a6bb14dcc004d174ad8502b76623139fbee06 (patch)
tree246376ac5f6e5ce15c819086a5a13f27a20176eb /apt-pkg/contrib
parentdaee881bf82d23197d991227fa0ab36b918b4323 (diff)
Fix the newly introduced method GetListOfFilesInDir to not accept every
file if no extension is enforced (= restore old behaviour). (Closes: #565213) This commit includes also: * apt-pkg/policy.cc: - accept also partfiles with "pref" file extension as valid * apt-pkg/contrib/configuration.cc: - accept also partfiles with "conf" file extension as valid * doc/apt.conf.5.xml: - reorder description and split out syntax - add partfile name convention (Closes: #558348) * doc/apt_preferences.conf.5.xml: - describe partfile name convention also here And a lovely test application of course.
Diffstat (limited to 'apt-pkg/contrib')
-rw-r--r--apt-pkg/contrib/configuration.cc2
-rw-r--r--apt-pkg/contrib/fileutl.cc84
-rw-r--r--apt-pkg/contrib/fileutl.h5
3 files changed, 85 insertions, 6 deletions
diff --git a/apt-pkg/contrib/configuration.cc b/apt-pkg/contrib/configuration.cc
index 80b089fac..7588b041c 100644
--- a/apt-pkg/contrib/configuration.cc
+++ b/apt-pkg/contrib/configuration.cc
@@ -832,7 +832,7 @@ bool ReadConfigFile(Configuration &Conf,const string &FName,bool const &AsSectio
bool ReadConfigDir(Configuration &Conf,const string &Dir,
bool const &AsSectional, unsigned const &Depth)
{
- vector<string> const List = GetListOfFilesInDir(Dir, "", true);
+ vector<string> const List = GetListOfFilesInDir(Dir, "conf", true, true);
// Read the files
for (vector<string>::const_iterator I = List.begin(); I != List.end(); I++)
diff --git a/apt-pkg/contrib/fileutl.cc b/apt-pkg/contrib/fileutl.cc
index cce8a4512..da32983f1 100644
--- a/apt-pkg/contrib/fileutl.cc
+++ b/apt-pkg/contrib/fileutl.cc
@@ -202,8 +202,37 @@ 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<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
- bool const &SortList)
+ bool const &SortList)
{
+ return GetListOfFilesInDir(Dir, Ext, SortList, false);
+}
+std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
+ bool const &SortList, bool const &AllowNoExt)
+{
+ std::vector<string> ext;
+ ext.reserve(2);
+ if (Ext.empty() == false)
+ ext.push_back(Ext);
+ if (AllowNoExt == true && ext.empty() == false)
+ ext.push_back("");
+ return GetListOfFilesInDir(Dir, ext, SortList);
+}
+std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
+ bool const &SortList)
+{
+ // Attention debuggers: need to be set with the environment config file!
+ bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false);
+ if (Debug == true)
+ {
+ std::clog << "Accept in " << Dir << " only files with the following " << Ext.size() << " extensions:" << std::endl;
+ if (Ext.empty() == true)
+ std::clog << "\tNO extension" << std::endl;
+ else
+ for (std::vector<string>::const_iterator e = Ext.begin();
+ e != Ext.end(); ++e)
+ std::clog << '\t' << (e->empty() == true ? "NO" : *e) << " extension" << std::endl;
+ }
+
std::vector<string> List;
DIR *D = opendir(Dir.c_str());
if (D == 0)
@@ -214,28 +243,73 @@ std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
{
+ // skip "hidden" files
if (Ent->d_name[0] == '.')
continue;
- if (Ext.empty() == false && flExtension(Ent->d_name) != Ext)
- continue;
+ // check for accepted extension:
+ // no extension given -> periods are bad as hell!
+ // extensions given -> "" extension allows no extension
+ if (Ext.empty() == false)
+ {
+ string d_ext = flExtension(Ent->d_name);
+ if (d_ext == Ent->d_name) // no extension
+ {
+ if (std::find(Ext.begin(), Ext.end(), "") == Ext.end())
+ {
+ if (Debug == true)
+ std::clog << "Bad file: " << Ent->d_name << " → no extension" << std::endl;
+ continue;
+ }
+ }
+ else if (std::find(Ext.begin(), Ext.end(), d_ext) == Ext.end())
+ {
+ if (Debug == true)
+ std::clog << "Bad file: " << Ent->d_name << " → bad extension »" << flExtension(Ent->d_name) << "«" << std::endl;
+ continue;
+ }
+ }
- // Skip bad file names ala run-parts
+ // Skip bad filenames ala run-parts
const char *C = Ent->d_name;
for (; *C != 0; ++C)
if (isalpha(*C) == 0 && isdigit(*C) == 0
- && *C != '_' && *C != '-' && *C != '.')
+ && *C != '_' && *C != '-') {
+ // no required extension -> dot is a bad character
+ if (*C == '.' && Ext.empty() == false)
+ continue;
break;
+ }
+ // we don't reach the end of the name -> bad character included
if (*C != 0)
+ {
+ if (Debug == true)
+ std::clog << "Bad file: " << Ent->d_name << " → bad character »"
+ << *C << "« in filename (period allowed: " << (Ext.empty() ? "no" : "yes") << ")" << std::endl;
continue;
+ }
+
+ // skip filenames which end with a period. These are never valid
+ if (*(C - 1) == '.')
+ {
+ if (Debug == true)
+ std::clog << "Bad file: " << Ent->d_name << " → Period as last character" << std::endl;
+ continue;
+ }
// Make sure it is a file and not something else
string const File = flCombine(Dir,Ent->d_name);
struct stat St;
if (stat(File.c_str(),&St) != 0 || S_ISREG(St.st_mode) == 0)
+ {
+ if (Debug == true)
+ std::clog << "Bad file: " << Ent->d_name << " → stat says not a good file" << std::endl;
continue;
+ }
+ if (Debug == true)
+ std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl;
List.push_back(File);
}
closedir(D);
diff --git a/apt-pkg/contrib/fileutl.h b/apt-pkg/contrib/fileutl.h
index 2807c29d9..85a94898c 100644
--- a/apt-pkg/contrib/fileutl.h
+++ b/apt-pkg/contrib/fileutl.h
@@ -82,8 +82,13 @@ bool RunScripts(const char *Cnf);
bool CopyFile(FileFd &From,FileFd &To);
int GetLock(string File,bool Errors = true);
bool FileExists(string File);
+// FIXME: next ABI-Break: merge the two method-headers
std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
bool const &SortList);
+std::vector<string> GetListOfFilesInDir(string const &Dir, string const &Ext,
+ bool const &SortList, bool const &AllowNoExt);
+std::vector<string> GetListOfFilesInDir(string const &Dir, std::vector<string> const &Ext,
+ bool const &SortList);
string SafeGetCWD();
void SetCloseExec(int Fd,bool Close);
void SetNonBlock(int Fd,bool Block);