Skip to content

rework getFileSize - #11659

Merged
PVince81 merged 6 commits into
owncloud:masterfrom
jknockaert:master
Oct 30, 2014
Merged

PVince81 merged 6 commits into
owncloud:masterfrom
jknockaert:master

Conversation

@jknockaert

Copy link
Copy Markdown
Contributor

I have been rewriting getFileSize with the purpose of avoiding opening the encrypted file as an encrypted stream. I will need this to avoid recursion when I rework #8390 (to make the encrypted stream seekable the unencrypted filesize is required upon opening the stream).
I used the opportunity to make the procedure more robust for a number of exotic situation, such as local streams that are non-seekable or streams that return less than 8192 bytes upon fread. That should all be covered now.
I didn't do much testing yet. It seems to work on my installation. Any feedback appreciated.

@ghost

ghost commented Oct 19, 2014

Copy link
Copy Markdown

Thanks a lot for your contribution! Contributions to the core repo require a signed contributors agreement http://owncloud.org/contribute/agreement/ Alternatively you can add a comment here stating that this contribution is MIT licensed. Some more details about out pull request workflow can be found here: http://owncloud.org/code-reviews-on-github/

2 similar comments
@ghost

ghost commented Oct 19, 2014

Copy link
Copy Markdown

Thanks a lot for your contribution! Contributions to the core repo require a signed contributors agreement http://owncloud.org/contribute/agreement/ Alternatively you can add a comment here stating that this contribution is MIT licensed. Some more details about out pull request workflow can be found here: http://owncloud.org/code-reviews-on-github/

@ghost

ghost commented Oct 19, 2014

Copy link
Copy Markdown

Thanks a lot for your contribution! Contributions to the core repo require a signed contributors agreement http://owncloud.org/contribute/agreement/ Alternatively you can add a comment here stating that this contribution is MIT licensed. Some more details about out pull request workflow can be found here: http://owncloud.org/code-reviews-on-github/

@LukasReschke

Copy link
Copy Markdown
Contributor

@schiesbn FYI

Comment thread apps/files_encryption/lib/util.php Outdated

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would be cool to add 6126 as constant and explain the value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should happen where Crypt::BLOCKSIZE is defined, not here.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, but you can add it there as well - when we already touch the code why not doing this as well ;-)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would favour a separate PR for that, provided that the 6126 will be featured also in other places that should be updated accordingly.

@schiessle

Copy link
Copy Markdown
Contributor

Thanks for your contribution! Can you elaborate a bit about the problem solved by this PR? This also looks like a performance drawback because you read the whole file until you reach the last chunk instead of fseek directly to it.

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesbn No, I fseek to the last chunk, and then I read it. But when fseek fails (because of an unseekable stream), the reading mechanism reads through the whole stream. In both cases the last chunk is correctly identified. In the case of a seekable stream it is as efficient as the existing implementation, but it also accounts for the exotic case where fread would not return the entire last chunk (a possibility according to the php documentation), my implementation would keep reading until it hits the end of the stream. In the case of a non-seekable stream my implementation is probably more efficient, as the existing implementation would have to read the whole encrypted file to copy it and subsequently operate on the (seekable) copy. My implementation only reads through the nonseekable stream once and doesn't need a copy. That way I avoid the issue that the copy may be unseekable as well. And the implementation is compatible with freads returning less than 8192 bytes.
Apart from its robustness (which I claim is better than the existing implementation), the problem solved is that of recursivity that may happen when I rewrite #8390 for OC7. In #8390 I make the encrypted stream seekable. To do so I need to have the unencrypted size upon opening the encrypted stream. I would like to call postFileSize (as suggested by you before). postFileSize in turn will call getFileSize if the unencrypted size is unknown. In the existing implementation getFileSize opens the encrypted file as an encrypted stream, hence the recursivity. I thought breaking that should happen here, as it's only when getFileSize is called that the actual file should be accessed.

@schiessle

Copy link
Copy Markdown
Contributor

Thanks for the explanation, sounds reasonable. I will have a more detailed look at the code later.

@schiessle

Copy link
Copy Markdown
Contributor

@owncloud-bot ok to test

@DeepDiver1975

Copy link
Copy Markdown
Contributor

@schiesbn more high level question: is it necessary to read the real file size from the encrypted file?

We already store the unencrypted file size in the database - working on this level should sufficient from my pov. E.g. in case we open an encrypted file and append some bytes we could calculate the file size based on the old file size and the change.

This way there would be no need to decrypt the while file. Or do I miss anything?

@jknockaert

Copy link
Copy Markdown
Contributor Author

