<commit_before>#include "spfs_fuse_track.h"
#include "spfs_fuse_artist.h"
#include "spfs_spotify.h"

static int is_starred_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	char *str = spotify_track_is_starred(SPFS_SP_SESSION, e->parent->auxdata) ? "1" : "0";
	READ_OFFSET(str, buf, size, offset);
	return size;
}

static int is_local_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	char *str = spotify_track_is_local(SPFS_SP_SESSION, e->parent->auxdata) ? "1" : "0";
	READ_OFFSET(str, buf, size, offset);
	return size;
}

static int is_autolinked_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	char *str = spotify_track_is_autolinked(SPFS_SP_SESSION, e->parent->auxdata) ? "1" : "0";
	READ_OFFSET(str, buf, size, offset);
	return size;
}

static int offlinestatus_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	const char *str = spotify_track_offline_status_str(
			spotify_track_offline_get_status(e->parent->auxdata)
			);
	READ_OFFSET(str, buf, size, offset);
	return size;
}

static int name_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup(spotify_track_name(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int disc_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup_printf("%d",
			spotify_track_disc(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int index_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup_printf("%d",
			spotify_track_index(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int popularity_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup_printf("%d",
			spotify_track_popularity(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int duration_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup_printf("%d",
			spotify_track_duration(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int pcm_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	sp_session *session = SPFS_SP_SESSION;

	if (!spotify_play_track(session, e->parent->auxdata)) {
		g_warning("Failed to play track!");
		return 0;
	}
	memset(buf, 0, size);
	size_t read = spotify_get_audio(buf, size);
	return read;
}

int artists_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
		struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	sp_track *track = e->parent->auxdata;
	int num_artists = spotify_track_num_artists(track);
	for (int i = 0; i < num_artists; ++i) {
		sp_artist *artist = spotify_track_artist(track, i);
		const gchar *artistname = spotify_artist_name(artist);
		spfs_entity *artist_browse_dir = create_artist_browse_dir(artist);
		if (!spfs_entity_dir_has_child(e->e.dir, artistname)) {
			spfs_entity *artist_link = spfs_entity_link_create(artistname, NULL);
			spfs_entity_dir_add_child(e, artist_link);
			gchar *rpath = relpath(e, artist_browse_dir);
			spfs_entity_link_set_target(artist_link, rpath);
			g_free(rpath);
		}
	}
	return 0;
}

spfs_entity *create_track_browse_dir(sp_track *track) {
	g_return_val_if_fail(track != NULL, NULL);
	spfs_entity *track_browse_dir = spfs_entity_find_path(SPFS_DATA->root, "/browse/tracks");
	sp_link *link = spotify_link_create_from_track(track);

	g_return_val_if_fail(link != NULL, NULL);
	gchar track_linkstring[1024] = {0, };
	spotify_link_as_string(link, track_linkstring, 1024);
	if (spfs_entity_dir_has_child(track_browse_dir->e.dir, track_linkstring)) {
		return NULL;
	}
	spfs_entity *track_dir = spfs_entity_dir_create(track_linkstring, NULL);
	track_dir->auxdata = track;

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("name", name_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("pcm", pcm_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("duration", duration_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("popularity", popularity_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("index", index_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("disc", disc_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("starred", is_starred_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("local", is_local_read));
<commit_msg>Don't return NULL if a track dir already exists<commit_after>#include "spfs_fuse_track.h"
#include "spfs_fuse_artist.h"
#include "spfs_spotify.h"

static int is_starred_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	char *str = spotify_track_is_starred(SPFS_SP_SESSION, e->parent->auxdata) ? "1" : "0";
	READ_OFFSET(str, buf, size, offset);
	return size;
}

static int is_local_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	char *str = spotify_track_is_local(SPFS_SP_SESSION, e->parent->auxdata) ? "1" : "0";
	READ_OFFSET(str, buf, size, offset);
	return size;
}

static int is_autolinked_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	char *str = spotify_track_is_autolinked(SPFS_SP_SESSION, e->parent->auxdata) ? "1" : "0";
	READ_OFFSET(str, buf, size, offset);
	return size;
}

static int offlinestatus_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	const char *str = spotify_track_offline_status_str(
			spotify_track_offline_get_status(e->parent->auxdata)
			);
	READ_OFFSET(str, buf, size, offset);
	return size;
}

static int name_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup(spotify_track_name(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int disc_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup_printf("%d",
			spotify_track_disc(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int index_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup_printf("%d",
			spotify_track_index(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int popularity_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup_printf("%d",
			spotify_track_popularity(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int duration_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	gchar *str = g_strdup_printf("%d",
			spotify_track_duration(e->parent->auxdata));
	READ_OFFSET(str, buf, size, offset);
	g_free(str);
	return size;
}

static int pcm_read(const char *path, char *buf, size_t size, off_t offset, struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	sp_session *session = SPFS_SP_SESSION;

	if (!spotify_play_track(session, e->parent->auxdata)) {
		g_warning("Failed to play track!");
		return 0;
	}
	memset(buf, 0, size);
	size_t read = spotify_get_audio(buf, size);
	return read;
}

int artists_readdir(const char *path, void *buf, fuse_fill_dir_t filler, off_t offset,
		struct fuse_file_info *fi) {
	spfs_entity *e = (spfs_entity *)fi->fh;
	sp_track *track = e->parent->auxdata;
	int num_artists = spotify_track_num_artists(track);
	for (int i = 0; i < num_artists; ++i) {
		sp_artist *artist = spotify_track_artist(track, i);
		const gchar *artistname = spotify_artist_name(artist);
		spfs_entity *artist_browse_dir = create_artist_browse_dir(artist);
		if (!spfs_entity_dir_has_child(e->e.dir, artistname)) {
			spfs_entity *artist_link = spfs_entity_link_create(artistname, NULL);
			spfs_entity_dir_add_child(e, artist_link);
			gchar *rpath = relpath(e, artist_browse_dir);
			spfs_entity_link_set_target(artist_link, rpath);
			g_free(rpath);
		}
	}
	return 0;
}

spfs_entity *create_track_browse_dir(sp_track *track) {
	g_return_val_if_fail(track != NULL, NULL);
	spfs_entity *track_browse_dir = spfs_entity_find_path(SPFS_DATA->root, "/browse/tracks");
	sp_link *link = spotify_link_create_from_track(track);

	g_return_val_if_fail(link != NULL, NULL);
	gchar track_linkstring[1024] = {0, };
	spotify_link_as_string(link, track_linkstring, 1024);
	if (spfs_entity_dir_has_child(track_browse_dir->e.dir, track_linkstring)) {
		return spfs_entity_dir_get_child(track_browse_dir->e.dir, track_linkstring);
	}
	spfs_entity *track_dir = spfs_entity_dir_create(track_linkstring, NULL);
	track_dir->auxdata = track;

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("name", name_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("pcm", pcm_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("duration", duration_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("popularity", popularity_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("index", index_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("disc", disc_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("starred", is_starred_read));

	spfs_entity_dir_add_child(track_dir,
			spfs_entity_file_create("local", is_local_read));

{'fraction_non_alphanumeric': 0.12705684367988032, 'fraction_numerical': 0.0035527299925205685, 'mean_word_length': 6.428472222222222, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 78, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '77360', 'n_tokens_mistral': 4419, 'n_tokens_neox': 4210, 'n_words': 857}