Bug 291374 - Cannot open DOCX document: "Could not read the beginning of the file"
Summary: Cannot open DOCX document: "Could not read the beginning of the file"
Status: RESOLVED FIXED
Alias: None
Product: calligracommon
Classification: Applications
Component: general (show other bugs)
Version: 2.4-snapshots
Platform: Microsoft Windows Microsoft Windows
: VHI critical
Target Milestone: ---
Assignee: Calligra Bugs
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2012-01-12 15:07 UTC by GGG
Modified: 2012-03-09 12:55 UTC (History)
5 users (show)

See Also:
Latest Commit:
Version Fixed In:


Attachments
.docx Debug (156.03 KB, text/plain)
2012-03-01 03:04 UTC, Andrius
Details
.doc debug (135.32 KB, text/plain)
2012-03-01 03:05 UTC, Andrius
Details
Patch for KSaveFile that maybe fixes the issue (662 bytes, patch)
2012-03-07 12:55 UTC, Sebastian Sauer
Details

Note You need to log in before you can comment on or make changes to this bug.
Description GGG 2012-01-12 15:07:33 UTC
Version:           2.4-snapshots (using KDE 4.7.4) 
OS:                MS Windows

When attempting to open DOCX document (and DOC as well) I see the error window saying "Could not open <insert filename.docx>. The reason: Could not read the beginning of the file". 

I tried to open RTF and ODT documents - they work. DOC and DOCX does not work.

Reproducible: Always

Steps to Reproduce:
1. Install Calligra Suite for Windows from http://www.kogmbh.com/download.html
2. Attempt to open DOCX
3. See the error.

Actual Results:  
The error window appears.

Expected Results:  
DOCX document has been opened.

OS: WindowsNT (i686) release Windows 7 Enterprise (64 bit actually)
Compiler: cl.exe
Comment 1 Camilla Boemann 2012-01-12 15:16:45 UTC
Moving to common as it apparently hit many filters on windows.
Comment 2 Halla Rempt 2012-02-01 09:35:14 UTC
Confirming the issue.
Comment 3 Andrius 2012-02-28 21:06:56 UTC
Isn't this a duplicate of bug 210098?
Comment 4 Halla Rempt 2012-02-28 21:14:11 UTC
Yes, it's the same bug. But I'm not sure which way we should close as duplicate, since I think it's up to us, the Calligra developers, to fix this issue...
Comment 5 Andrius 2012-03-01 03:04:44 UTC
Created attachment 69201 [details]
.docx Debug

debug info - opening .docx file.
Comment 6 Andrius 2012-03-01 03:05:34 UTC
Created attachment 69202 [details]
.doc debug

debug info - opening .doc file.
Comment 7 Andrius 2012-03-03 20:04:59 UTC
KZip uses KSaveFile, and in KSaveFile::finalize(), the KDE::rename call uses WINAPI's MoveFileExW, which returns zero (failure) and GetLastError gives error "5: Access Denied" and I have no clue why.

In KSaveFile I have at that point:

d->tempFileName:  "c:/users/andrius/appdata/roaming/.kde/tmp-andrius-pc/wordsvc6680.tmpgx6680.new"
d->realFileName:  "c:/users/andrius/appdata/roaming/.kde/tmp-andrius-pc/wordsvc6680.tmp"

As far as I could see, [wordsvc6680.tmpgx6680.new] was a valid ODF file, but since KSaveFile fails, no data is written to [wordsvc6680.tmp].

I've tried using QDir::toNativeSeparators, but this does not seem to be the problem there, as KSaveFile is also used for configuration files (e.g.: c:/users/andrius/appdata/roaming/.kde/config/wordsrc) and works normally.
Comment 8 Camilla Boemann 2012-03-03 20:16:32 UTC
do the tmp-andrius-pc exist
and if not does it work if you create that directory by hand
Comment 9 Andrius 2012-03-03 20:21:17 UTC
(In reply to comment #8)
> do the tmp-andrius-pc exist
> and if not does it work if you create that directory by hand

As I said, the file in KSaveFile's d->tempFileName is a valid ODF file, it's under the same dir.
Comment 10 Sebastian Sauer 2012-03-07 04:12:55 UTC
I think the reason could be that the temporary file is still open. Windows cannot rename files that are opened at least afaik. The problem is that KTemporaryFile/QTemporaryFile::close() does *NOT* close the file/file-descriptor;

bool QTemporaryFileEngine::close()
{
    // Don't close the file, just seek to the front.
    seek(0);
    setError(QFile::UnspecifiedError, QString());
    return true;
}

What means that the temporary file will stay open as long as it's used making it impossible to rename it during it's lifetime and when it's actually closed it#s to late cause it's autoRemove'd means deleted :-/

Does that make sense?
Comment 11 Sebastian Sauer 2012-03-07 04:21:30 UTC
There btw is a

bool QTemporaryFileEngine::rename(const QString &newName)
{
    QFSFileEngine::close();
    return QFSFileEngine::rename(newName);
}

which does really close the temporary-file using the QFSFileEngine::close() rather then the overloaded QTemporaryFileEngine::close(). But KDE does not use that as you just wrote by uses the WinAPI direct bypassing the QTemporaryFile special-case :-/
Comment 12 Sebastian Sauer 2012-03-07 04:24:31 UTC
Suggestion would be to not use KSaveFile in KArchive. Just create the archive and check later or on write if writing/saving it failed....
Comment 13 Sebastian Sauer 2012-03-07 04:34:26 UTC
If my assumption above is true then just creating any QTemporaryFile zip-file using KArchive would always fail. It should be rather easy to make a simple test-case but since I do not have a KDE windows devel setup any longer... :-/
Comment 14 Halla Rempt 2012-03-07 07:51:44 UTC
Yes, that makes a lot of sense to me.
Comment 15 Camilla Boemann 2012-03-07 09:40:56 UTC
So assuming you deductions are correct (sounds plausable) how would we go around making the best long term solution:

You suggest not using KSaveFile in KArchive, but wouldn't that just leave other users of KSaveFile to rediscover the bug. So wouldn't the best solution be to fix KSaveFile somehow? 

For the windows packaging of Calligra we should be able to patch KDElibs ourselves, so we wouldn't have to wait for a bug fix release of KDElibs. Or at least that is what I assume.
Comment 16 Sebastian Sauer 2012-03-07 12:55:36 UTC
Created attachment 69347 [details]
Patch for KSaveFile that maybe fixes the issue

re KSaveFile; the current code and with it the bug is gone with Qt5/kdeframeworks ( for the case http://mail.kde.org/pipermail/kde-frameworks-devel/2011-October/000003.html will be or already is applied ) but yes, we should still fix it for the time being till Qt5/kdeframeworks is out and a Calligra-version based on it is ported and a Calligra@Windows version of it done and so on. Attached patch should fix it (all untested - it would rock if someone with a windows devel-setup could test if that really fixes the issue this bugreport is about).
Comment 17 Halla Rempt 2012-03-07 13:58:19 UTC
Comment on attachment 69347 [details]
Patch for KSaveFile that maybe fixes the issue

I had to add .toLocal8Bit() to both arguments before I could compile, but after that I was sorry to see that the problem persists.
Comment 18 Halla Rempt 2012-03-09 12:55:50 UTC
Fixed in bf47fed5ead06f2128abf042897f86bab39cf333