16
16
* You should have received a copy of the GNU Lesser General Public License
17
17
* along with this program; if not, write to the Free Software
18
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
18
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301
42
42
* Returns a new file or NULL.
45
45
gsf_output_gnomevfs_new (char const *text_uri, GError **err)
47
GnomeVFSURI *uri = gnome_vfs_uri_new (text_uri);
48
GsfOutputGnomeVFS *res =gsf_output_gnomevfs_new_uri (uri, err);
47
GnomeVFSURI *uri = gnome_vfs_uri_new (text_uri);
48
GsfOutput *res = gsf_output_gnomevfs_new_uri (uri, err);
49
49
gnome_vfs_uri_unref (uri);
58
58
* Returns a new file or NULL.
61
61
gsf_output_gnomevfs_new_uri (GnomeVFSURI * uri, GError **err)
63
63
GsfOutputGnomeVFS *output;
64
64
GnomeVFSHandle *handle;
65
65
GnomeVFSResult res;
68
69
g_set_error (err, gsf_output_error_id (), 0,
69
70
"Filename/URI cannot be NULL");
74
if (gnome_vfs_uri_exists (uri)) {
75
/* see bug 159442 - if the file exists, we want to do our best to preserve existing
76
* pemissions AND truncate the file. that is, we want to emulate truncate() in case
77
* a gnomevfs backend doesn't support it */
78
GnomeVFSFileInfo *info;
80
info = gnome_vfs_file_info_new ();
81
res = gnome_vfs_get_file_info_uri (uri,
83
GNOME_VFS_FILE_INFO_FOLLOW_LINKS|GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
85
if ((res == GNOME_VFS_OK) && (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS)) {
86
perms = info->permissions;
89
gnome_vfs_file_info_unref (info);
93
/* we didn't get the permissions, but calling open_uri() with OPEN_WRITE set will create the file for us.
94
* if the uri_exists(), let's hope that truncate() works. */
72
95
res = gnome_vfs_open_uri (&handle, uri, GNOME_VFS_OPEN_WRITE|GNOME_VFS_OPEN_RANDOM);
97
if (res != GNOME_VFS_OK) {
98
res = gnome_vfs_create_uri (&handle, uri, GNOME_VFS_OPEN_WRITE|GNOME_VFS_OPEN_RANDOM, FALSE, 0644);
101
/* we got the permissions, so let's call create() with the existing permissions instead of open() since
102
* create() will truncate the file for us. */
103
res = gnome_vfs_create_uri (&handle, uri, GNOME_VFS_OPEN_WRITE|GNOME_VFS_OPEN_RANDOM, FALSE, perms);
105
if (res != GNOME_VFS_OK) {
106
/* create() failed. let's see if we can open_uri() instead and hope that truncate works. */
107
res = gnome_vfs_open_uri (&handle, uri, GNOME_VFS_OPEN_WRITE|GNOME_VFS_OPEN_RANDOM);
74
111
if (res != GNOME_VFS_OK) {
75
112
g_set_error (err, gsf_output_error_id (), (gint) res,
76
113
gnome_vfs_result_to_string (res));
117
/* truncate the file to length 0 so if we overwrite a file smaller than
118
* it was before, it doesn't show the rest of the old file (Bug: 159442).
119
* for many gnomevfs backends, this might actually be a noop */
120
gnome_vfs_truncate_handle(handle, 0);
80
122
output = g_object_new (GSF_OUTPUT_GNOMEVFS_TYPE, NULL);
81
123
output->handle = handle;
125
return GSF_OUTPUT (output);