bool pipe;
APT::Configuration::Compressor compressor;
unsigned int openmode;
- FileFdPrivate() : gz(NULL), compressed_fd(-1), compressor_pid(-1), pipe(false) {};
+ unsigned long long seekpos;
+ FileFdPrivate() : gz(NULL), compressed_fd(-1), compressor_pid(-1), pipe(false),
+ openmode(0), seekpos(0) {};
};
// RunScripts - Run a set of scripts from a configuration subtree /*{{{*/
{
if (RealFileExists(File.c_str()) == false)
{
+ // do not show ignoration warnings for directories
+ if (
+#ifdef _DIRENT_HAVE_D_TYPE
+ Ent->d_type == DT_DIR ||
+#endif
+ DirectoryExists(File.c_str()) == true)
+ continue;
if (SilentIgnore.Match(Ent->d_name) == false)
_error->Notice(_("Ignoring '%s' in directory '%s' as it is not a regular file"), Ent->d_name, Dir.c_str());
continue;
}
closedir(D);
+ if (SortList == true)
+ std::sort(List.begin(),List.end());
+ return List;
+}
+std::vector<string> GetListOfFilesInDir(string const &Dir, bool SortList)
+{
+ bool const Debug = _config->FindB("Debug::GetListOfFilesInDir", false);
+ if (Debug == true)
+ std::clog << "Accept in " << Dir << " all regular files" << std::endl;
+
+ std::vector<string> List;
+
+ if (DirectoryExists(Dir.c_str()) == false)
+ {
+ _error->Error(_("List of files can't be created as '%s' is not a directory"), Dir.c_str());
+ return List;
+ }
+
+ DIR *D = opendir(Dir.c_str());
+ if (D == 0)
+ {
+ _error->Errno("opendir",_("Unable to read %s"),Dir.c_str());
+ return List;
+ }
+
+ for (struct dirent *Ent = readdir(D); Ent != 0; Ent = readdir(D))
+ {
+ // skip "hidden" files
+ if (Ent->d_name[0] == '.')
+ continue;
+
+ // Make sure it is a file and not something else
+ string const File = flCombine(Dir,Ent->d_name);
+#ifdef _DIRENT_HAVE_D_TYPE
+ if (Ent->d_type != DT_REG)
+#endif
+ {
+ if (RealFileExists(File.c_str()) == false)
+ {
+ if (Debug == true)
+ std::clog << "Bad file: " << Ent->d_name << " → it is not a real file" << std::endl;
+ continue;
+ }
+ }
+
+ // 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 != '.')
+ 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" << 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;
+ }
+
+ if (Debug == true)
+ std::clog << "Accept file: " << Ent->d_name << " in " << Dir << std::endl;
+ List.push_back(File);
+ }
+ closedir(D);
+
if (SortList == true)
std::sort(List.begin(),List.end());
return List;
else
iFd = open(FileName.c_str(), fileflags, Perms);
+ this->FileName = FileName;
if (iFd == -1 || OpenInternDescriptor(Mode, compressor) == false)
{
if (iFd != -1)
return _error->Errno("open",_("Could not open file %s"), FileName.c_str());
}
- this->FileName = FileName;
SetCloseExec(iFd,true);
return true;
}
std::vector<APT::Configuration::Compressor> const compressors = APT::Configuration::getCompressors();
std::vector<APT::Configuration::Compressor>::const_iterator compressor = compressors.begin();
std::string name;
+
+ // compat with the old API
+ if (Mode == ReadOnlyGzip && Compress == None)
+ Compress = Gzip;
+
switch (Compress)
{
case None: name = "."; break;
d->openmode = Mode;
Flags = (AutoClose) ? FileFd::AutoClose : 0;
iFd = Fd;
+ this->FileName = "";
if (OpenInternDescriptor(Mode, compressor) == false)
{
if (AutoClose)
close (iFd);
return _error->Errno("gzdopen",_("Could not open file descriptor %d"), Fd);
}
- this->FileName = "";
return true;
}
bool FileFd::OpenInternDescriptor(unsigned int const Mode, APT::Configuration::Compressor const &compressor)
else
#endif
Res = read(iFd,To,Size);
- if (Res < 0 && errno == EINTR)
- continue;
+
if (Res < 0)
{
+ if (errno == EINTR)
+ continue;
Flags |= Fail;
+#if APT_USE_ZLIB
+ if (d->gz != NULL)
+ {
+ int err;
+ char const * const errmsg = gzerror(d->gz, &err);
+ if (err != Z_ERRNO)
+ return _error->Error("gzread: %s (%d: %s)", _("Read error"), err, errmsg);
+ }
+#endif
return _error->Errno("read",_("Read error"));
}
To = (char *)To + Res;
Size -= Res;
+ d->seekpos += Res;
if (Actual != 0)
*Actual += Res;
}
#endif
unsigned long long read = 0;
- if (Read(To, Size, &read) == false)
+ while ((Size - 1) != read)
+ {
+ unsigned long long done = 0;
+ if (Read(To + read, 1, &done) == false)
+ return NULL;
+ if (done == 0)
+ break;
+ if (To[read++] == '\n')
+ break;
+ }
+ if (read == 0)
return NULL;
- char* c = To;
- for (; *c != '\n' && *c != '\0' && read != 0; --read, ++c)
- ; // find the end of the line
- if (*c != '\0')
- *c = '\0';
- if (read != 0)
- Seek(Tell() - read);
+ To[read] = '\0';
return To;
}
/*}}}*/
From = (char *)From + Res;
Size -= Res;
+ d->seekpos += Res;
}
while (Res > 0 && Size > 0);
{
if (d->pipe == true)
{
+ // Our poor man seeking in pipes is costly, so try to avoid it
+ unsigned long long seekpos = Tell();
+ if (seekpos == To)
+ return true;
+ else if (seekpos < To)
+ return Skip(To - seekpos);
+
if ((d->openmode & ReadOnly) != ReadOnly)
return _error->Error("Reopen is only implemented for read-only files!");
close(iFd);
+ iFd = 0;
if (TemporaryFileName.empty() == false)
iFd = open(TemporaryFileName.c_str(), O_RDONLY);
else if (FileName.empty() == false)
iFd = open(FileName.c_str(), O_RDONLY);
else
- return _error->Error("Reopen is not implemented for OpenDescriptor()-FileFd!");
+ {
+ if (d->compressed_fd > 0)
+ if (lseek(d->compressed_fd, 0, SEEK_SET) != 0)
+ iFd = d->compressed_fd;
+ if (iFd <= 0)
+ return _error->Error("Reopen is not implemented for pipes opened with FileFd::OpenDescriptor()!");
+ }
if (OpenInternDescriptor(d->openmode, d->compressor) == false)
return _error->Error("Seek on file %s because it couldn't be reopened", FileName.c_str());
if (To != 0)
return Skip(To);
+
+ d->seekpos = To;
return true;
}
int res;
Flags |= Fail;
return _error->Error("Unable to seek to %llu", To);
}
-
+
+ d->seekpos = To;
return true;
}
/*}}}*/
/* */
bool FileFd::Skip(unsigned long long Over)
{
+ if (d->pipe == true)
+ {
+ d->seekpos += Over;
+ char buffer[1024];
+ while (Over != 0)
+ {
+ unsigned long long toread = std::min((unsigned long long) sizeof(buffer), Over);
+ if (Read(buffer, toread) == false)
+ return _error->Error("Unable to seek ahead %llu",Over);
+ Over -= toread;
+ }
+ return true;
+ }
+
int res;
#if APT_USE_ZLIB
if (d->gz != NULL)
Flags |= Fail;
return _error->Error("Unable to seek ahead %llu",Over);
}
-
+ d->seekpos = res;
+
return true;
}
/*}}}*/
/* */
unsigned long long FileFd::Tell()
{
+ // In theory, we could just return seekpos here always instead of
+ // 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)
+ return d->seekpos;
+
off_t Res;
#if APT_USE_ZLIB
if (d->gz != NULL)
Res = lseek(iFd,0,SEEK_CUR);
if (Res == (off_t)-1)
_error->Errno("lseek","Failed to determine the current file position");
+ d->seekpos = Res;
return Res;
}
/*}}}*/
// so we 'read' the content and 'seek' back - see there
if (d->pipe == true)
{
- // FIXME: If we have read first and then FileSize() the report is wrong
- size = 0;
+ unsigned long long const oldSeek = Tell();
char ignore[1000];
unsigned long long read = 0;
do {
Read(ignore, sizeof(ignore), &read);
- size += read;
} while(read != 0);
- Seek(0);
+ size = Tell();
+ Seek(oldSeek);
}
#if APT_USE_ZLIB
// only check gzsize if we are actually a gzip file, just checking for
// gzopen in "direct" mode as well
else if (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
* this ourselves; the original (uncompressed) file size is the last 32
* bits of the file */
// FIXME: Size for gz-files is limited by 32bit… no largefile support
- off_t orig_pos = lseek(iFd, 0, SEEK_CUR);
if (lseek(iFd, -4, SEEK_END) < 0)
return _error->Errno("lseek","Unable to seek to end of gzipped file");
size = 0L;
size = tmp_size;
#endif
- if (lseek(iFd, orig_pos, SEEK_SET) < 0)
+ if (lseek(iFd, oldPos, SEEK_SET) < 0)
return _error->Errno("lseek","Unable to seek in gzipped file");
+
return size;
}
#endif
#if APT_USE_ZLIB
if (d != NULL && d->gz != NULL) {
int const e = gzclose(d->gz);
- // gzdopen() on empty files always fails with "buffer error" here, ignore that
+ // 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