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 /*{{{*/
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;
}
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