diff --git a/goo/gfile.cc b/goo/gfile.cc index fe05de67..be75c5a6 100644 --- a/goo/gfile.cc +++ b/goo/gfile.cc @@ -670,6 +670,22 @@ GooFile* GooFile::open(const GooString *fileName) { return fd < 0 ? NULL : new GooFile(fd); } +GooFile::GooFile(int fdA) + : fd(fdA) +{ + struct stat statbuf; + fstat(fd, &statbuf); + modifiedTimeOnOpen = statbuf.st_mtim; +} + +bool GooFile::modificationTimeChangedSinceOpen() const +{ + struct stat statbuf; + fstat(fd, &statbuf); + + return modifiedTimeOnOpen.tv_sec != statbuf.st_mtim.tv_sec || modifiedTimeOnOpen.tv_nsec != statbuf.st_mtim.tv_nsec; +} + #endif // _WIN32 //------------------------------------------------------------------------ diff --git a/goo/gfile.h b/goo/gfile.h index 805e5232..233c4829 100644 --- a/goo/gfile.h +++ b/goo/gfile.h @@ -145,16 +145,22 @@ public: static GooFile *open(const wchar_t *fileName); ~GooFile() { CloseHandle(handle); } + + // Asuming than on windows you can't change files that are already open + bool modificationTimeChangedSinceOpen() const { return false; }; private: GooFile(HANDLE handleA): handle(handleA) {} HANDLE handle; #else ~GooFile() { close(fd); } + + bool modificationTimeChangedSinceOpen() const; private: - GooFile(int fdA) : fd(fdA) {} + GooFile(int fdA); int fd; + struct timespec modifiedTimeOnOpen; #endif // _WIN32 }; diff --git a/poppler/ErrorCodes.h b/poppler/ErrorCodes.h index b28528df..216412f6 100644 --- a/poppler/ErrorCodes.h +++ b/poppler/ErrorCodes.h @@ -33,4 +33,6 @@ #define errFileIO 10 // file I/O error +#define errFileChangedSinceOpen 11 // file has changed since opening and save can't be done + #endif diff --git a/poppler/PDFDoc.cc b/poppler/PDFDoc.cc index 147d1f45..3fcc18fe 100644 --- a/poppler/PDFDoc.cc +++ b/poppler/PDFDoc.cc @@ -728,6 +728,11 @@ int PDFDoc::savePageAs(GooString *name, int pageNo) FILE *f; OutStream *outStr; XRef *yRef, *countRef; + + if (file && file->modificationTimeChangedSinceOpen()) + return errFileChangedSinceOpen; + + int rootNum = getXRef()->getNumObjects() + 1; // Make sure that special flags are set, because we are going to read @@ -899,6 +904,9 @@ int PDFDoc::saveAs(GooString *name, PDFWriteMode mode) { } int PDFDoc::saveAs(OutStream *outStr, PDFWriteMode mode) { + if (file && file->modificationTimeChangedSinceOpen()) + return errFileChangedSinceOpen; + if (!xref->isModified() && mode == writeStandard) { // simply copy the original file saveWithoutChangesAs (outStr); @@ -932,6 +940,9 @@ int PDFDoc::saveWithoutChangesAs(GooString *name) { int PDFDoc::saveWithoutChangesAs(OutStream *outStr) { int c; + + if (file && file->modificationTimeChangedSinceOpen()) + return errFileChangedSinceOpen; BaseStream *copyStr = str->copy(); copyStr->reset();