示例#1
0
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;
}
示例#2
0
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;
}
示例#3
0
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;
}
示例#4
0
bool
spl_delete(const char *name_utf8, GError **error_r)
{
    char *path_fs;
    int ret;

    path_fs = spl_map_to_fs(name_utf8, error_r);
    if (path_fs == NULL)
        return false;

    ret = unlink(path_fs);
    g_free(path_fs);
    if (ret < 0) {
        playlist_errno(error_r);
        return false;
    }

    idle_add(IDLE_STORED_PLAYLIST);
    return true;
}
示例#5
0
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;
}
示例#6
0
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;
}