+/* must specify the ctx and tr in cfgwatch! */
+static int tr_read_and_apply_config(TR_CFGWATCH *cfgwatch)
+{
+ TALLOC_CTX *tmp_ctx=talloc_new(NULL);
+ char *config_dir=cfgwatch->config_dir;
+ int n_files = 0;
+ struct dirent **cfg_files=NULL;
+ TR_CFG_RC rc = TR_CFG_SUCCESS; /* presume success */
+ struct tr_fstat *new_fstat_list=NULL;
+ int retval=0;
+
+ /* find the configuration files -- n.b., tr_find_config_files()
+ * allocates memory to cfg_files which we must later free */
+ tr_debug("Reading configuration files from %s/", config_dir);
+ n_files = tr_find_config_files(config_dir, &cfg_files);
+ if (n_files <= 0) {
+ tr_crit("Can't locate configuration files, exiting.");
+ retval=1; goto cleanup;
+ }
+
+ /* Get the list of update times.
+ * Do this before loading in case they change between obtaining their timestamp
+ * and reading the file---this way they will immediately reload if this happens. */
+ new_fstat_list=tr_fstat_get_all(tmp_ctx, config_dir, cfg_files, n_files);
+ if (new_fstat_list==NULL) {
+ tr_crit("Could not allocate config file status list.");
+ retval=1; goto cleanup;
+ }
+
+ if (TR_CFG_SUCCESS != tr_parse_config(cfgwatch->tr, config_dir, n_files, cfg_files)) {
+ tr_crit("Error decoding configuration information.");
+ retval=1; goto cleanup;
+ }
+
+ /* apply initial configuration */
+ if (TR_CFG_SUCCESS != (rc = tr_apply_new_config(cfgwatch->tr))) {
+ tr_crit("Error applying configuration, rc = %d.", rc);
+ retval=1; goto cleanup;
+ }
+
+ /* give ownership of the new_fstat_list to caller's context */
+ if (cfgwatch->fstat_list != NULL) {
+ /* free the old one */
+ talloc_free(cfgwatch->fstat_list);
+ }
+ cfgwatch->n_files=n_files;
+ cfgwatch->fstat_list=new_fstat_list;
+ talloc_steal(cfgwatch->ctx, new_fstat_list);
+ new_fstat_list=NULL;
+
+ cleanup:
+ tr_free_config_file_list(n_files, &cfg_files);
+ talloc_free(tmp_ctx);
+ return retval;
+}
+
+