~ubuntu-branches/debian/lenny/libgsf/lenny

« back to all changes in this revision

Viewing changes to gsf-gnome/gsf-output-gnomevfs.c

  • Committer: Bazaar Package Importer
  • Author(s): J.H.M. Dassen (Ray)
  • Date: 2006-11-06 22:45:03 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 dapper)
  • Revision ID: james.westby@ubuntu.com-20061106224503-g6pmv1m82zy8jya9
Tags: 1.14.3-1
New upstream release.

Show diffs side-by-side

added added

removed removed

Lines of Context:
15
15
 *
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
19
19
 * USA
20
20
 */
21
21
 
41
41
 *
42
42
 * Returns a new file or NULL.
43
43
 **/
44
 
GsfOutputGnomeVFS *
 
44
GsfOutput *
45
45
gsf_output_gnomevfs_new (char const *text_uri, GError **err)
46
46
{
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);
50
50
        return res;
51
51
}
57
57
 *
58
58
 * Returns a new file or NULL.
59
59
 **/
60
 
GsfOutputGnomeVFS *
 
60
GsfOutput *
61
61
gsf_output_gnomevfs_new_uri (GnomeVFSURI * uri, GError **err)
62
62
{
63
63
        GsfOutputGnomeVFS *output;
64
64
        GnomeVFSHandle *handle;
65
65
        GnomeVFSResult res;
 
66
        int perms = -1;
66
67
 
67
68
        if (uri == NULL) {
68
69
                g_set_error (err, gsf_output_error_id (), 0,
69
70
                             "Filename/URI cannot be NULL");
70
71
                return NULL;
71
 
        } else
 
72
        }
 
73
 
 
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;
 
79
 
 
80
                info = gnome_vfs_file_info_new ();
 
81
                res = gnome_vfs_get_file_info_uri (uri,
 
82
                                                   info,
 
83
                                                   GNOME_VFS_FILE_INFO_FOLLOW_LINKS|GNOME_VFS_FILE_INFO_GET_ACCESS_RIGHTS);
 
84
 
 
85
                if ((res == GNOME_VFS_OK) && (info->valid_fields & GNOME_VFS_FILE_INFO_FIELDS_PERMISSIONS)) {
 
86
                        perms = info->permissions;
 
87
                } 
 
88
 
 
89
                gnome_vfs_file_info_unref (info);
 
90
        }
 
91
 
 
92
        if (perms == -1) {
 
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);
73
96
 
 
97
                if (res != GNOME_VFS_OK) {
 
98
                        res = gnome_vfs_create_uri (&handle, uri, GNOME_VFS_OPEN_WRITE|GNOME_VFS_OPEN_RANDOM, FALSE, 0644);
 
99
                }
 
100
        } else {
 
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);
 
104
 
 
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);
 
108
                }
 
109
        }
 
110
 
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));
77
114
                return NULL;
78
115
        }
79
116
 
 
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);
 
121
 
80
122
        output = g_object_new (GSF_OUTPUT_GNOMEVFS_TYPE, NULL);
81
123
        output->handle = handle;
82
124
 
83
 
        return output;
 
125
        return GSF_OUTPUT (output);
84
126
}
85
127
 
86
128
static gboolean