[vlc-devel] commit: Write snapshot to a temporary file first, then rename it to destination file. (Jean-Paul Saman )

git version control git at videolan.org
Wed Sep 24 12:29:43 CEST 2008


vlc | branch: master | Jean-Paul Saman <jean-paul.saman at m2x.nl> | Wed Sep 24 12:13:24 2008 +0200| [06b2a5e2f7efd4909ce97be00bc909bd054a9424] | committer: Jean-Paul Saman 

Write snapshot to a temporary file first, then rename it to destination file.

It is possible to read from a file which is in the midst of being written.

> http://git.videolan.org/gitweb.cgi/vlc.git/?a=commit;h=06b2a5e2f7efd4909ce97be00bc909bd054a9424
---

 modules/video_filter/scene.c |   35 ++++++++++++++++++++++++++++++-----
 1 files changed, 30 insertions(+), 5 deletions(-)

diff --git a/modules/video_filter/scene.c b/modules/video_filter/scene.c
index 104dac9..a2a0702 100644
--- a/modules/video_filter/scene.c
+++ b/modules/video_filter/scene.c
@@ -265,6 +265,7 @@ static void SavePicture( filter_t *p_filter, picture_t *p_pic )
     filter_sys_t *p_sys = (filter_sys_t *)p_filter->p_sys;
     video_format_t fmt_in, fmt_out;
     char *psz_filename = NULL;
+    char *psz_temp = NULL;
     int i_ret;
 
     memset( &fmt_in, 0, sizeof(video_format_t) );
@@ -275,7 +276,7 @@ static void SavePicture( filter_t *p_filter, picture_t *p_pic )
     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
     fmt_out.i_width = p_sys->i_width;
     fmt_out.i_height = p_sys->i_height;
-    if( !strncmp( p_sys->psz_format, "png", 3 ) )
+    if( strlen( p_sys->psz_format ) == 3 )
         fmt_out.i_chroma = VLC_FOURCC('p','n','g',' ');
     else
         fmt_out.i_chroma = VLC_FOURCC('j','p','e','g');
@@ -294,7 +295,10 @@ static void SavePicture( filter_t *p_filter, picture_t *p_pic )
         fmt_out.i_height = fmt_in.i_height;
     }
 
-    /* Save the snapshot */
+    /*
+     * Save the snapshot to a temporary file and
+     * switch it to the real name afterwards.
+     */
     if( p_sys->b_replace )
         i_ret = asprintf( &psz_filename, "%s" DIR_SEP "%s.%s",
                           p_sys->psz_path, p_sys->psz_prefix,
@@ -307,16 +311,37 @@ static void SavePicture( filter_t *p_filter, picture_t *p_pic )
     if( i_ret == -1 )
     {
         msg_Err( p_filter, "could not create snapshot %s", psz_filename );
-        return;
+        goto error;
     }
     path_sanitize( psz_filename );
 
+    i_ret = asprintf( &psz_temp, "%s.swp", psz_filename );
+    if( i_ret == -1 )
+    {
+        msg_Err( p_filter, "could not create snapshot temporarily file %s", psz_temp );
+        goto error;
+    }
+    path_sanitize( psz_temp );
+
     /* Save the image */
     i_ret = image_WriteUrl( p_sys->p_image, p_pic, &fmt_in, &fmt_out,
-                            psz_filename );
+                            psz_temp );
     if( i_ret != VLC_SUCCESS )
     {
-        msg_Err( p_filter, "could not create snapshot %s", psz_filename );
+        msg_Err( p_filter, "could not create snapshot %s", psz_temp );
     }
+    else
+    {
+        /* switch to the final destination */
+        i_ret = rename( psz_temp, psz_filename );
+        if( i_ret == -1 )
+        {
+            msg_Err( p_filter, "could not rename snapshot %s %m", psz_filename );
+            goto error;
+        }
+    }
+
+error:
+    free( psz_temp );
     free( psz_filename );
 }




More information about the vlc-devel mailing list