Write the contents of the file with fwrite instead of g_file_set_contents

Since g_file_set_contents removes the file and re-creates it, all permissions get lost during this operation. Using fwrite preserves all the permissions.