Пример #1
0
static bool
spl_rename_internal(const char *from_path_fs, const char *to_path_fs,
                    GError **error_r)
{
    if (!g_file_test(from_path_fs, G_FILE_TEST_IS_REGULAR)) {
        g_set_error_literal(error_r, playlist_quark(),
                            PLAYLIST_RESULT_NO_SUCH_LIST,
                            "No such playlist");
        return false;
    }

    if (g_file_test(to_path_fs, G_FILE_TEST_EXISTS)) {
        g_set_error_literal(error_r, playlist_quark(),
                            PLAYLIST_RESULT_LIST_EXISTS,
                            "Playlist exists already");
        return false;
    }

    if (rename(from_path_fs, to_path_fs) < 0) {
        playlist_errno(error_r);
        return false;
    }

    idle_add(IDLE_STORED_PLAYLIST);
    return true;
}
Пример #2
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;
}
Пример #3
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;
}
Пример #4
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;
}
Пример #5
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;
}
Пример #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;
}