#include <apt-pkg/fileutl.h>
#include <apt-pkg/mmap.h>
#include <apt-pkg/error.h>
#include <apt-pkg/acquire-method.h>
#include <apt-pkg/strutl.h>
#include <apt-pkg/hashes.h>
#include <apt-pkg/fileutl.h>
#include <apt-pkg/mmap.h>
#include <apt-pkg/error.h>
#include <apt-pkg/acquire-method.h>
#include <apt-pkg/strutl.h>
#include <apt-pkg/hashes.h>
// return values
enum State {ED_OK, ED_ORDERING, ED_PARSER, ED_FAILURE, MMAP_FAILED};
// return values
enum State {ED_OK, ED_ORDERING, ED_PARSER, ED_FAILURE, MMAP_FAILED};
- State applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_file,
+ State applyFile(FileFd &ed_cmds, FileFd &in_file, FileFd &out_file,
- void ignoreLineInFile(FILE *fin, char *buffer) const;
- void copyLinesFromFileToFile(FILE *fin, FILE *fout, unsigned int lines,
+ void ignoreLineInFile(FileFd &fin, char *buffer) const;
+ void copyLinesFromFileToFile(FileFd &fin, FileFd &fout, unsigned int lines,
Hashes *hash, char *buffer) const;
State patchFile(FileFd &Patch, FileFd &From, FileFd &out_file, Hashes *hash) const;
Hashes *hash, char *buffer) const;
State patchFile(FileFd &Patch, FileFd &From, FileFd &out_file, Hashes *hash) const;
* \param hash the created file for correctness
* \return the success State of the ed command executor
*/
* \param hash the created file for correctness
* \return the success State of the ed command executor
*/
-RredMethod::State RredMethod::applyFile(FILE *ed_cmds, FILE *in_file, FILE *out_file,
+RredMethod::State RredMethod::applyFile(FileFd &ed_cmds, FileFd &in_file, FileFd &out_file,
unsigned long &line, char *buffer, Hashes *hash) const {
// get the current command and parse it
unsigned long &line, char *buffer, Hashes *hash) const {
// get the current command and parse it
copyLinesFromFileToFile(ed_cmds, out_file, data_length, hash, buffer);
}
copyLinesFromFileToFile(ed_cmds, out_file, data_length, hash, buffer);
}
-void RredMethod::copyLinesFromFileToFile(FILE *fin, FILE *fout, unsigned int lines,/*{{{*/
+void RredMethod::copyLinesFromFileToFile(FileFd &fin, FileFd &fout, unsigned int lines,/*{{{*/
- fgets(buffer, BUF_SIZE, fin);
- size_t const written = fwrite(buffer, 1, strlen(buffer), fout);
- hash->Add((unsigned char*)buffer, written);
+ fin.ReadLine(buffer, BUF_SIZE);
+ unsigned long long const towrite = strlen(buffer);
+ fout.Write(buffer, towrite);
+ hash->Add((unsigned char*)buffer, towrite);
-void RredMethod::ignoreLineInFile(FILE *fin, char *buffer) const { /*{{{*/
- fgets(buffer, BUF_SIZE, fin);
+void RredMethod::ignoreLineInFile(FileFd &fin, char *buffer) const { /*{{{*/
+ fin.ReadLine(buffer, BUF_SIZE);
RredMethod::State RredMethod::patchFile(FileFd &Patch, FileFd &From, /*{{{*/
FileFd &out_file, Hashes *hash) const {
char buffer[BUF_SIZE];
RredMethod::State RredMethod::patchFile(FileFd &Patch, FileFd &From, /*{{{*/
FileFd &out_file, Hashes *hash) const {
char buffer[BUF_SIZE];
- FILE* fFrom = fdopen(From.Fd(), "r");
- FILE* fPatch = fdopen(Patch.Fd(), "r");
- FILE* fTo = fdopen(out_file.Fd(), "w");
/* we do a tail recursion to read the commands in the right order */
unsigned long line = -1; // assign highest possible value
/* we do a tail recursion to read the commands in the right order */
unsigned long line = -1; // assign highest possible value
- State const result = applyFile(fPatch, fFrom, fTo, line, buffer, hash);
+ State const result = applyFile(Patch, From, out_file, line, buffer, hash);
- while (fgets(buffer, BUF_SIZE, fFrom) != NULL) {
- size_t const written = fwrite(buffer, 1, strlen(buffer), fTo);
- hash->Add((unsigned char*)buffer, written);
+ while (From.ReadLine(buffer, BUF_SIZE) != NULL) {
+ unsigned long long const towrite = strlen(buffer);
+ out_file.Write(buffer, towrite);
+ hash->Add((unsigned char*)buffer, towrite);
/*}}}*/
RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/
FileFd &out_file, Hashes *hash) const {
/*}}}*/
RredMethod::State RredMethod::patchMMap(FileFd &Patch, FileFd &From, /*{{{*/
FileFd &out_file, Hashes *hash) const {
MMap ed_cmds(Patch, MMap::ReadOnly);
MMap in_file(From, MMap::ReadOnly);
MMap ed_cmds(Patch, MMap::ReadOnly);
MMap in_file(From, MMap::ReadOnly);
- commands = (EdCommand*) realloc(commands, command_alloc * sizeof(EdCommand));
+ EdCommand* newCommands = (EdCommand*) realloc(commands, command_alloc * sizeof(EdCommand));
+ if (newCommands == NULL) {
+ free(commands);
+ return MMAP_FAILED;
+ }
+ commands = newCommands;
// Open the source and destination files (the d'tor of FileFd will do
// the cleanup/closing of the fds)
FileFd From(Path,FileFd::ReadOnly);
// Open the source and destination files (the d'tor of FileFd will do
// the cleanup/closing of the fds)
FileFd From(Path,FileFd::ReadOnly);
FileFd To(Itm->DestFile,FileFd::WriteAtomic);
To.EraseOnFailure();
if (_error->PendingError() == true)
FileFd To(Itm->DestFile,FileFd::WriteAtomic);
To.EraseOnFailure();
if (_error->PendingError() == true)
State const result = patchMMap(Patch, From, To, &Hash);
if (result == MMAP_FAILED) {
// retry with patchFile
State const result = patchMMap(Patch, From, To, &Hash);
if (result == MMAP_FAILED) {
// retry with patchFile
To.Open(Itm->DestFile,FileFd::WriteAtomic);
if (_error->PendingError() == true)
return false;
To.Open(Itm->DestFile,FileFd::WriteAtomic);
if (_error->PendingError() == true)
return false;
and use the access time from the "old" file */
struct stat BufBase, BufPatch;
if (stat(Path.c_str(),&BufBase) != 0 ||
and use the access time from the "old" file */
struct stat BufBase, BufPatch;
if (stat(Path.c_str(),&BufBase) != 0 ||