/*
- * Copyright (C) 2007-2014 Andre Noll <maan@tuebingen.mpg.de>
+ * Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>
*
* Licensed under the GPL v2. For licencing details see COPYING.
*/
LS_MODE_PARSER,
};
+/* Data about one audio file. Needed for ls and stat output. */
+struct ls_data {
+ /* Usual audio format handler information. */
+ struct afh_info afhi;
+ /* Audio file selector information. */
+ struct afs_info afsi;
+ /* The full path of the audio file. */
+ char *path;
+ /* The score value (if -a was given). */
+ long score;
+ /* The hash value of the audio file data. */
+ unsigned char *hash;
+};
+
/** The flags accepted by the ls command. */
enum ls_flags {
/** -p */
/**
* The size of the individual output fields of the ls command.
*
- * These depend on the actual content being listed. If, for instance only files
- * with duration less than an hour are being listed, then the duration with is
- * made smaller because then the duration is listed as mm:ss rather than
- * hh:mm:ss.
+ * These depend on the content being listed. For example, if each listed file
+ * is shorter than an hour, the duration format is set to mm:ss. Otherwise it
+ * is set to hh:mm:ss.
*/
struct ls_widths {
/** size of the score field. */
.column_descriptions = aft_cols
};
-/* We don't want dot or dot-dot anywhere. */
-static int verify_dotfile(const char *rest)
-{
- /*
- * The first character was '.', but that has already been discarded, we
- * now test the rest.
- */
- switch (*rest) {
- case '\0': case '/': /* /foo/. and /foo/./bar are not ok */
- return -1;
- case '.': /* path start with /foo/.. */
- if (rest[1] == '\0' || rest[1] == '/')
- return -1; /* /foo/.. or /foo/../bar are not ok */
- /* /foo/..bar is ok */
- }
- return 1;
-}
-
/*
- * We fundamentally don't like some paths: We don't want double slashes or
- * slashes at the end that can make pathnames ambiguous.
+ * Produce a canonicalized absolute pathname.
+ *
+ * Returns one if the resolved path a directory, zero if it is a regular file,
+ * negative on errors.
*/
static int verify_path(const char *orig_path, char **resolved_path)
{
- char c;
- size_t len;
- char *path;
+ int ret;
+ char *path = NULL;
+ struct stat statbuf;
if (*orig_path != '/') /* we only accept absolute paths */
- return -E_BAD_PATH;
- len = strlen(orig_path);
- *resolved_path = para_strdup(orig_path);
- path = *resolved_path;
- while (len > 1 && path[--len] == '/')
- path[len] = '\0'; /* remove slash at the end */
- c = *path++;
- while (c) {
- if (c == '/') {
- c = *path++;
- switch (c) {
- case '/': /* double slash */
- goto bad_path;
- case '.':
- if (verify_dotfile(path) < 0)
- goto bad_path;
- default:
- continue;
- }
- }
- c = *path++;
- }
- return 1;
-bad_path:
- free(*resolved_path);
+ goto fail;
+ path = realpath(orig_path, NULL);
+ if (!path)
+ goto fail;
+ if (stat(path, &statbuf) < 0)
+ goto fail;
+ if (S_ISREG(statbuf.st_mode))
+ ret = 0;
+ else if (S_ISDIR(statbuf.st_mode))
+ ret = 1;
+ else
+ goto fail;
+ *resolved_path = path;
+ return ret;
+fail:
+ *resolved_path = NULL;
+ free(path);
return -E_BAD_PATH;
}
int load_afd(int shmid, struct audio_file_data *afd)
{
void *shm_afd;
- char *buf;
int ret;
ret = shm_attach(shmid, ATTACH_RO, &shm_afd);
if (ret < 0)
return ret;
*afd = *(struct audio_file_data *)shm_afd;
- buf = shm_afd;
- buf += sizeof(*afd);
- load_chunk_table(&afd->afhi, buf);
+ load_chunk_table(&afd->afhi, shm_afd + sizeof(*afd));
shm_detach(shm_afd);
return 1;
}
}
}
-
static int write_attribute_items(struct para_buffer *b,
const char *att_bitmap, struct afs_info *afsi)
{
char *att_text;
int ret;
- ret = WRITE_STATUS_ITEM(b, SI_ATTRIBUTES_BITMAP, "%s\n", att_bitmap);
- if (ret < 0)
- return ret;
+ WRITE_STATUS_ITEM(b, SI_ATTRIBUTES_BITMAP, "%s\n", att_bitmap);
ret = get_attribute_text(&afsi->attributes, " ", &att_text);
if (ret < 0)
return ret;
- ret = WRITE_STATUS_ITEM(b, SI_ATTRIBUTES_TXT, "%s\n", att_text);
+ WRITE_STATUS_ITEM(b, SI_ATTRIBUTES_TXT, "%s\n", att_text);
free(att_text);
return ret;
}
-static int write_lyrics_items(struct para_buffer *b, struct afs_info *afsi)
+static void write_lyrics_items(struct para_buffer *b, struct afs_info *afsi)
{
char *lyrics_name;
- int ret;
- ret = WRITE_STATUS_ITEM(b, SI_LYRICS_ID, "%u\n", afsi->lyrics_id);
- if (ret < 0)
- return ret;
+ WRITE_STATUS_ITEM(b, SI_LYRICS_ID, "%u\n", afsi->lyrics_id);
lyr_get_name_by_id(afsi->lyrics_id, &lyrics_name);
- return WRITE_STATUS_ITEM(b, SI_LYRICS_NAME, "%s\n", lyrics_name?
+ WRITE_STATUS_ITEM(b, SI_LYRICS_NAME, "%s\n", lyrics_name?
lyrics_name : "(none)");
}
-static int write_image_items(struct para_buffer *b, struct afs_info *afsi)
+static void write_image_items(struct para_buffer *b, struct afs_info *afsi)
{
char *image_name;
- int ret;
- ret = WRITE_STATUS_ITEM(b, SI_IMAGE_ID, "%u\n", afsi->image_id);
- if (ret < 0)
- return ret;
+ WRITE_STATUS_ITEM(b, SI_IMAGE_ID, "%u\n", afsi->image_id);
img_get_name_by_id(afsi->image_id, &image_name);
- return WRITE_STATUS_ITEM(b, SI_IMAGE_NAME, "%s\n", image_name?
+ WRITE_STATUS_ITEM(b, SI_IMAGE_NAME, "%s\n", image_name?
image_name : "(none)");
}
-static int write_filename_items(struct para_buffer *b, const char *path,
+static void write_filename_items(struct para_buffer *b, const char *path,
unsigned flags)
{
char *val;
- int ret;
- if (!(flags & LS_FLAG_FULL_PATH))
- return WRITE_STATUS_ITEM(b, SI_BASENAME, "%s\n", path);
- ret = WRITE_STATUS_ITEM(b, SI_PATH, "%s\n", path);
- if (ret < 0)
- return ret;
+ if (!(flags & LS_FLAG_FULL_PATH)) {
+ WRITE_STATUS_ITEM(b, SI_BASENAME, "%s\n", path);
+ return;
+ }
+ WRITE_STATUS_ITEM(b, SI_PATH, "%s\n", path);
val = para_basename(path);
- ret = WRITE_STATUS_ITEM(b, SI_BASENAME, "%s\n", val? val : "");
- if (ret < 0)
- return ret;
+ WRITE_STATUS_ITEM(b, SI_BASENAME, "%s\n", val? val : "");
val = para_dirname(path);
- ret = WRITE_STATUS_ITEM(b, SI_DIRECTORY, "%s\n", val? val : "");
+ WRITE_STATUS_ITEM(b, SI_DIRECTORY, "%s\n", val? val : "");
free(val);
- return ret;
}
static int print_chunk_table(struct ls_data *d, struct para_buffer *b)
ret = aft_get_row_of_hash(d->hash, &aft_row);
if (ret < 0)
return ret;
- ret = osl_open_disk_object(audio_file_table, aft_row,
- AFTCOL_CHUNKS, &chunk_table_obj);
+ ret = osl(osl_open_disk_object(audio_file_table, aft_row,
+ AFTCOL_CHUNKS, &chunk_table_obj));
if (ret < 0)
return ret;
- ret = para_printf(b, "%s\n"
+ para_printf(b, "%s\n"
"chunk_time: %lu:%lu\nchunk_offsets: ",
d->path,
(long unsigned) d->afhi.chunk_tv.tv_sec,
(long unsigned) d->afhi.chunk_tv.tv_usec
);
- if (ret < 0)
- goto out;
buf = chunk_table_obj.data;
- for (i = 0; i <= d->afhi.chunks_total; i++) {
- ret = para_printf(b, "%u ", (unsigned) read_u32(buf + 4 * i));
- if (ret < 0)
- goto out;
- }
- ret = para_printf(b, "\n");
-out:
+ for (i = 0; i <= d->afhi.chunks_total; i++)
+ para_printf(b, "%u ", (unsigned) read_u32(buf + 4 * i));
+ para_printf(b, "\n");
+ ret = 1;
osl_close_disk_object(&chunk_table_obj);
return ret;
}
-static int write_score(struct para_buffer *b, struct ls_data *d,
+static void write_score(struct para_buffer *b, struct ls_data *d,
struct ls_options *opts)
{
if (!(opts->flags & LS_FLAG_ADMISSIBLE_ONLY)) /* no score*/
- return 0;
- return WRITE_STATUS_ITEM(b, SI_SCORE, "%li\n", d->score);
+ return;
+ WRITE_STATUS_ITEM(b, SI_SCORE, "%li\n", d->score);
}
static int print_list_item(struct ls_data *d, struct ls_options *opts,
char asc_hash[2 * HASH_SIZE + 1];
if (opts->mode == LS_MODE_SHORT) {
- ret = para_printf(b, "%s\n", d->path);
+ para_printf(b, "%s\n", d->path);
+ ret = 1;
goto out;
}
if (opts->mode == LS_MODE_CHUNKS) {
if (opts->mode == LS_MODE_LONG) {
struct ls_widths *w = &opts->widths;
if (opts->flags & LS_FLAG_ADMISSIBLE_ONLY) {
- ret = para_printf(b, "%*li ",
- opts->widths.score_width, d->score);
- if (ret < 0)
- goto out;
+ para_printf(b, "%*li ", opts->widths.score_width,
+ d->score);
}
- ret = para_printf(b,
+ para_printf(b,
"%s " /* attributes */
"%*u " /* amp */
"%*d " /* image_id */
last_played_time,
d->path
);
+ ret = 1;
goto out;
}
if (opts->mode == LS_MODE_MBOX) {
const char *bn = para_basename(d->path);
- ret = para_printf(b,
+ para_printf(b,
"From foo@localhost %s\n"
"Received: from\nTo: bar\nFrom: a\n"
"Subject: %s\n\n",
last_played_time,
bn? bn : "?");
- if (ret < 0)
- goto out;
}
- ret = write_filename_items(b, d->path, opts->flags);
- if (ret < 0)
- goto out;
- ret = write_score(b, d, opts);
- if (ret < 0)
- goto out;
+ write_filename_items(b, d->path, opts->flags);
+ write_score(b, d, opts);
ret = write_attribute_items(b, att_buf, afsi);
if (ret < 0)
goto out;
- ret = write_image_items(b, afsi);
- if (ret < 0)
- goto out;
- ret = write_lyrics_items(b, afsi);
- if (ret < 0)
- goto out;
+ write_image_items(b, afsi);
+ write_lyrics_items(b, afsi);
hash_to_asc(d->hash, asc_hash);
- ret = WRITE_STATUS_ITEM(b, SI_HASH, "%s\n", asc_hash);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_BITRATE, "%dkbit/s\n", afhi->bitrate);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_FORMAT, "%s\n",
+ WRITE_STATUS_ITEM(b, SI_HASH, "%s\n", asc_hash);
+ WRITE_STATUS_ITEM(b, SI_BITRATE, "%dkbit/s\n", afhi->bitrate);
+ WRITE_STATUS_ITEM(b, SI_FORMAT, "%s\n",
audio_format_name(afsi->audio_format_id));
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_FREQUENCY, "%dHz\n", afhi->frequency);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_CHANNELS, "%d\n", afhi->channels);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_DURATION, "%s\n", duration_buf);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_SECONDS_TOTAL, "%lu\n",
- afhi->seconds_total);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_LAST_PLAYED, "%s\n", last_played_time);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_NUM_PLAYED, "%d\n", afsi->num_played);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_AMPLIFICATION, "%u\n", afsi->amp);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_CHUNK_TIME, "%lu\n",
- tv2ms(&afhi->chunk_tv));
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_NUM_CHUNKS, "%lu\n",
- afhi->chunks_total);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_TECHINFO, "%s\n", afhi->techinfo);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_ARTIST, "%s\n", afhi->tags.artist);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_TITLE, "%s\n", afhi->tags.title);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_YEAR, "%s\n", afhi->tags.year);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_ALBUM, "%s\n", afhi->tags.album);
- if (ret < 0)
- goto out;
- ret = WRITE_STATUS_ITEM(b, SI_COMMENT, "%s\n", afhi->tags.comment);
- if (ret < 0)
- goto out;
+ WRITE_STATUS_ITEM(b, SI_FREQUENCY, "%dHz\n", afhi->frequency);
+ WRITE_STATUS_ITEM(b, SI_CHANNELS, "%d\n", afhi->channels);
+ WRITE_STATUS_ITEM(b, SI_DURATION, "%s\n", duration_buf);
+ WRITE_STATUS_ITEM(b, SI_SECONDS_TOTAL, "%lu\n", afhi->seconds_total);
+ WRITE_STATUS_ITEM(b, SI_LAST_PLAYED, "%s\n", last_played_time);
+ WRITE_STATUS_ITEM(b, SI_NUM_PLAYED, "%d\n", afsi->num_played);
+ WRITE_STATUS_ITEM(b, SI_AMPLIFICATION, "%u\n", afsi->amp);
+ WRITE_STATUS_ITEM(b, SI_CHUNK_TIME, "%lu\n", tv2ms(&afhi->chunk_tv));
+ WRITE_STATUS_ITEM(b, SI_NUM_CHUNKS, "%lu\n", afhi->chunks_total);
+ WRITE_STATUS_ITEM(b, SI_TECHINFO, "%s\n", afhi->techinfo);
+ WRITE_STATUS_ITEM(b, SI_ARTIST, "%s\n", afhi->tags.artist);
+ WRITE_STATUS_ITEM(b, SI_TITLE, "%s\n", afhi->tags.title);
+ WRITE_STATUS_ITEM(b, SI_YEAR, "%s\n", afhi->tags.year);
+ WRITE_STATUS_ITEM(b, SI_ALBUM, "%s\n", afhi->tags.album);
+ WRITE_STATUS_ITEM(b, SI_COMMENT, "%s\n", afhi->tags.comment);
if (opts->mode == LS_MODE_MBOX) {
struct osl_object lyrics_def;
lyr_get_def_by_id(afsi->lyrics_id, &lyrics_def);
if (lyrics_def.data) {
- ret = para_printf(b, "Lyrics:\n~~~~~~~\n%s",
+ para_printf(b, "Lyrics:\n~~~~~~~\n%s",
(char *)lyrics_def.data);
osl_close_disk_object(&lyrics_def);
}
return ret;
}
-static int make_status_items(struct audio_file_data *afd,
- struct afs_info *afsi, char *path, long score,
- unsigned char *hash)
+static struct ls_data status_item_ls_data;
+static struct osl_row *current_aft_row;
+
+static void make_inode_status_items(struct para_buffer *pb)
+{
+ struct stat statbuf = {.st_size = 0};
+ char *path, mtime_str[30] = "\0";
+ struct tm mtime_tm;
+ int ret;
+
+ ret = get_audio_file_path_of_row(current_aft_row, &path);
+ if (ret < 0)
+ goto out;
+ ret = stat(path, &statbuf);
+ if (ret < 0)
+ goto out;
+ localtime_r(&statbuf.st_mtime, &mtime_tm);
+ ret = strftime(mtime_str, 29, "%b %d %Y", &mtime_tm);
+ assert(ret > 0); /* number of bytes placed in mtime_str */
+out:
+ WRITE_STATUS_ITEM(pb, SI_MTIME, "%s\n", mtime_str);
+ WRITE_STATUS_ITEM(pb, SI_FILE_SIZE, "%ld\n", statbuf.st_size / 1024);
+}
+
+static int make_status_items(void)
{
- struct ls_data d = {
- .afhi = afd->afhi,
- .afsi = *afsi,
- .path = path,
- .score = score,
- .hash = hash
- };
struct ls_options opts = {
.flags = LS_FLAG_FULL_PATH | LS_FLAG_ADMISSIBLE_ONLY,
.mode = LS_MODE_VERBOSE,
int ret;
time(¤t_time);
- ret = print_list_item(&d, &opts, &pb, current_time);
+ ret = print_list_item(&status_item_ls_data, &opts, &pb, current_time);
if (ret < 0)
return ret;
+ make_inode_status_items(&pb);
free(status_items);
status_items = pb.buf;
memset(&pb, 0, sizeof(pb));
pb.max_size = shm_get_shmmax() - 1;
pb.flags = PBF_SIZE_PREFIX;
- ret = print_list_item(&d, &opts, &pb, current_time);
+ ret = print_list_item(&status_item_ls_data, &opts, &pb, current_time);
if (ret < 0) {
free(status_items);
status_items = NULL;
return ret;
}
+ make_inode_status_items(&pb);
free(parser_friendly_status_items);
parser_friendly_status_items = pb.buf;
return 1;
}
/**
- * Mmap the given audio file and update statistics.
+ * Open the audio file with highest score and set up an afd structure.
*
- * \param aft_row Determines the audio file to be opened and updated.
- * \param score The score of the audio file.
* \param afd Result pointer.
*
* On success, the numplayed field of the audio file selector info is increased
*
* \return Positive shmid on success, negative on errors.
*/
-int open_and_update_audio_file(struct osl_row *aft_row, long score,
- struct audio_file_data *afd)
+int open_and_update_audio_file(struct audio_file_data *afd)
{
- unsigned char *aft_hash, file_hash[HASH_SIZE];
+ unsigned char file_hash[HASH_SIZE];
struct osl_object afsi_obj;
- struct afs_info old_afsi, new_afsi;
- int ret = get_hash_of_row(aft_row, &aft_hash);
+ struct afs_info new_afsi;
+ int ret;
struct afsi_change_event_data aced;
struct osl_object map, chunk_table_obj;
- char *path;
-
+ struct ls_data *d = &status_item_ls_data;
+again:
+ ret = score_get_best(¤t_aft_row, &d->score);
if (ret < 0)
return ret;
- ret = get_audio_file_path_of_row(aft_row, &path);
+ ret = get_hash_of_row(current_aft_row, &d->hash);
if (ret < 0)
return ret;
- PARA_NOTICE_LOG("%s\n", path);
- ret = get_afsi_object_of_row(aft_row, &afsi_obj);
+ ret = get_audio_file_path_of_row(current_aft_row, &d->path);
if (ret < 0)
return ret;
- ret = load_afsi(&old_afsi, &afsi_obj);
+ PARA_NOTICE_LOG("%s\n", d->path);
+ ret = get_afsi_object_of_row(current_aft_row, &afsi_obj);
if (ret < 0)
return ret;
- ret = get_afhi_of_row(aft_row, &afd->afhi);
+ ret = load_afsi(&d->afsi, &afsi_obj);
if (ret < 0)
return ret;
- afd->afhi.chunk_table = NULL;
- ret = osl_open_disk_object(audio_file_table, aft_row,
- AFTCOL_CHUNKS, &chunk_table_obj);
+ ret = get_afhi_of_row(current_aft_row, &afd->afhi);
if (ret < 0)
- goto err;
- ret = mmap_full_file(path, O_RDONLY, &map.data,
- &map.size, &afd->fd);
+ return ret;
+ d->afhi = afd->afhi;
+ d->afhi.chunk_table = afd->afhi.chunk_table = NULL;
+ ret = osl(osl_open_disk_object(audio_file_table, current_aft_row,
+ AFTCOL_CHUNKS, &chunk_table_obj));
if (ret < 0)
- goto err;
+ return ret;
+ ret = mmap_full_file(d->path, O_RDONLY, &map.data, &map.size, &afd->fd);
+ if (ret < 0)
+ goto out;
hash_function(map.data, map.size, file_hash);
- ret = hash_compare(file_hash, aft_hash);
+ ret = hash_compare(file_hash, d->hash);
para_munmap(map.data, map.size);
if (ret) {
ret = -E_HASH_MISMATCH;
- goto err;
+ goto out;
}
- new_afsi = old_afsi;
+ new_afsi = d->afsi;
new_afsi.num_played++;
new_afsi.last_played = time(NULL);
save_afsi(&new_afsi, &afsi_obj); /* in-place update */
- afd->audio_format_id = old_afsi.audio_format_id;
+ afd->audio_format_id = d->afsi.audio_format_id;
load_chunk_table(&afd->afhi, chunk_table_obj.data);
- ret = make_status_items(afd, &old_afsi, path, score, file_hash);
- if (ret < 0)
- goto err;
- aced.aft_row = aft_row;
- aced.old_afsi = &old_afsi;
+ aced.aft_row = current_aft_row;
+ aced.old_afsi = &d->afsi;
+ /*
+ * No need to update the status items as the AFSI_CHANGE event will
+ * recreate them.
+ */
afs_event(AFSI_CHANGE, NULL, &aced);
ret = save_afd(afd);
-err:
+out:
free(afd->afhi.chunk_table);
osl_close_disk_object(&chunk_table_obj);
- if (ret < 0)
- PARA_ERROR_LOG("%s: %s\n", path, para_strerror(-ret));
+ if (ret < 0) {
+ PARA_ERROR_LOG("%s: %s\n", d->path, para_strerror(-ret));
+ ret = score_delete(current_aft_row);
+ if (ret >= 0)
+ goto again;
+ }
return ret;
}
goto out;
}
out:
- if (b.offset)
- pass_buffer_as_shm(fd, SBD_OUTPUT, b.buf, b.offset);
- free(b.buf);
+ flush_and_free_pb(&b);
free(opts->data);
free(opts->data_ptr);
free(opts->patterns);
goto out;
if (hs && pb && hs == pb && !(flags & ADD_FLAG_FORCE)) {
if (flags & ADD_FLAG_VERBOSE)
- ret = para_printf(&msg, "ignoring duplicate\n");
- else
- ret = 1;
+ para_printf(&msg, "ignoring duplicate\n");
+ ret = 1;
goto out;
}
if (hs && hs != pb) {
struct osl_object obj;
if (pb) { /* hs trumps pb, remove pb */
- if (flags & ADD_FLAG_VERBOSE) {
- ret = para_printf(&msg, "removing path brother\n");
- if (ret < 0)
- goto out;
- }
+ if (flags & ADD_FLAG_VERBOSE)
+ para_printf(&msg, "removing %s\n", path);
+ afs_event(AUDIO_FILE_REMOVE, &msg, pb);
ret = osl(osl_del_row(audio_file_table, pb));
if (ret < 0)
goto out;
AFTCOL_PATH, &obj));
if (ret < 0)
goto out;
- ret = para_printf(&msg, "renamed from %s\n", (char *)obj.data);
- if (ret < 0)
- goto out;
+ para_printf(&msg, "renamed from %s\n", (char *)obj.data);
}
ret = osl(osl_update_object(audio_file_table, hs, AFTCOL_PATH,
&objs[AFTCOL_PATH]));
if (ret < 0)
goto out;
hash_to_asc(old_hash, old_asc);
- if (flags & ADD_FLAG_VERBOSE) {
- ret = para_printf(&msg, "file change: %s -> %s\n",
- old_asc, asc);
- if (ret < 0)
- goto out;
- }
+ if (flags & ADD_FLAG_VERBOSE)
+ para_printf(&msg, "file change: %s -> %s\n", old_asc,
+ asc);
ret = osl_update_object(audio_file_table, pb, AFTCOL_HASH,
&objs[AFTCOL_HASH]);
if (ret < 0)
if (hs || pb) { /* (hs != NULL and pb != NULL) implies hs == pb */
struct osl_row *row = pb? pb : hs;
/* update afhi and chunk_table */
- if (flags & ADD_FLAG_VERBOSE) {
- ret = para_printf(&msg, "updating afhi and chunk table\n");
- if (ret < 0)
- goto out;
- }
+ if (flags & ADD_FLAG_VERBOSE)
+ para_printf(&msg, "updating afhi and chunk table\n");
ret = osl(osl_update_object(audio_file_table, row, AFTCOL_AFHI,
&objs[AFTCOL_AFHI]));
if (ret < 0)
goto out;
}
/* new entry, use default afsi */
- if (flags & ADD_FLAG_VERBOSE) {
- ret = para_printf(&msg, "new file\n");
- if (ret < 0)
- goto out;
- }
+ if (flags & ADD_FLAG_VERBOSE)
+ para_printf(&msg, "new file\n");
default_afsi.last_played = time(NULL) - 365 * 24 * 60 * 60;
default_afsi.audio_format_id = read_u8(buf + CAB_AUDIO_FORMAT_OFFSET);
out:
if (ret < 0)
para_printf(&msg, "%s\n", para_strerror(-ret));
- if (msg.offset)
- pass_buffer_as_shm(fd, SBD_OUTPUT, msg.buf, msg.offset);
- free(msg.buf);
+ flush_and_free_pb(&msg);
}
/** Used by com_add(). */
unsigned char hash[HASH_SIZE];
ret = guess_audio_format(path);
- if (ret < 0 && !(pad->flags & ADD_FLAG_ALL))
+ if (ret < 0 && !(pad->flags & ADD_FLAG_ALL)) {
+ ret = 0;
goto out_free;
+ }
query.data = (char *)path;
query.size = strlen(path) + 1;
ret = send_callback_request(path_brother_callback, &query,
{
int i, ret;
struct private_add_data pad = {.cc = cc, .flags = 0};
- struct stat statbuf;
for (i = 1; i < cc->argc; i++) {
const char *arg = cc->argv[i];
return ret;
continue;
}
- ret = stat(path, &statbuf);
- if (ret < 0) {
- ret = send_sb_va(&cc->scc, SBD_ERROR_LOG,
- "failed to stat %s (%s)\n", path,
- strerror(errno));
- free(path);
- if (ret < 0)
- return ret;
- continue;
- }
- if (S_ISDIR(statbuf.st_mode))
+ if (ret == 1) /* directory */
ret = for_each_file_in_dir(path, add_one_audio_file,
&pad);
- else
+ else /* regular file */
ret = add_one_audio_file(path, &pad);
if (ret < 0) {
send_sb_va(&cc->scc, SBD_OUTPUT, "%s: %s\n", path,
free(path);
}
return 1;
-
}
/**
struct afsi_change_event_data aced;
ret = get_afsi_object_of_row(row, &obj);
- if (ret < 0)
- return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
+ if (ret < 0) {
+ para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
+ return ret;
+ }
ret = load_afsi(&old_afsi, &obj);
- if (ret < 0)
- return para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
+ if (ret < 0) {
+ para_printf(&tad->pb, "%s: %s\n", name, para_strerror(-ret));
+ return ret;
+ }
new_afsi = old_afsi;
if (no_options) {
new_afsi.num_played++;
new_afsi.last_played = time(NULL);
- if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
- ret = para_printf(&tad->pb, "%s: num_played = %u, "
+ if (tad->cto->flags & TOUCH_FLAG_VERBOSE)
+ para_printf(&tad->pb, "%s: num_played = %u, "
"last_played = now()\n", name,
new_afsi.num_played);
- if (ret < 0)
- return ret;
- }
} else {
- if (tad->cto->flags & TOUCH_FLAG_VERBOSE) {
- ret = para_printf(&tad->pb, "touching %s\n", name);
- if (ret < 0)
- return ret;
- }
+ if (tad->cto->flags & TOUCH_FLAG_VERBOSE)
+ para_printf(&tad->pb, "touching %s\n", name);
if (tad->cto->lyrics_id >= 0)
new_afsi.lyrics_id = tad->cto->lyrics_id;
if (tad->cto->image_id >= 0)
struct touch_action_data tad = {.cto = query->data,
.pb = {
.max_size = shm_get_shmmax(),
- .private_data = &fd,
+ .private_data = &(struct afs_max_size_handler_data) {
+ .fd = fd,
+ .band = SBD_OUTPUT
+ },
.max_size_handler = afs_max_size_handler
}
};
- int ret, ret2 = 0;
+ int ret;
struct pattern_match_data pmd = {
.table = audio_file_table,
.loop_col_num = AFTCOL_HASH,
pmd.fnmatch_flags |= FNM_PATHNAME;
ret = for_each_matching_row(&pmd);
if (ret < 0)
- ret2 = para_printf(&tad.pb, "%s\n", para_strerror(-ret));
+ para_printf(&tad.pb, "%s\n", para_strerror(-ret));
else if (pmd.num_matches == 0)
- ret2 = para_printf(&tad.pb, "no matches\n");
- if (ret2 >= 0 && tad.pb.offset)
- pass_buffer_as_shm(fd, SBD_OUTPUT, tad.pb.buf, tad.pb.offset);
- free(tad.pb.buf);
+ para_printf(&tad.pb, "no matches\n");
+ flush_and_free_pb(&tad.pb);
}
int com_touch(struct command_context *cc)
struct com_rm_action_data *crd = data;
int ret;
- if (crd->flags & RM_FLAG_VERBOSE) {
- ret = para_printf(&crd->pb, "removing %s\n", name);
- if (ret < 0)
- return ret;
- }
+ if (crd->flags & RM_FLAG_VERBOSE)
+ para_printf(&crd->pb, "removing %s\n", name);
afs_event(AUDIO_FILE_REMOVE, &crd->pb, row);
ret = osl(osl_del_row(audio_file_table, row));
if (ret < 0)
struct com_rm_action_data crd = {.flags = *(uint32_t *)query->data,
.pb = {
.max_size = shm_get_shmmax(),
- .private_data = &fd,
+ .private_data = &(struct afs_max_size_handler_data) {
+ .fd = fd,
+ .band = SBD_OUTPUT
+ },
.max_size_handler = afs_max_size_handler
}
};
return;
}
if ((pmd.num_matches == 0) && !(crd.flags & RM_FLAG_FORCE))
- ret = para_printf(&crd.pb, "no matches -- nothing removed\n");
- else if (crd.flags & RM_FLAG_VERBOSE) {
- ret = para_printf(&crd.pb, "removed %u files\n",
- pmd.num_matches);
- }
- if (ret >= 0 && crd.pb.offset)
- pass_buffer_as_shm(fd, SBD_OUTPUT, crd.pb.buf, crd.pb.offset);
- free(crd.pb.buf);
+ para_printf(&crd.pb, "no matches -- nothing removed\n");
+ else if (crd.flags & RM_FLAG_VERBOSE)
+ para_printf(&crd.pb, "removed %u files\n", pmd.num_matches);
+ flush_and_free_pb(&crd.pb);
}
/* TODO options: -r (recursive) */
if (cad->flags & CPSI_FLAG_COPY_ATTRIBUTES)
target_afsi.attributes = cad->source_afsi.attributes;
save_afsi(&target_afsi, &target_afsi_obj); /* in-place update */
- if (cad->flags & CPSI_FLAG_VERBOSE) {
- ret = para_printf(&cad->pb, "copied afsi to %s\n", name);
- if (ret < 0)
- return ret;
- }
+ if (cad->flags & CPSI_FLAG_VERBOSE)
+ para_printf(&cad->pb, "copied afsi to %s\n", name);
aced.aft_row = row;
aced.old_afsi = &old_afsi;
afs_event(AFSI_CHANGE, &cad->pb, &aced);
.flags = *(unsigned *)query->data,
.pb = {
.max_size = shm_get_shmmax(),
- .private_data = &fd,
+ .private_data = &(struct afs_max_size_handler_data) {
+ .fd = fd,
+ .band = SBD_OUTPUT
+ },
.max_size_handler = afs_max_size_handler
}
};
"to %u files\n", source_path, pmd.num_matches);
} else
para_printf(&cad.pb, "no matches - nothing copied\n");
- if (cad.pb.offset)
- pass_buffer_as_shm(fd, SBD_OUTPUT, cad.pb.buf, cad.pb.offset);
- free(cad.pb.buf);
+ flush_and_free_pb(&cad.pb);
}
int com_cpsi(struct command_context *cc)
break;
p[len - 1] = '\0';
ret = get_attribute_bitnum_by_name(p, &bitnum);
- if (ret < 0)
+ if (ret < 0) {
+ para_printf(&cad.pb, "attribute not found: %s\n", p);
goto out;
+ }
if (c == '+')
cad.add_mask |= (1UL << bitnum);
else
out:
if (ret < 0)
para_printf(&cad.pb, "%s\n", para_strerror(-ret));
- if (cad.pb.offset)
- pass_buffer_as_shm(fd, SBD_OUTPUT, cad.pb.buf, cad.pb.offset);
- free(cad.pb.buf);
+ flush_and_free_pb(&cad.pb);
}
int com_setatt(struct command_context *cc)
* is used to pass the status items from the afs process to the command handler
* via a shared memory area and a pipe.
*
- * \return The return value of the underyling call to \ref send_callback_request().
+ * \return The return value of the underlying call to \ref send_callback_request().
*/
int send_afs_status(struct command_context *cc, int parser_friendly)
{
afs_cb_result_handler, cc);
}
-/* TODO: optionally fix problems by removing offending rows */
+/* returns success even on errors to keep the loop going */
static int check_audio_file(struct osl_row *row, void *data)
{
char *path;
struct afs_info afsi;
char *blob_name;
- if (ret < 0)
- return para_printf(pb, "%s\n", para_strerror(-ret));
- if (stat(path, &statbuf) < 0) {
- ret = para_printf(pb, "%s: stat error (%s)\n", path, strerror(errno));
- if (ret < 0)
- return ret;
- } else {
- if (!S_ISREG(statbuf.st_mode)) {
- ret = para_printf(pb, "%s: not a regular file\n", path);
- if (ret < 0)
- return ret;
- }
+ if (ret < 0) {
+ para_printf(pb, "%s\n", para_strerror(-ret));
+ return 0;
}
+ if (stat(path, &statbuf) < 0)
+ para_printf(pb, "%s: stat error (%s)\n", path, strerror(errno));
+ else if (!S_ISREG(statbuf.st_mode))
+ para_printf(pb, "%s: not a regular file\n", path);
ret = get_afsi_of_row(row, &afsi);
- if (ret < 0)
- return para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
- ret = lyr_get_name_by_id(afsi.lyrics_id, &blob_name);
if (ret < 0) {
- ret = para_printf(pb, "%s lyrics id %u: %s\n", path, afsi.lyrics_id,
- para_strerror(-ret));
- if (ret < 0)
- return ret;
+ para_printf(pb, "%s: %s\n", path, para_strerror(-ret));
+ return 1;
}
+ ret = lyr_get_name_by_id(afsi.lyrics_id, &blob_name);
+ if (ret < 0)
+ para_printf(pb, "%s lyrics id %u: %s\n", path, afsi.lyrics_id,
+ para_strerror(-ret));
ret = img_get_name_by_id(afsi.image_id, &blob_name);
if (ret < 0)
- ret = para_printf(pb, "%s image id %u: %s\n", path, afsi.image_id,
+ para_printf(pb, "%s image id %u: %s\n", path, afsi.image_id,
para_strerror(-ret));
- return ret;
+ return 0;
}
/**
},
.max_size_handler = afs_max_size_handler
};
- int ret = para_printf(&pb, "checking audio file table...\n");
-
- if (ret < 0)
- return;
+ para_printf(&pb, "checking audio file table...\n");
audio_file_loop(&pb, check_audio_file);
- if (pb.offset)
- pass_buffer_as_shm(fd, SBD_OUTPUT, pb.buf, pb.offset);
- free(pb.buf);
+ flush_and_free_pb(&pb);
}
/**
PARA_INFO_LOG("audio file table contains %d files\n", num);
return ret;
}
- PARA_INFO_LOG("failed to open audio file table\n");
+ PARA_NOTICE_LOG("failed to open audio file table\n");
audio_file_table = NULL;
- if (ret >= 0 || ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
+ if (ret == -OSL_ERRNO_TO_PARA_ERROR(E_OSL_NOENT))
return 1;
return ret;
}
{
int ret;
- switch(event) {
+ switch (event) {
case ATTRIBUTE_REMOVE: {
const struct rmatt_event_data *red = data;
- ret = para_printf(pb, "clearing attribute %s (bit %u) from all "
+ para_printf(pb, "clearing attribute %s (bit %u) from all "
"entries in the audio file table\n", red->name,
red->bitnum);
+ return audio_file_loop(data, clear_attribute);
+ } case AFSI_CHANGE: {
+ struct afsi_change_event_data *aced = data;
+ uint64_t old_last_played = status_item_ls_data.afsi.last_played;
+ if (aced->aft_row != current_aft_row)
+ return 0;
+ ret = get_afsi_of_row(aced->aft_row, &status_item_ls_data.afsi);
if (ret < 0)
return ret;
- return audio_file_loop(data, clear_attribute);
- }
- default:
+ status_item_ls_data.afsi.last_played = old_last_played;
+ make_status_items();
return 1;
+ } case AFHI_CHANGE: {
+ if (data != current_aft_row)
+ return 0;
+ ret = get_afhi_of_row(data, &status_item_ls_data.afhi);
+ if (ret < 0)
+ return ret;
+ make_status_items();
+ return 1;
+ } default:
+ return 0;
}
}