After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 598748 - *_download_document prevents non-overwriting saving into directories
*_download_document prevents non-overwriting saving into directories
Status: RESOLVED FIXED
Product: libgdata
Classification: Platform
Component: Google Documents service
git master
Other All
: Normal normal
: ---
Assigned To: libgdata-maint
libgdata-maint
Depends on:
Blocks:
 
 
Reported: 2009-10-17 05:19 UTC by Richard Schwarting
Modified: 2010-01-19 15:23 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Patch altering _gdata_documents_entry_download_document (4.63 KB, patch)
2010-01-12 05:26 UTC, Richard Schwarting
committed Details | Review

Description Richard Schwarting 2009-10-17 05:19:14 UTC
Logic in gdata_documents_entry_download_document() appears to be like this

| if (dest exists) 
|   if (replace_file_if_exists == TRUE)
|     if (dest is directory)
|       output = new child destination in directory
|     else
|       output = dest
|   else 
|     error out
| else 
|   output = dest

This way, you won't be able to save into a directory if you don't set replace_file_if_exists.  However, client code might reasonably want to not overwrite files in the directory, but still just pass a directory.  But, if the target (i.e. the directory) exists and we don't set that flag, we error out :(

PicasaWeb has the following logic, first determine what the true destination will be, and then using an appropriate API to handle overwriting in case of existence.  

| if (target_dest exists) 
|   if (target_dest is a directory) 
|     output = new child destination
| if (output not set)
|   output = target_dest
| 
| if (replace_file_if_exists)
|   g_file_replace (output)
| else 
|   g_file_create (output)


(Note: I would have like to do something like this
| if (target_dest exists and target_dest is directory) 
|   output = new child destination
| else 
|   output = target_dest
| g_file_create (output, replace_file_if_exists ?
|       G_FILE_CREATE_REPLACE_DESTINATION : G_FILE_CREATE_NONE, ...)

But apparently, G_FILE_CREATE_REPLACE_DESTINATION doesn't do what I thought it did (it seems to be ignored in g_file_create () (whose docs say it will never overwrite) and in g_file_replace () (which seems to replace regardless)), and I cannot test the directory without first getting the GFileInfo which I can't do if it doesn't exist first.  Sigh! 



Perhaps the code in gdata_documents_entry_download_common () and the bulk of gdata_picasaweb_file_download_content () are similar enough that they should be merged somewhere?  I just realised that I have to refactor the Picasa one to also handle the thumbnails!
Comment 1 Philip Withnall 2009-10-19 16:38:29 UTC
(In reply to comment #0)
*snip*
> But apparently, G_FILE_CREATE_REPLACE_DESTINATION doesn't do what I thought it
> did (it seems to be ignored in g_file_create () (whose docs say it will never
> overwrite) and in g_file_replace () (which seems to replace regardless)), and I
> cannot test the directory without first getting the GFileInfo which I can't do
> if it doesn't exist first.  Sigh! 

Good catch. Patch (with test cases!) welcome. :-)

> Perhaps the code in gdata_documents_entry_download_common () and the bulk of
> gdata_picasaweb_file_download_content () are similar enough that they should be
> merged somewhere?  I just realised that I have to refactor the Picasa one to
> also handle the thumbnails!

If you can see a decent way to do that when you write gdata_picasaweb_file_download_content(), feel free. I would suggest that a private method of GDataDownloadStream is probably the best place to put such logic. Perhaps _gdata_download_stream_find_destination()?
Comment 2 Richard Schwarting 2010-01-09 07:46:23 UTC
Indeed.  I do have a patch for this languishing.  I will clean it double-check it tomorrow and attach.
Comment 3 Richard Schwarting 2010-01-12 05:26:30 UTC
Created attachment 151221 [details] [review]
Patch altering _gdata_documents_entry_download_document

This makes _gdata_documents_entry_download_document () behave a bit better, and actually, almost exactly like gdata_media_content_download ().

Some of the variables have been changed as well to be more consistent between the two, in case we want to merge them.  

Not quite the "tomorrow" I had typed before.  Need to be better able at anticipating availability :|
Comment 4 Philip Withnall 2010-01-12 17:27:32 UTC
Review of attachment 151221 [details] [review]:

Assuming the documents test suite still passes, please go ahead and commit. Thanks.
Comment 5 Richard Schwarting 2010-01-12 18:09:08 UTC
Patch pushed.

The test suite still passes, and the test documents downloaded to /tmp/ seem alright.

commit 06ca3389099f3dee7b68a49ca5d18d39208d32fe
Author: Richard Schwarting <rschwart@src.gnome.org>
Date:   Tue Jan 12 12:44:58 2010 -0500

    Bug 598748 — *_download_document prevents non-overwriting saving into directories
    
    Make _gdata_documents_entry_download_document rely on
    _gdata_download_stream_find_destination to allow saving into directories
    without the overwrite flag.  Closes: bgo#598748
Comment 6 Thibault Saunier 2010-01-19 15:23:23 UTC
Seems to be much more logical, thanks for the patch Richard!