From 191d16070b9511f5b32b1ede7a9d0db1ffb3da54 Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Mon, 10 Sep 2007 21:52:00 +0200 Subject: [PATCH] afs: Replace DATABASE_DIR by a runtime config option. Also, split afs_init() into two parts: The new open_afs_tables() that does what its name suggests and the remaining part that initializes the scheduler. --- afs.c | 92 ++++++++++++++++++++++++++++++++++++----------------- afs.h | 13 +++----- aft.c | 9 +++--- attribute.c | 6 ++-- blob.c | 10 +++--- score.c | 5 +-- server.ggo | 14 ++++++++ 7 files changed, 98 insertions(+), 51 deletions(-) diff --git a/afs.c b/afs.c index 5e9bd118..21148f0e 100644 --- a/afs.c +++ b/afs.c @@ -723,43 +723,46 @@ void register_tasks(uint32_t cookie) register_command_task(cookie); } -__noreturn int afs_init(uint32_t cookie, int socket_fd) +static int open_afs_tables(void) { int ret; - enum play_mode current_play_mode; - struct sched s; - - server_socket = socket_fd; - PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n", - server_socket, (unsigned) cookie); - - ret = attribute_init(&afs_tables[TBLNUM_ATTRIBUTES]); + char *db; + + if (conf.afs_database_dir_given) + db = conf.afs_database_dir_arg; + else { + char *home = para_homedir(); + db = make_message("%s/.paraslash/afs_database", home); + free(home); + } + PARA_INFO_LOG("afs_database dir %s\n", db); + ret = para_mkdir(db, 0777); + if (ret < 0 && ret != -E_EXIST) + goto err; + ret = attribute_init(&afs_tables[TBLNUM_ATTRIBUTES], db); if (ret < 0) - goto attribute_init_error; - ret = moods_init(&afs_tables[TBLNUM_MOODS]); + goto err; + ret = moods_init(&afs_tables[TBLNUM_MOODS], db); if (ret < 0) goto moods_init_error; - ret = playlists_init(&afs_tables[TBLNUM_PLAYLIST]); + ret = playlists_init(&afs_tables[TBLNUM_PLAYLIST], db); if (ret < 0) goto playlists_init_error; - ret = lyrics_init(&afs_tables[TBLNUM_LYRICS]); + ret = lyrics_init(&afs_tables[TBLNUM_LYRICS], db); if (ret < 0) goto lyrics_init_error; - ret = images_init(&afs_tables[TBLNUM_IMAGES]); + ret = images_init(&afs_tables[TBLNUM_IMAGES], db); if (ret < 0) goto images_init_error; - ret = score_init(&afs_tables[TBLNUM_SCORES]); + ret = score_init(&afs_tables[TBLNUM_SCORES], db); if (ret < 0) goto score_init_error; - ret = aft_init(&afs_tables[TBLNUM_AUDIO_FILES]); + ret = aft_init(&afs_tables[TBLNUM_AUDIO_FILES], db); if (ret < 0) goto aft_init_error; - - current_play_mode = init_admissible_files(); - register_tasks(cookie); - s.default_timeout.tv_sec = 0; - s.default_timeout.tv_usec = 99 * 1000; - sched(&s); + if (!conf.afs_database_dir_given) + free(db); + return 1; aft_init_error: score_shutdown(OSL_MARK_CLEAN); @@ -773,7 +776,32 @@ playlists_init_error: moods_shutdown(OSL_MARK_CLEAN); moods_init_error: attribute_shutdown(OSL_MARK_CLEAN); -attribute_init_error: +err: + if (!conf.afs_database_dir_given) + free(db); + return ret; +} + +__noreturn int afs_init(uint32_t cookie, int socket_fd) +{ + enum play_mode current_play_mode; + struct sched s; + int ret = open_afs_tables(); + + if (ret < 0) { + PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret)); + exit(EXIT_FAILURE); + } + server_socket = socket_fd; + PARA_INFO_LOG("server_socket: %d, afs_socket_cookie: %u\n", + server_socket, (unsigned) cookie); + current_play_mode = init_admissible_files(); + register_tasks(cookie); + s.default_timeout.tv_sec = 0; + s.default_timeout.tv_usec = 99 * 1000; + ret = sched(&s); + assert(ret < 0); + PARA_EMERG_LOG("%s\n", PARA_STRERROR(-ret)); exit(EXIT_FAILURE); } @@ -793,12 +821,16 @@ static int create_all_tables(void) return 1; } -/* TODO load tables after init */ -int com_init(__a_unused int fd, int argc, char * const * const argv) +int com_init(int fd, int argc, char * const * const argv) { int i, j, ret; - if (argc == 1) - return create_all_tables(); + + if (argc == 1) { + ret = create_all_tables(); + if (ret < 0) + return ret; + return open_afs_tables(); + } for (i = 1; i < argc; i++) { for (j = 0; j < NUM_AFS_TABLES; j++) { struct table_info *ti = afs_tables + j; @@ -807,7 +839,9 @@ int com_init(__a_unused int fd, int argc, char * const * const argv) continue; if (strcmp(argv[i], ti->desc->name)) continue; - PARA_NOTICE_LOG("creating table %s\n", argv[i]); + ret = send_va_buffer(fd, "creating table %s\n", argv[i]); + if (ret < 0) + return ret; ret = osl_create_table(ti->desc); if (ret < 0) return ret; @@ -816,5 +850,5 @@ int com_init(__a_unused int fd, int argc, char * const * const argv) if (j == NUM_AFS_TABLES) return -E_BAD_TABLE_NAME; } - return 1; + return open_afs_tables(); } diff --git a/afs.h b/afs.h index 22d70ad9..80134720 100644 --- a/afs.h +++ b/afs.h @@ -2,8 +2,6 @@ #include "osl.h" #include "hash.h" -#define DATABASE_DIR "/home/maan/tmp/osl" /* FIXME */ - struct afs_info { uint64_t last_played; uint64_t attributes; @@ -109,7 +107,7 @@ int open_next_audio_file(struct audio_file_data *afd); int close_audio_file(struct audio_file_data *afd); /* score */ -int score_init(struct table_info *ti); +int score_init(struct table_info *ti, const char *db); void score_shutdown(enum osl_close_flags flags); int admissible_file_loop(void *data, osl_rbtree_loop_func *func); int admissible_file_loop_reverse(void *data, osl_rbtree_loop_func *func); @@ -122,14 +120,14 @@ int score_delete(const struct osl_row *aft_row); int row_belongs_to_score_table(const struct osl_row *aft_row); /* attribute */ -int attribute_init(struct table_info *ti); +int attribute_init(struct table_info *ti, const char *db); void attribute_shutdown(enum osl_close_flags flags); void get_attribute_bitmap(uint64_t *atts, char *buf); int get_attribute_bitnum_by_name(const char *att_name, unsigned char *bitnum); int get_attribute_text(uint64_t *atts, const char *delim, char **text); /* aft */ -int aft_init(struct table_info *ti); +int aft_init(struct table_info *ti, const char *db); void aft_shutdown(enum osl_close_flags flags); int aft_get_row_of_path(char *path, struct osl_row **row); int open_and_update_audio_file(struct osl_row *aft_row, struct audio_file_data *afd); @@ -158,7 +156,7 @@ int playlist_update_audio_file(struct osl_row *aft_row); #define DECLARE_BLOB_SYMBOLS(table_name, cmd_prefix) \ - int table_name ## _init(struct table_info *ti); \ + int table_name ## _init(struct table_info *ti, const char *db); \ void table_name ## _shutdown(enum osl_close_flags flags); \ int cmd_prefix ## _get_name_by_id(uint32_t id, char **name); \ extern struct osl_table *table_name ## _table; @@ -170,8 +168,7 @@ DECLARE_BLOB_SYMBOLS(playlists, pl); enum blob_table_columns {BLOBCOL_ID, BLOBCOL_NAME, BLOBCOL_DEF, NUM_BLOB_COLUMNS}; #define DEFINE_BLOB_TABLE_DESC(table_name) \ - const struct osl_table_description table_name ## _table_desc = { \ - .dir = DATABASE_DIR, \ + struct osl_table_description table_name ## _table_desc = { \ .name = #table_name, \ .num_columns = NUM_BLOB_COLUMNS, \ .flags = OSL_LARGE_TABLE, \ diff --git a/aft.c b/aft.c index 1d768071..3c33aae0 100644 --- a/aft.c +++ b/aft.c @@ -126,8 +126,7 @@ static struct osl_column_description aft_cols[] = { } }; -static const struct osl_table_description audio_file_table_desc = { - .dir = DATABASE_DIR, +static struct osl_table_description audio_file_table_desc = { .name = "audio_files", .num_columns = NUM_AFT_COLUMNS, .flags = OSL_LARGE_TABLE, @@ -1712,16 +1711,18 @@ void aft_shutdown(enum osl_close_flags flags) /** * Open the audio file table. * - * \param ti Gets initialized by this function + * \param ti Gets initialized by this function. + * \param db The database directory. * * \return Positive on success, negative on errors. * * \sa osl_open_table(). */ -int aft_init(struct table_info *ti) +int aft_init(struct table_info *ti, const char *db) { int ret; + audio_file_table_desc.dir = db; ti->desc = &audio_file_table_desc; ret = osl_open_table(ti->desc, &ti->table); if (ret >= 0) { diff --git a/attribute.c b/attribute.c index 925795be..af3dfb06 100644 --- a/attribute.c +++ b/attribute.c @@ -37,8 +37,7 @@ static struct osl_column_description att_cols[] = { } }; -static const struct osl_table_description attribute_table_desc = { - .dir = DATABASE_DIR, +static struct osl_table_description attribute_table_desc = { .name = "attributes", .num_columns = NUM_ATT_COLUMNS, .flags = 0, @@ -380,10 +379,11 @@ void attribute_shutdown(enum osl_close_flags flags) osl_close_table(attribute_table, flags); } -int attribute_init(struct table_info *ti) +int attribute_init(struct table_info *ti, const char *db) { int ret; + attribute_table_desc.dir = db; ti->desc = &attribute_table_desc; ret = osl_open_table(ti->desc, &ti->table); greatest_att_bitnum = -1; /* no atts available */ diff --git a/blob.c b/blob.c index fa5134d7..55956450 100644 --- a/blob.c +++ b/blob.c @@ -352,11 +352,11 @@ static int blob_get_name_by_id(struct osl_table *table, uint32_t id, } static int blob_init(struct osl_table **table, - const struct osl_table_description *desc, - struct table_info *ti) + struct osl_table_description *desc, + struct table_info *ti, const char *db) { int ret; - + desc->dir = db; ti->desc = desc; ret = osl_open_table(ti->desc, &ti->table); if (ret >= 0) { @@ -369,10 +369,10 @@ static int blob_init(struct osl_table **table, /** Define the \p init function for this blob type. */ #define DEFINE_BLOB_INIT(table_name) \ - int table_name ## _init(struct table_info *ti) \ + int table_name ## _init(struct table_info *ti, const char *db) \ { \ return blob_init(&table_name ## _table, \ - &table_name ## _table_desc, ti); \ + &table_name ## _table_desc, ti, db); \ } diff --git a/score.c b/score.c index 6c15a0f5..86c6c4bb 100644 --- a/score.c +++ b/score.c @@ -78,7 +78,6 @@ static struct osl_column_description score_cols[] = { }; static struct osl_table_description score_table_desc = { - .dir = DATABASE_DIR, .name = "score", .num_columns = NUM_SCORE_COLUMNS, .flags = 0, @@ -349,13 +348,15 @@ void score_shutdown(enum osl_close_flags flags) * Init the scoring subsystem. * * \param ti Gets filled in by the function. + * \param db The database directory. * * \return The return value of the underlying call to osl_open_table(). * * \sa score_shutdown(). */ -int score_init(struct table_info *ti) +int score_init(struct table_info *ti, const char *db) { + score_table_desc.dir = db; ti->desc = &score_table_desc; ti->flags = TBLFLAG_SKIP_CREATE; int ret = osl_open_table(ti->desc, &ti->table); diff --git a/server.ggo b/server.ggo index dedb0e86..54aea85a 100644 --- a/server.ggo +++ b/server.ggo @@ -132,6 +132,20 @@ option "selector" S string typestr="name" optional +############################# +section "audio file selector" +############################# + +option "afs_database_dir" D +#~~~~~~~~~~~~~~~~~~~~~~~~~~ + +"Directory containing the osl database of the +audio file selector. +(default='~/.paraslash/afs_database'" + + string typestr="path" + optional + option "afs_socket" s #~~~~~~~~~~~~~~~~~~~~ -- 2.39.5