From df0232d67ee161abc1c1341856a2c5e60b9e568a Mon Sep 17 00:00:00 2001 From: waker Date: Tue, 22 May 2012 22:21:55 +0200 Subject: implemented playlist saving as atomic operation; fixed few PATH_MAX definitions --- playlist.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) (limited to 'playlist.c') diff --git a/playlist.c b/playlist.c index 855486c6..3b957487 100644 --- a/playlist.c +++ b/playlist.c @@ -1864,10 +1864,12 @@ plt_save (playlist_t *plt, playItem_t *first, playItem_t *last, const char *fnam } } + char tempfile[PATH_MAX]; + snprintf (tempfile, sizeof (tempfile), "%s.tmp", fname); const char magic[] = "DBPL"; uint8_t majorver = PLAYLIST_MAJOR_VER; uint8_t minorver = PLAYLIST_MINOR_VER; - FILE *fp = fopen (fname, "w+b"); + FILE *fp = fopen (tempfile, "w+b"); if (!fp) { UNLOCK; return -1; @@ -2034,11 +2036,14 @@ plt_save (playlist_t *plt, playItem_t *first, playItem_t *last, const char *fnam UNLOCK; fclose (fp); + if (rename (tempfile, fname) != 0) { + fprintf (stderr, "playlist rename %s -> %s failed: %s\n", tempfile, fname, strerror (errno)); + } return 0; save_fail: UNLOCK; fclose (fp); - unlink (fname); + unlink (tempfile); return -1; } -- cgit v1.2.3