Repository navigation
rework getFileSize - #11659
rework getFileSize#11659
Conversation
|
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
|
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/ |
|
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/ |
|
@schiesbn FYI |
There was a problem hiding this comment.
Would be cool to add 6126 as constant and explain the value.
There was a problem hiding this comment.
That should happen where Crypt::BLOCKSIZE is defined, not here.
There was a problem hiding this comment.
Sure, but you can add it there as well - when we already touch the code why not doing this as well ;-)
There was a problem hiding this comment.
I would favour a separate PR for that, provided that the 6126 will be featured also in other places that should be updated accordingly.
|
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. |
|
@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. |
|
Thanks for the explanation, sounds reasonable. I will have a more detailed look at the code later. |
|
@owncloud-bot ok to test |
|
@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? |
|
@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. |
|
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). |
|
Forgot to reverse the proxy status; is now fixed. |
|
@owncloud-bot Retest this please. |
|
🚀 Test PASSed. 🚀 |
|
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 |
Do we know under which circumstances this can happen? |
|
@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). |
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 |
|
@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. |
One example is if you rescan the file system with occ. @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. |
|
@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. |
|
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. |
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.
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) |
|
@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 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.
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. |
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. |
|
OK, I did some first tests:
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? |
|
@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. |
|
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. |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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".
There was a problem hiding this comment.
Ok, I prefer initialising $cipher correctly; I will update the PR accordingly.
|
The inspection completed: 10 new issues |
|
🚀 Test PASSed. 🚀 |
|
test passed, code looks good... 👍 Need a second reviewer, maybe @DeepDiver1975 or @PVince81 ? |
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
I see, clever idea.
Would you might adding a comment to mention this around the fseek call ?
There was a problem hiding this comment.
Ok, I'll update the PR accordingly after the review is finished.
|
I did some random testing with sharing, SFTP external storage and it all seemed to work fine 👍 |
|
Great work @jknockaert 😄 |
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.