Search code examples
cgtkglibgtk2

gtk+ save file while preserving file permissions in linux


I'm writing a text editor in gtk+ 2.0 & gtksourceview 2.0. Currently I'm using gtk_text_buffer_get_text and g_file_set_contents to save the textbuffer to a file. I see in the docs for g_file_set_contents that it says:

Also since the file is recreated, existing permissions, access control lists, metadata etc. may be lost.

I looked around devhelp and Google and can't find an alternative way to save the textbuffer to a file while preserving file permissions. Any ideas on how to accomplish this? Thanks.


Solution

  • As @ptomato suggested using a method that truncates the file, then writing out the text buffer worked as desired. Here is a snippet of code which worked for me:

    gtk_text_buffer_get_end_iter(tbuffer,&end_iter);
    gtk_text_buffer_get_start_iter(tbuffer,&start_iter);
    text = gtk_text_buffer_get_text(tbuffer,&start_iter,&end_iter,FALSE);
        FILE *fp;
        fp=fopen(path, "w");
        fprintf(fp, "%s", text);
        fclose(fp);