Keep the original file mode when writing files.
[ruby-vorbistagger.git] / ext / vcedit.c
index 7976a066f4e5409fd5945617d6ec386fa4439cd2..414d6d060f31c0d98f6b3a4a12dfa18748e79904 100644 (file)
@@ -23,6 +23,8 @@
 #include <errno.h>
 #include <limits.h>
 #include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
 #include <ogg/ogg.h>
 #include <vorbis/codec.h>
 #include <assert.h>
@@ -41,6 +43,8 @@ struct vcedit_state_St {
        vorbis_info vi;
 
        FILE *in;
+       mode_t file_mode;
+
        bool opened;
        long serial;
        unsigned char *mainbuf;
@@ -265,6 +269,7 @@ vcedit_open (vcedit_state *s)
        ogg_packet *header;
        ogg_packet header_main, header_comments, header_codebooks;
        ogg_page og;
+       struct stat st;
        char *buffer;
        size_t bytes, total = 0;
        int i = 0;
@@ -273,6 +278,8 @@ vcedit_open (vcedit_state *s)
        if (!s->in)
                return VCEDIT_ERR_OPEN;
 
+       s->file_mode = stat (s->filename, &st) ? 0664 : st.st_mode;
+
        ogg_sync_init (&s->oy);
 
        do {
@@ -560,6 +567,7 @@ cleanup:
                fclose (out);
                unlink (s->filename);
                rename (tmpfile, s->filename);
+               chmod (s->filename, s->file_mode);
        }
 
        ogg_stream_clear (&streamout);