static bool spl_save(GPtrArray *list, const char *utf8path, GError **error_r) { FILE *file; assert(utf8path != NULL); if (spl_map(error_r) == NULL) return false; char *path_fs = spl_map_to_fs(utf8path, error_r); if (path_fs == NULL) return false; file = fopen(path_fs, "w"); g_free(path_fs); if (file == NULL) { playlist_errno(error_r); return false; } for (unsigned i = 0; i < list->len; ++i) { const char *uri = g_ptr_array_index(list, i); playlist_print_uri(file, uri); } fclose(file); return true; }
GPtrArray * spl_list(GError **error_r) { const char *parent_path_fs = spl_map(error_r); DIR *dir; struct dirent *ent; GPtrArray *list; struct stored_playlist_info *playlist; if (parent_path_fs == NULL) return NULL; dir = opendir(parent_path_fs); if (dir == NULL) { g_set_error_literal(error_r, g_file_error_quark(), errno, g_strerror(errno)); return NULL; } list = g_ptr_array_new(); while ((ent = readdir(dir)) != NULL) { playlist = load_playlist_info(parent_path_fs, ent->d_name); if (playlist != NULL) g_ptr_array_add(list, playlist); } closedir(dir); return list; }
GPtrArray * spl_load(const char *utf8path, GError **error_r) { FILE *file; GPtrArray *list; char *path_fs; if (spl_map(error_r) == NULL) return NULL; path_fs = spl_map_to_fs(utf8path, error_r); if (path_fs == NULL) return NULL; file = fopen(path_fs, "r"); g_free(path_fs); if (file == NULL) { playlist_errno(error_r); return NULL; } list = g_ptr_array_new(); GString *buffer = g_string_sized_new(1024); char *s; while ((s = read_text_line(file, buffer)) != NULL) { if (*s == 0 || *s == PLAYLIST_COMMENT) continue; if (g_path_is_absolute(s)) { char *t = fs_charset_to_utf8(s); if (t == NULL) continue; s = g_strconcat("file://", t, NULL); g_free(t); } else if (!uri_has_scheme(s)) { char *path_utf8; path_utf8 = map_fs_to_utf8(s); if (path_utf8 == NULL) continue; s = path_utf8; } else { s = fs_charset_to_utf8(s); if (s == NULL) continue; } g_ptr_array_add(list, s); if (list->len >= playlist_max_length) break; } fclose(file); return list; }
static char * spl_map_to_fs(const char *name_utf8, GError **error_r) { if (spl_map(error_r) == NULL || !spl_check_name(name_utf8, error_r)) return NULL; char *path_fs = map_spl_utf8_to_fs(name_utf8); if (path_fs == NULL) g_set_error_literal(error_r, playlist_quark(), PLAYLIST_RESULT_BAD_NAME, "Bad playlist name"); return path_fs; }
bool spl_append_song(const char *utf8path, struct song *song, GError **error_r) { FILE *file; struct stat st; if (spl_map(error_r) == NULL) return false; char *path_fs = spl_map_to_fs(utf8path, error_r); if (path_fs == NULL) return false; file = fopen(path_fs, "a"); g_free(path_fs); if (file == NULL) { playlist_errno(error_r); return false; } if (fstat(fileno(file), &st) < 0) { playlist_errno(error_r); fclose(file); return false; } if (st.st_size / (MPD_PATH_MAX + 1) >= (off_t)playlist_max_length) { fclose(file); g_set_error_literal(error_r, playlist_quark(), PLAYLIST_RESULT_TOO_LARGE, "Stored playlist is too large"); return false; } playlist_print_song(file, song); fclose(file); idle_add(IDLE_STORED_PLAYLIST); return true; }
bool spl_rename(const char *utf8from, const char *utf8to, GError **error_r) { if (spl_map(error_r) == NULL) return false; char *from_path_fs = spl_map_to_fs(utf8from, error_r); if (from_path_fs == NULL) return false; char *to_path_fs = spl_map_to_fs(utf8to, error_r); if (to_path_fs == NULL) { g_free(from_path_fs); return false; } bool success = spl_rename_internal(from_path_fs, to_path_fs, error_r); g_free(from_path_fs); g_free(to_path_fs); return success; }
bool spl_clear(const char *utf8path, GError **error_r) { FILE *file; if (spl_map(error_r) == NULL) return false; char *path_fs = spl_map_to_fs(utf8path, error_r); if (path_fs == NULL) return false; file = fopen(path_fs, "w"); g_free(path_fs); if (file == NULL) { playlist_errno(error_r); return false; } fclose(file); idle_add(IDLE_STORED_PLAYLIST); return true; }