예제 #1
0
static enum playlist_result
spl_save(GPtrArray *list, const char *utf8path)
{
	FILE *file;
	char *path_fs;

	assert(utf8path != NULL);

	path_fs = map_spl_utf8_to_fs(utf8path);
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;

	while (!(file = fopen(path_fs, "w")) && errno == EINTR);
	g_free(path_fs);
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;

	for (unsigned i = 0; i < list->len; ++i) {
		const char *uri = g_ptr_array_index(list, i);
		playlist_print_uri(file, uri);
	}

	while (fclose(file) != 0 && errno == EINTR);
	return PLAYLIST_RESULT_SUCCESS;
}
enum playlist_result
spl_save_queue(const char *name_utf8, const struct queue *queue)
{
	char *path_fs;
	FILE *file;

	if (map_spl_path() == NULL)
		return PLAYLIST_RESULT_DISABLED;

	if (!spl_valid_name(name_utf8))
		return PLAYLIST_RESULT_BAD_NAME;

	path_fs = map_spl_utf8_to_fs(name_utf8);
	if (path_fs == NULL)
		return PLAYLIST_RESULT_BAD_NAME;

	if (g_file_test(path_fs, G_FILE_TEST_EXISTS)) {
		g_free(path_fs);
		return PLAYLIST_RESULT_LIST_EXISTS;
	}

	file = fopen(path_fs, "w");
	g_free(path_fs);

	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;

	for (unsigned i = 0; i < queue_length(queue); i++)
		playlist_print_song(file, queue_get(queue, i));

	fclose(file);

	idle_add(IDLE_STORED_PLAYLIST);
	return PLAYLIST_RESULT_SUCCESS;
}
예제 #3
0
GPtrArray *
spl_load(const char *utf8path)
{
	FILE *file;
	GPtrArray *list;
	char buffer[MPD_PATH_MAX];
	char *path_fs;

	if (!spl_valid_name(utf8path))
		return NULL;

	path_fs = map_spl_utf8_to_fs(utf8path);
	if (path_fs == NULL)
		return NULL;

	while (!(file = fopen(path_fs, "r")) && errno == EINTR);
	g_free(path_fs);
	if (file == NULL)
		return NULL;

	list = g_ptr_array_new();

	while (fgets(buffer, sizeof(buffer), file)) {
		char *s = buffer;

		if (*s == PLAYLIST_COMMENT)
			continue;

		g_strchomp(buffer);

		if (!uri_has_scheme(s)) {
			char *path_utf8;
			struct song *song;

			path_utf8 = map_fs_to_utf8(s);
			if (path_utf8 == NULL)
				continue;

			song = db_get_song(path_utf8);
			g_free(path_utf8);
			if (song == NULL)
				continue;

			s = song_get_uri(song);
		} else
			s = g_strdup(s);

		g_ptr_array_add(list, s);

		if (list->len >= playlist_max_length)
			break;
	}

	while (fclose(file) && errno == EINTR);
	return list;
}
예제 #4
0
enum playlist_result
spl_rename(const char *utf8from, const char *utf8to)
{
	char *from_path_fs, *to_path_fs;
	static enum playlist_result ret;

	if (!spl_valid_name(utf8from) || !spl_valid_name(utf8to))
		return PLAYLIST_RESULT_BAD_NAME;

	from_path_fs = map_spl_utf8_to_fs(utf8from);
	to_path_fs = map_spl_utf8_to_fs(utf8to);

	if (from_path_fs != NULL && to_path_fs != NULL)
		ret = spl_rename_internal(from_path_fs, to_path_fs);
	else
		ret = PLAYLIST_RESULT_DISABLED;

	g_free(from_path_fs);
	g_free(to_path_fs);

	return ret;
}
예제 #5
0
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;
}
예제 #6
0
enum playlist_result
spl_append_song(const char *utf8path, struct song *song)
{
	FILE *file;
	struct stat st;
	char *path_fs;

	if (!spl_valid_name(utf8path))
		return PLAYLIST_RESULT_BAD_NAME;

	path_fs = map_spl_utf8_to_fs(utf8path);
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;

	while (!(file = fopen(path_fs, "a")) && errno == EINTR);
	g_free(path_fs);
	if (file == NULL) {
		int save_errno = errno;
		while (fclose(file) != 0 && errno == EINTR);
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
	}

	if (fstat(fileno(file), &st) < 0) {
		int save_errno = errno;
		while (fclose(file) != 0 && errno == EINTR);
		errno = save_errno;
		return PLAYLIST_RESULT_ERRNO;
	}

	if (st.st_size / (MPD_PATH_MAX + 1) >= (off_t)playlist_max_length) {
		while (fclose(file) != 0 && errno == EINTR);
		return PLAYLIST_RESULT_TOO_LARGE;
	}

	playlist_print_song(file, song);

	while (fclose(file) != 0 && errno == EINTR);

	idle_add(IDLE_STORED_PLAYLIST);
	return PLAYLIST_RESULT_SUCCESS;
}
예제 #7
0
enum playlist_result
spl_delete(const char *name_utf8)
{
	char *path_fs;
	int ret;

	path_fs = map_spl_utf8_to_fs(name_utf8);
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;

	ret = unlink(path_fs);
	g_free(path_fs);
	if (ret < 0)
		return errno == ENOENT
			? PLAYLIST_RESULT_NO_SUCH_LIST
			: PLAYLIST_RESULT_ERRNO;

	idle_add(IDLE_STORED_PLAYLIST);
	return PLAYLIST_RESULT_SUCCESS;
}
예제 #8
0
enum playlist_result
spl_clear(const char *utf8path)
{
	char *path_fs;
	FILE *file;

	if (!spl_valid_name(utf8path))
		return PLAYLIST_RESULT_BAD_NAME;

	path_fs = map_spl_utf8_to_fs(utf8path);
	if (path_fs == NULL)
		return PLAYLIST_RESULT_DISABLED;

	while (!(file = fopen(path_fs, "w")) && errno == EINTR);
	g_free(path_fs);
	if (file == NULL)
		return PLAYLIST_RESULT_ERRNO;

	while (fclose(file) != 0 && errno == EINTR);

	idle_add(IDLE_STORED_PLAYLIST);
	return PLAYLIST_RESULT_SUCCESS;
}