@DeepDiver1975 I have been puzzled as well by the need for getFileSize. But as far as I can see, it is called every time a new file is uploaded over DAV. Calling getFileSize only happens when postFileSize is called with (in the database) the encrypted size > 0 and the unencrypted size == 0. So somehow the database ends up in that state and needs a fix, which can only happen by reading the encrypted file (and it is my argument that this should happen here rather than in the encrypted stream definition). When I was playing with #8390 I could not find out how this specific situation happens, but as far as I understood it could not be fixed in the open/close procedures of the encrypted stream.

@jknockaert

Copy link
Copy Markdown
Contributor Author

When cron.php runs with uid pubShare_xxxxxxxx it somehow initialises getFileSize for files that are not shared. getShareKey then returns false; so I updated getFileSize to return 0 in that case (which results in the same behaviour as with the existing code). By doing the check first an error message with multiKeyDecrypt is avoided (and it is more efficient as well).
Other than that I added some small fixes.

@jknockaert

Copy link
Copy Markdown
Contributor Author

Forgot to reverse the proxy status; is now fixed.

@LukasReschke

Copy link
Copy Markdown
Contributor

@owncloud-bot Retest this please.

@ghost

ghost commented Oct 22, 2014

Copy link
Copy Markdown

🚀 Test PASSed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org//job/pull-request-analyser-ng-simple/929/
🚀 Test PASSed. 🚀

@schiessle

Copy link
Copy Markdown
Contributor

If the unencrypted size is stored in the database, then this value should be used. Reading the file to calculate the unencrypted size is only a fall-back if for some reasons we don't have a unencrypted size at the database

For reference: https://github.com/owncloud/core/blob/master/apps/files_encryption/lib/proxy.php#L360

@DeepDiver1975

Copy link
Copy Markdown
Contributor

if for some reasons we don't have a unencrypted size at the database

Do we know under which circumstances this can happen?
Maybe we can eliminate these reasons and kill that code snippet

@jknockaert

Copy link
Copy Markdown
Contributor Author

@DeepDiver1975 No I couldn't determine. This is pretty low level stuff. When an encrypted stream is opened a number of things happen in the background. But I did observe that getFileSize is called pretty often and in a deterministic way (so everytime you do a specific action it's being called).

@DeepDiver1975

Copy link
Copy Markdown
Contributor

But I did observe that getFileSize is called pretty often

Thx for the input - especially if it is called pretty often we should analyze the scenarios and try to eliminate them.

@schiesbn can you please have a look - looks like this is more than just a fall-back - THX

@jknockaert

Copy link
Copy Markdown
Contributor Author

@DeepDiver1975 Yes in regular use it's a structural element in the functioning of ownCloud. getFileSize is called every time a new file is uploaded. It's also called when cron.php runs and finds a new unshared file (even when in that case filesize cannot be determined, because with the public sharekey the file cannot be read). I'll have a second look at it when I will be reworking the stream functions.

@schiessle

Copy link
Copy Markdown
Contributor

Do we know under which circumstances this can happen?
Maybe we can eliminate these reasons and kill that code snippet

One example is if you rescan the file system with occ.
I would not like to kill that code. We need to guarantee that getFileSize() always returns the correct (unencrypted size). If it doesn't exists in the database, for whatever reasons we need a way to re-calculate it. As you can see at the code I mentioned, the re-calculation will not be called if a unencrypted size exists. If it gets called from somewhere else it is a bug which need to be fixed. Just removing the code is not a solution.

@jknockaert as you can see here https://github.com/owncloud/core/blob/master/apps/files_encryption/lib/proxy.php#L360 it should only be called if we don't have a unencrypted size in the database. If you have the feeling that it is also called if a unencrypted size exists in the database then it would be good if you could debug it and find out how and why it gets called.

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesbn I'm actually debugging now. Some first observations. When a file is uploaded with the owncloud client, sabre kicks in. A lot happens then in lib/private/connector/sabre/file.php. First the data is stored in a partFile with put_files_contents. That correctly opens an encrypted stream, writes to it, and closes it. Upon closure, stream_close does write file info to the cache, but not for the partfile but rather the "normal" file (I guess that fails because the file doesn't exist yet). In the next step it wants to check if the file was fully received, and asks for the file size of the partFile. Because there's no filesize registered for the partFile, getFileSize kicks in. After that the partFile is renamed to give it its final name using rename. That operation somehow calls postGetFileInfo (for the final filename), which in turn calls postFileSize (but I couldn't find the exact mechanism linking rename to getFileInfo, rename doesn't open an encrypted stream). Again it doesn't find the relevant fileinfo and calls getFileSize. So far for creating a file. Seems like there's a bit of work left to do. All case of calls to getFileSize that I observed so far happen because https://github.com/owncloud/core/blob/master/apps/files_encryption/lib/proxy.php#L358 fails.

@jknockaert

Copy link
Copy Markdown
Contributor Author

Actually upon renaming the partFile to the final file, a lot of getFileInfo happens, not only for the final file (the first instance of these repeated calls does invoke getFileSize), but also for its parrent folder and for the trashbin. I guess there's some updating of the filesystem happening, but I couldn't find where that code is.

@DeepDiver1975

Copy link
Copy Markdown
Contributor

One example is if you rescan the file system with occ.

not really - nothing outside of owncloud can change an encrypted file. The saved file size in the database is as per definition the correct file size.

Whenever a file enters ownCloud we know the file size before the file is encrypted - there is no situation where this is not the case. As a result this fallback code is pointless.

If it doesn't exists in the database, for whatever reasons we need a way to re-calculate it.

The times are long gone where the database is to be seen as a volatile cache. As stated above especially with encryption enabled file have to enter ownCloud through ownCloud - bypassing is not possible (by the way this means we can disable the background scanner as soon as encryption is enabled)

@jknockaert

Copy link
Copy Markdown
Contributor Author

@DeepDiver1975 I agree with you, but the current situation is far from that. I also observed that deleted files are not removed from cache, so the cache keeps growing; it shouldn't.
I have been thinking a bit further, and I think this should be the topic of a separate PR. I suggest first to continue with rewriting getFileSize, as there are still some smaller issues with the current PR that I need to iron out. Next I would like to rewrite the encrypted stream code to make it searchable, as the benefits are considerable (in terms of consistency, stability and efficiency). And next on the list could be a review of the handling of encrypted files and the cache. There's a large number of file operations that should be covered, and as I noted above they are not well integrated with the cache handling.

@schiessle

Copy link
Copy Markdown
Contributor

not really - nothing outside of owncloud can change an encrypted file. The saved file size in the database is as per definition the correct file size.

I remember bug reports not that long ago that user had lost the unencrypted size because the re-scanned the filesystem with occ, but maybe this was a bug in the occ script. Luckily it was recalculated by getFileSize. No matter how unlikely it is to happen, I feel better if we have some self-healing mechanism. If the user loses this information it is hardly a option to tell him that he now have to delete all his files and upload them again.

by the way this means we can disable the background scanner as soon as encryption is enabled

No, you can still upload your files directly to your samba, dropbox,... Of course the file will not be encrypted until you touched it with ownCloud. But it works and I'm not convinced that we should prohibit it.

@DeepDiver1975

Copy link
Copy Markdown
Contributor

No, you can still upload your files directly to your samba, dropbox,... Of course the file will not be encrypted until you touched it with ownCloud.

Crazy shit we do ;-)

@schiessle

Copy link
Copy Markdown
Contributor

Crazy shit we do ;-)

And that's just the tip of the iceberg ;-)

Seriously, we already have to explain often enough restrictions which are not avoidable because of encryption. I don't want to add additional ones which could be avoided.

@jknockaert thanks for trying to debug this. I will also make some tests, maybe I can find something.

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesbn I agree there's a useful purpose for getFileSize, but I also agree with @DeepDiver1975 that it shouldn't be used where the encrypted stream should set the cache right. I now understand what happens when uploading a file with the owncloud client.
The first call to getFileSize happens here: https://github.com/owncloud/core/blob/master/lib/private/connector/sabre/file.php#L110 The partFile was created using an encrypted stream here: https://github.com/owncloud/core/blob/master/lib/private/connector/sabre/file.php#L77 Upon close_stream, the filecache is not set for the partfile but for the real file: https://github.com/owncloud/core/blob/master/apps/files_encryption/lib/stream.php#L630 So that means that getFileSize is invoked to calculate the unencrypted size of the partFile.
The next call happens here: https://github.com/owncloud/core/blob/master/lib/private/connector/sabre/file.php#L119 The rename actually runs this: https://github.com/owncloud/core/blob/master/lib/private/files/view.php#L425 Because renaming from a part file to the real one, this condition is met: https://github.com/owncloud/core/blob/master/lib/private/files/view.php#L505 which calls signal_post_create here https://github.com/owncloud/core/blob/master/lib/private/files/view.php#L352. There's a post_create hook in apps/activity, probably to register the activity: https://github.com/owncloud/activity/blob/master/lib/hooks.php#L35 Running fileCreate runs addNotificationsForFileAction which runs getUserPathsFromPath which returns getUsersSharingFile: https://github.com/owncloud/activity/blob/master/lib/hooks.php#L135 That function calls getFileInfo: https://github.com/owncloud/core/blob/master/lib/private/share/share.php#L111, which in turn runs postFileSize https://github.com/owncloud/core/blob/master/apps/files_encryption/lib/proxy.php#L301 For some reason the filecache is not set properly at this instance so it calls getFileSize here: https://github.com/owncloud/core/blob/master/apps/files_encryption/lib/proxy.php#L373
It's clear that in both cases the call to getFileSize happens because the file cache is not set correctly whereas it should have been. Even when I now understand what happens, I'm not sure where it should be corrected. I think the partfile should be registered anyhow in the filecache. Upon rename the filecache should be updated in a rename hook. For some reason I observed that the rename hooks are not run when updating from a partfile, rather does it invoke write hooks and create hooks.
Still I think that we should open a separate PR for this.

@schiessle

Copy link
Copy Markdown
Contributor

OK, I did some first tests:

  • initial file encryption, getFileSize not called
  • upload file, getFileSize not called
  • download file, getFileSize not called
  • edit file with external text exitor, getFileSize not called
  • share file to user2, user2 downloads the file, getFileSize not called
  • delete unencrypted size from file cache, getFileSize is called
  • sync client download files, getFileSize not called
  • sync client edit file, getFileSize not called, Fix: if we read a .part file we try to get the file info from the real file #11736

Most stuff seems to work fine. The sync client behaviour is strange. We call getFileSize for the part file written to the files folder before it gets renamed to the original filename. If getFileSize ignores part files and instead return the real size the sync client stops with the error that he can't download the file, even if a file was uploaded but not downloaded. Maybe a check from the sync client that the complete file was uploaded?

@jknockaert

Copy link
Copy Markdown
Contributor Author

@schiesbn pls also test uploading over dav. I also noticed geFileSize to be called when cron.php is run and there's a new unshared file.

@jknockaert

Copy link
Copy Markdown
Contributor Author

I've been researching an error that I noticed when running cron.php. But the error also happens with the old code and is probably related to lucene.
I think my getFileSize PR is now ready for review.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to read the Cipher always this way. parseHeader() and getCipher() can handle the case if no header exists. If no header exists we always need to use the legacy cipher "AES-128-CFB" and not the cipher configured in config.php.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What I do is first initialise $cipher with Helper::getCipher() (at line 388). I assume that handles legacy cases well (didn't test it). Then in the case where there is a header the value of $cipher is updated (and all along the stream pointer is moved; the code is optimised to pass only once through the file; also the encrypted file $size is updated to reflect the pointer move). Note that $data only contains the first encrypted block at this stage. The alternative would be to initialise $data as an empty string, update it if there's a header, and then run parseHeader() and getCipher() on $data (even when it is an empty string). Would that be preferred?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this doesn't handle the legacy case well. Because this way you use the cipher configured in config.php which is not necessarily "AES-128-CFB" which was used for all old files.

The alternative would be to initialise $data as an empty string, update it if there's a header, and then run parseHeader() and getCipher() on $data (even when it is an empty string).

Yes, this should work. Another solution would be to initialize $cipher not with Helper::getCipher() but with "AES-128-CFB".

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I prefer initialising $cipher correctly; I will update the PR accordingly.

@scrutinizer-notifier

Copy link
Copy Markdown

The inspection completed: 10 new issues

@ghost

ghost commented Oct 28, 2014

Copy link
Copy Markdown

🚀 Test PASSed. 🚀
Refer to this link for build results (access rights to CI server needed):
https://ci.owncloud.org//job/pull-request-analyser-ng-simple/1462/
🚀 Test PASSed. 🚀

@schiessle

Copy link
Copy Markdown
Contributor

test passed, code looks good... 👍

Need a second reviewer, maybe @DeepDiver1975 or @PVince81 ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is the workaround not needed any more ?
Some external storages don't support fseek(), that's why it needed to be downloaded locally with getLocalFile() (ugly but needed)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, it's not needed anymore. At line 424 it will try to do an fseek to the last block of the file (and if that succeeds realSize will be updated to reflect to pointer relocation). The loop starting on line 429 will then read through the remainder of the file, which is either the last block only (for seekable streams) or the full file minus the header (for nonseekable streams). This will always work (for correctly implemented streams).

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see, clever idea.
Would you might adding a comment to mention this around the fseek call ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, I'll update the PR accordingly after the review is finished.

@PVince81

Copy link
Copy Markdown
Contributor

I did some random testing with sharing, SFTP external storage and it all seemed to work fine 👍

PVince81 pushed a commit that referenced this pull request Oct 30, 2014
@PVince81
PVince81 merged commit 146c46b into owncloud:master Oct 30, 2014
@PVince81

Copy link
Copy Markdown
Contributor

Great work @jknockaert 😄

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants