src/valhalla.c
author Mathieu Schroeter <mathieu.schroeter@mycable.ch>
Tue Feb 09 09:24:36 2010 +0100 (2 hours ago)
changeset 907 a2554eed0e8a
parent 770b8fa653fe354
permissions -rw-r--r--
cosmetic: fix indent after previous commit
     1 /*
     2  * GeeXboX Valhalla: tiny media scanner API.
     3  * Copyright (C) 2009 Mathieu Schroeter <mathieu.schroeter@gamesover.ch>
     4  *
     5  * This file is part of libvalhalla.
     6  *
     7  * libvalhalla is free software; you can redistribute it and/or
     8  * modify it under the terms of the GNU Lesser General Public
     9  * License as published by the Free Software Foundation; either
    10  * version 2.1 of the License, or (at your option) any later version.
    11  *
    12  * libvalhalla is distributed in the hope that it will be useful,
    13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
    15  * Lesser General Public License for more details.
    16  *
    17  * You should have received a copy of the GNU Lesser General Public
    18  * License along with libvalhalla; if not, write to the Free Software
    19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
    20  */
    21 
    22 #include <pthread.h>
    23 #include <stdlib.h>
    24 #include <string.h>
    25 
    26 #ifdef USE_LAVC
    27 #include <libavcodec/avcodec.h>
    28 #endif /* USE_LAVC */
    29 #include <libavformat/avformat.h>
    30 
    31 #include "valhalla.h"
    32 #include "valhalla_internals.h"
    33 #include "parser.h"
    34 #include "scanner.h"
    35 #include "dbmanager.h"
    36 #include "dispatcher.h"
    37 #include "ondemand.h"
    38 #include "event_handler.h"
    39 #include "utils.h"
    40 #include "stats.h"
    41 #include "metadata.h"
    42 #include "logs.h"
    43 
    44 #ifdef USE_GRABBER
    45 #include "grabber.h"
    46 #include "downloader.h"
    47 #include "url_utils.h"
    48 #endif /* USE_GRABBER */
    49 
    50 
    51 unsigned int
    52 libvalhalla_version (void)
    53 {
    54   return LIBVALHALLA_VERSION_INT;
    55 }
    56 
    57 /******************************************************************************/
    58 /*                                                                            */
    59 /*                             Valhalla Handling                              */
    60 /*                                                                            */
    61 /******************************************************************************/
    62 
    63 static void
    64 queue_cleanup (fifo_queue_t *queue)
    65 {
    66   int e;
    67   void *data;
    68 
    69   vh_fifo_queue_push (queue,
    70                       FIFO_QUEUE_PRIORITY_NORMAL, ACTION_CLEANUP_END, NULL);
    71 
    72   do
    73   {
    74     e = ACTION_NO_OPERATION;
    75     data = NULL;
    76     vh_fifo_queue_pop (queue, &e, &data);
    77 
    78     switch (e)
    79     {
    80     default:
    81       break;
    82 
    83     case ACTION_DB_INSERT_P:
    84     case ACTION_DB_INSERT_G:
    85     case ACTION_DB_UPDATE_P:
    86     case ACTION_DB_UPDATE_G:
    87     case ACTION_DB_END:
    88     case ACTION_DB_NEWFILE:
    89       if (data)
    90         vh_file_data_free (data);
    91       break;
    92 
    93     case ACTION_DB_EXT_INSERT:
    94     case ACTION_DB_EXT_UPDATE:
    95     case ACTION_DB_EXT_DELETE:
    96       if (data)
    97         vh_dbmanager_extmd_free (data);
    98       break;
    99 
   100     case ACTION_OD_ENGAGE:
   101     case ACTION_EH_EVENTGL:
   102       if (data)
   103         free (data);
   104       break;
   105 
   106     case ACTION_EH_EVENTOD:
   107       if (data)
   108         vh_event_handler_od_free (data);
   109       break;
   110 
   111     case ACTION_EH_EVENTMD:
   112       if (data)
   113         vh_event_handler_md_free (data);
   114       break;
   115     }
   116   }
   117   while (e != ACTION_CLEANUP_END);
   118 }
   119 
   120 static void
   121 valhalla_mrproper (valhalla_t *handle)
   122 {
   123   unsigned int i;
   124   fifo_queue_t *fifo_o;
   125 
   126   fifo_queue_t *fifo_i[] = {
   127     vh_scanner_fifo_get (handle->scanner),
   128     vh_dbmanager_fifo_get (handle->dbmanager),
   129     vh_dispatcher_fifo_get (handle->dispatcher),
   130     vh_parser_fifo_get (handle->parser),
   131 #ifdef USE_GRABBER
   132     vh_grabber_fifo_get (handle->grabber),
   133     vh_downloader_fifo_get (handle->downloader),
   134 #endif /* USE_GRABBER */
   135     vh_event_handler_fifo_get (handle->event_handler),
   136   };
   137 
   138   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   139 
   140   if (!handle)
   141     return;
   142 
   143   fifo_o = vh_fifo_queue_new ();
   144   if (!fifo_o)
   145     return;
   146 
   147 #ifdef USE_GRABBER
   148   vh_dbmanager_db_begin_transaction (handle->dbmanager);
   149 
   150   /* remove all previous contexts */
   151   vh_dbmanager_db_dlcontext_delete (handle->dbmanager);
   152 #endif /* USE_GRABBER */
   153 
   154   /*
   155    * The same data pointer can exist in several queues at the same time.
   156    * The goal is to identify all unique entries from all queues and to save
   157    * these entries in the fifo_o queue.
   158    * Then, all data pointers can be safety freed (prevents double free).
   159    */
   160   for (i = 0; i < ARRAY_NB_ELEMENTS (fifo_i); i++)
   161   {
   162     int e;
   163     void *data;
   164 
   165     if (!fifo_i[i])
   166       continue;
   167 
   168     vh_fifo_queue_push (fifo_i[i],
   169                         FIFO_QUEUE_PRIORITY_NORMAL, ACTION_CLEANUP_END, NULL);
   170 
   171     do
   172     {
   173       e = ACTION_NO_OPERATION;
   174       data = NULL;
   175       vh_fifo_queue_pop (fifo_i[i], &e, &data);
   176 
   177       switch (e)
   178       {
   179       case ACTION_DB_INSERT_P:
   180       case ACTION_DB_INSERT_G:
   181       case ACTION_DB_UPDATE_P:
   182       case ACTION_DB_UPDATE_G:
   183       case ACTION_DB_END:
   184       case ACTION_DB_NEWFILE:
   185       {
   186         file_data_t *file = data;
   187         if (!file || file->clean_f)
   188           break;
   189 
   190         file->clean_f = 1;
   191         vh_fifo_queue_push (fifo_o, FIFO_QUEUE_PRIORITY_NORMAL, e, data);
   192 
   193 #ifdef USE_GRABBER
   194         /* save downloader context */
   195         if (file->step < STEP_ENDING && file->list_downloader)
   196           vh_dbmanager_db_dlcontext_save (handle->dbmanager, file);
   197 #endif /* USE_GRABBER */
   198         break;
   199       }
   200 
   201       case ACTION_DB_EXT_INSERT:
   202       case ACTION_DB_EXT_UPDATE:
   203       case ACTION_DB_EXT_DELETE:
   204       case ACTION_EH_EVENTOD:
   205       case ACTION_EH_EVENTMD:
   206       case ACTION_EH_EVENTGL:
   207         vh_fifo_queue_push (fifo_o, FIFO_QUEUE_PRIORITY_NORMAL, e, data);
   208         break;
   209 
   210       default:
   211         break;
   212       }
   213     }
   214     while (e != ACTION_CLEANUP_END);
   215   }
   216 
   217 #ifdef USE_GRABBER
   218   vh_dbmanager_db_end_transaction (handle->dbmanager);
   219 #endif /* USE_GRABBER */
   220 
   221   queue_cleanup (fifo_o);
   222   vh_fifo_queue_free (fifo_o);
   223 
   224   /* On-demand queue must be handled separately. */
   225   fifo_o = vh_ondemand_fifo_get (handle->ondemand);
   226   if (!fifo_o)
   227     return;
   228   queue_cleanup (fifo_o);
   229 }
   230 
   231 int
   232 valhalla_config_set_orig (valhalla_t *handle, valhalla_cfg_t conf, ...)
   233 {
   234   int res = 0;
   235   const void *p1 = NULL, *p2 = NULL;
   236   int i = 0;
   237 
   238   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   239 
   240   if (!handle)
   241     return -1;
   242 
   243   if (conf >= (1 << VH_CFG_RANGE))
   244   {
   245     va_list ap;
   246 
   247     va_start (ap, conf);
   248 
   249     if (conf & VH_VOIDP_T)
   250       p1 = va_arg (ap, void *);
   251     if (conf & VH_INT_T)
   252       i  = va_arg (ap, int);
   253     if (conf & VH_VOIDP_2_T)
   254       p2 = va_arg (ap, void *);
   255 
   256     if (va_arg (ap, int) != ~0) /* check for safeguard */
   257     {
   258       vh_log (VALHALLA_MSG_CRITICAL,
   259               "unrecoverable error with valhalla_config_set(), conf = %#x, "
   260               "it is probably a bad use of this function", conf);
   261       res = -2;
   262     }
   263 
   264     va_end (ap);
   265   }
   266 
   267   if (res)
   268     return res;
   269 
   270   switch (conf)
   271   {
   272 #ifdef USE_GRABBER
   273   case VALHALLA_CFG_DOWNLOADER_DEST:
   274     vh_downloader_destination_set (handle->downloader, (valhalla_dl_t) i, p1);
   275     break;
   276 
   277   case VALHALLA_CFG_GRABBER_PRIORITY:
   278     vh_grabber_priority_set (handle->grabber,
   279                              p1, (valhalla_metadata_pl_t) i, p2);
   280     break;
   281 
   282   case VALHALLA_CFG_GRABBER_STATE:
   283     if (p1)
   284       vh_grabber_state_set (handle->grabber, p1, i);
   285     break;
   286 #endif /* USE_GRABBER */
   287 
   288   case VALHALLA_CFG_PARSER_KEYWORD:
   289     if (p1)
   290       vh_parser_bl_keyword_add (handle->parser, p1);
   291     break;
   292 
   293   case VALHALLA_CFG_SCANNER_PATH:
   294     if (p1)
   295       vh_scanner_path_add (handle->scanner, p1, i);
   296     break;
   297 
   298   case VALHALLA_CFG_SCANNER_SUFFIX:
   299     if (p1)
   300       vh_scanner_suffix_add (handle->scanner, p1);
   301     break;
   302 
   303   default:
   304     vh_log (VALHALLA_MSG_WARNING,
   305             "%s: unsupported option %#x", __FUNCTION__, conf);
   306     res = -1;
   307     break;
   308   }
   309 
   310   return res;
   311 }
   312 
   313 void
   314 valhalla_wait (valhalla_t *handle)
   315 {
   316   const int f = STOP_FLAG_REQUEST | STOP_FLAG_WAIT;
   317 
   318   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   319 
   320   if (!handle || handle->noscan)
   321     return;
   322 
   323   vh_scanner_wait (handle->scanner);
   324 
   325   vh_ondemand_stop (handle->ondemand, f);
   326   vh_dbmanager_wait (handle->dbmanager);
   327   vh_dispatcher_stop (handle->dispatcher, f);
   328   vh_parser_stop (handle->parser, f);
   329 #ifdef USE_GRABBER
   330   vh_grabber_stop (handle->grabber, f);
   331   vh_downloader_stop (handle->downloader, f);
   332 #endif /* USE_GRABBER */
   333   vh_event_handler_stop (handle->event_handler, f);
   334 
   335   handle->fstop = 1;
   336 }
   337 
   338 static void
   339 valhalla_force_stop (valhalla_t *handle)
   340 {
   341   unsigned int i;
   342 
   343   vh_log (VALHALLA_MSG_WARNING,
   344           "%s: This can take a time, please be patient", __FUNCTION__);
   345 
   346   for (i = 0; i < 2; i++)
   347   {
   348     int f = !i ? STOP_FLAG_REQUEST : STOP_FLAG_WAIT;
   349 
   350     vh_ondemand_stop (handle->ondemand, f);
   351     if (!handle->noscan)
   352       vh_scanner_stop (handle->scanner, f);
   353     vh_dbmanager_stop (handle->dbmanager, f);
   354     vh_dispatcher_stop (handle->dispatcher, f);
   355     vh_parser_stop (handle->parser, f);
   356 #ifdef USE_GRABBER
   357     vh_grabber_stop (handle->grabber, f);
   358     vh_downloader_stop (handle->downloader, f);
   359 #endif /* USE_GRABBER */
   360     vh_event_handler_stop (handle->event_handler, f);
   361   }
   362 
   363   valhalla_mrproper (handle);
   364 
   365   handle->fstop = 1;
   366 }
   367 
   368 void
   369 valhalla_uninit (valhalla_t *handle)
   370 {
   371   vh_log (VALHALLA_MSG_VERBOSE, "%s: begin", __FUNCTION__);
   372 
   373   if (!handle)
   374     return;
   375 
   376   if (!handle->fstop)
   377     valhalla_force_stop (handle);
   378 
   379   /* dump all statistics */
   380   vh_stats_dump (handle->stats, NULL);
   381   vh_stats_debug_dump (handle->stats);
   382 
   383   vh_ondemand_uninit (handle->ondemand);
   384   vh_scanner_uninit (handle->scanner);
   385   vh_dbmanager_uninit (handle->dbmanager);
   386   vh_dispatcher_uninit (handle->dispatcher);
   387   vh_parser_uninit (handle->parser);
   388 #ifdef USE_GRABBER
   389   vh_grabber_uninit (handle->grabber);
   390   vh_downloader_uninit (handle->downloader);
   391 #endif /* USE_GRABBER */
   392   vh_event_handler_uninit (handle->event_handler);
   393 
   394 #if USE_GRABBER
   395   vh_url_global_uninit ();
   396 #endif /* USE_GRABBER */
   397 
   398 #ifdef USE_LAVC
   399   av_lockmgr_register (NULL);
   400 #endif /* USE_LAVC */
   401 
   402   vh_stats_free (handle->stats);
   403 
   404   vh_log (VALHALLA_MSG_VERBOSE, "%s: end", __FUNCTION__);
   405 
   406   free (handle);
   407 }
   408 
   409 int
   410 valhalla_run (valhalla_t *handle, int loop, uint16_t timeout, int priority)
   411 {
   412   int res;
   413 
   414   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   415 
   416   if (!handle)
   417     return VALHALLA_ERROR_HANDLER;
   418 
   419   if (handle->run)
   420     return VALHALLA_ERROR_DEAD;
   421 
   422   handle->run = 1;
   423 
   424   if (handle->event_handler)
   425   {
   426     res = vh_event_handler_run (handle->event_handler, priority);
   427     if (res)
   428       return VALHALLA_ERROR_THREAD;
   429   }
   430 
   431   res = vh_scanner_run (handle->scanner, loop, timeout, priority);
   432   if (res)
   433   {
   434     if (res == SCANNER_ERROR_PATH)
   435     {
   436       handle->noscan = 1;
   437       vh_log (VALHALLA_MSG_INFO , "no path defined, scanner disabled");
   438     }
   439     else
   440       return VALHALLA_ERROR_THREAD;
   441   }
   442 
   443   res = vh_dbmanager_run (handle->dbmanager, priority);
   444   if (res)
   445     return VALHALLA_ERROR_THREAD;
   446 
   447   res = vh_dispatcher_run (handle->dispatcher, priority);
   448   if (res)
   449     return VALHALLA_ERROR_THREAD;
   450 
   451   res = vh_parser_run (handle->parser, priority);
   452   if (res)
   453     return VALHALLA_ERROR_THREAD;
   454 
   455 #ifdef USE_GRABBER
   456   res = vh_grabber_run (handle->grabber, priority);
   457   if (res)
   458     return VALHALLA_ERROR_THREAD;
   459 
   460   res = vh_downloader_run (handle->downloader, priority);
   461   if (res)
   462     return VALHALLA_ERROR_THREAD;
   463 #endif /* USE_GRABBER */
   464 
   465   res = vh_ondemand_run (handle->ondemand, priority);
   466   if (res)
   467     return VALHALLA_ERROR_THREAD;
   468 
   469   return VALHALLA_SUCCESS;
   470 }
   471 
   472 const char *
   473 valhalla_metadata_group_str (valhalla_meta_grp_t group)
   474 {
   475   return vh_metadata_group_str (group);
   476 }
   477 
   478 const char *
   479 valhalla_grabber_next (valhalla_t *handle, const char *id)
   480 {
   481   vh_log (VALHALLA_MSG_VERBOSE, "%s : %s", __FUNCTION__, id ? id : "");
   482 
   483   if (!handle)
   484     return NULL;
   485 
   486 #ifdef USE_GRABBER
   487   return vh_grabber_next (handle->grabber, id);
   488 #else
   489   vh_log (VALHALLA_MSG_WARNING,
   490           "This function is usable only with grabbing support!");
   491   return NULL;
   492 #endif /* USE_GRABBER */
   493 }
   494 
   495 valhalla_metadata_pl_t
   496 valhalla_grabber_priority_read (valhalla_t *handle,
   497                                 const char *id, const char **meta)
   498 {
   499   vh_log (VALHALLA_MSG_VERBOSE,
   500           "%s : %s/%s", __FUNCTION__, id ? id : "", meta && *meta ? *meta : "");
   501 
   502   if (!handle)
   503     return 0;
   504 
   505 #ifdef USE_GRABBER
   506   return vh_grabber_priority_read (handle->grabber, id, meta);
   507 #else
   508   vh_log (VALHALLA_MSG_WARNING,
   509           "This function is usable only with grabbing support!");
   510   return 0;
   511 #endif /* USE_GRABBER */
   512 }
   513 
   514 const char *
   515 valhalla_stats_group_next (valhalla_t *handle, const char *id)
   516 {
   517   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   518 
   519   if (!handle)
   520     return NULL;
   521 
   522   return vh_stats_group_next (handle->stats, id);
   523 }
   524 
   525 unsigned long
   526 valhalla_stats_read_next (valhalla_t *handle, const char *id,
   527                           valhalla_stats_type_t type, const char **item)
   528 {
   529   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   530 
   531   if (!handle)
   532     return 0;
   533 
   534   return vh_stats_read_next (handle->stats, id, type, item);
   535 }
   536 
   537 void
   538 valhalla_verbosity (valhalla_verb_t level)
   539 {
   540   vh_log_verb (level);
   541 }
   542 
   543 #ifdef USE_LAVC
   544 static int
   545 valhalla_avlock (void **mutex, enum AVLockOp op)
   546 {
   547   switch (op)
   548   {
   549   case AV_LOCK_CREATE:
   550     *mutex = malloc (sizeof (pthread_mutex_t));
   551     if (!*mutex)
   552       return 1;
   553     return !!pthread_mutex_init (*mutex, NULL);
   554 
   555   case AV_LOCK_OBTAIN:
   556     return !!pthread_mutex_lock (*mutex);
   557 
   558   case AV_LOCK_RELEASE:
   559     return !!pthread_mutex_unlock (*mutex);
   560 
   561   case AV_LOCK_DESTROY:
   562     pthread_mutex_destroy (*mutex);
   563     free (*mutex);
   564     return 0;
   565 
   566   default:
   567     return 1;
   568   }
   569 }
   570 #endif /* USE_LAVC */
   571 
   572 valhalla_t *
   573 valhalla_init (const char *db, valhalla_init_param_t *param)
   574 {
   575   static int preinit = 0;
   576   valhalla_t *handle;
   577   valhalla_init_param_t p;
   578   const valhalla_init_param_t *pp = &p;
   579 
   580   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   581 
   582   if (!db)
   583     return NULL;
   584 
   585   if (param)
   586     pp = param;
   587   else
   588     memset (&p, 0, sizeof (p));
   589 
   590   handle = calloc (1, sizeof (valhalla_t));
   591   if (!handle)
   592     return NULL;
   593 
   594 #ifdef USE_GRABBER
   595   vh_url_global_init ();
   596 #endif /* USE_GRABBER */
   597 
   598   handle->stats = vh_stats_new ();
   599   if (!handle->stats)
   600     goto err;
   601 
   602   if (pp->od_cb || pp->gl_cb || pp->md_cb)
   603   {
   604     event_handler_cb_t cb;
   605 
   606     cb.od_cb = pp->od_cb;
   607     cb.gl_cb = pp->gl_cb;
   608     cb.md_cb = pp->md_cb;
   609     cb.od_data = pp->od_data;
   610     cb.gl_data = pp->gl_data;
   611     cb.md_data = pp->md_data;
   612 
   613     handle->event_handler = vh_event_handler_init (handle, &cb);
   614     if (!handle->event_handler)
   615       goto err;
   616   }
   617 
   618   handle->dispatcher = vh_dispatcher_init (handle);
   619   if (!handle->dispatcher)
   620     goto err;
   621 
   622   handle->parser = vh_parser_init (handle, pp->parser_nb, pp->decrapifier);
   623   if (!handle->parser)
   624     goto err;
   625 
   626 #ifdef USE_GRABBER
   627   handle->grabber = vh_grabber_init (handle, pp->grabber_nb);
   628   if (!handle->grabber)
   629     goto err;
   630 
   631   handle->downloader = vh_downloader_init (handle);
   632   if (!handle->downloader)
   633     goto err;
   634 #endif /* USE_GRABBER */
   635 
   636   handle->scanner = vh_scanner_init (handle);
   637   if (!handle->scanner)
   638     goto err;
   639 
   640   handle->dbmanager = vh_dbmanager_init (handle, db, pp->commit_int);
   641   if (!handle->dbmanager)
   642     goto err;
   643 
   644   handle->ondemand = vh_ondemand_init (handle);
   645   if (!handle->ondemand)
   646     goto err;
   647 
   648   if (!preinit)
   649   {
   650 #ifdef USE_LAVC
   651     if (av_lockmgr_register (valhalla_avlock))
   652       goto err;
   653 #endif /* USE_LAVC */
   654     av_log_set_level (AV_LOG_FATAL);
   655     av_register_all ();
   656     preinit = 1;
   657   }
   658 
   659   return handle;
   660 
   661  err:
   662   valhalla_uninit (handle);
   663   return NULL;
   664 }
   665 
   666 void
   667 valhalla_scanner_wakeup (valhalla_t *handle)
   668 {
   669   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   670 
   671   if (!handle)
   672     return;
   673 
   674   vh_scanner_wakeup (handle->scanner);
   675 }
   676 
   677 void
   678 valhalla_ondemand (valhalla_t *handle, const char *file)
   679 {
   680   char *odfile;
   681 
   682   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   683 
   684   if (!handle || !file)
   685     return;
   686 
   687   odfile = strdup (file);
   688   if (!odfile)
   689     return;
   690 
   691   vh_ondemand_action_send (handle->ondemand, FIFO_QUEUE_PRIORITY_HIGH,
   692                            ACTION_OD_ENGAGE, odfile);
   693 }
   694 
   695 /******************************************************************************/
   696 /*                                                                            */
   697 /*                         Public Database Selections                         */
   698 /*                                                                            */
   699 /******************************************************************************/
   700 
   701 int valhalla_db_metalist_get (valhalla_t *handle,
   702                               valhalla_db_item_t *search,
   703                               valhalla_file_type_t filetype,
   704                               valhalla_db_restrict_t *restriction,
   705                               int (*result_cb) (void *data,
   706                                                 valhalla_db_metares_t *res),
   707                               void *data)
   708 {
   709   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   710 
   711   if (!handle || !search || !result_cb)
   712     return -1;
   713 
   714   return vh_dbmanager_db_metalist_get (handle->dbmanager, search,
   715                                        filetype, restriction, result_cb, data);
   716 }
   717 
   718 int valhalla_db_filelist_get (valhalla_t *handle,
   719                               valhalla_file_type_t filetype,
   720                               valhalla_db_restrict_t *restriction,
   721                               int (*result_cb) (void *data,
   722                                                 valhalla_db_fileres_t *res),
   723                               void *data)
   724 {
   725   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   726 
   727   if (!handle || !result_cb)
   728     return -1;
   729 
   730   return vh_dbmanager_db_filelist_get (handle->dbmanager,
   731                                        filetype, restriction, result_cb, data);
   732 }
   733 
   734 int
   735 valhalla_db_file_get (valhalla_t *handle,
   736                       int64_t id, const char *path,
   737                       valhalla_db_restrict_t *restriction,
   738                       valhalla_db_filemeta_t **res)
   739 {
   740   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   741 
   742   if (!handle || !res)
   743     return -1;
   744 
   745   return
   746     vh_dbmanager_db_file_get (handle->dbmanager, id, path, restriction, res);
   747 }
   748 
   749 /******************************************************************************/
   750 /*                                                                            */
   751 /*                  For Public Insertions/Updates/Deletions                   */
   752 /*                                                                            */
   753 /******************************************************************************/
   754 
   755 int
   756 valhalla_db_metadata_insert (valhalla_t *handle, const char *path,
   757                              const char *meta, const char *data,
   758                              valhalla_meta_grp_t group)
   759 {
   760   dbmanager_extmd_t *extmd;
   761 
   762   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   763 
   764   if (!handle || !path || !meta || !data)
   765     return -1;
   766 
   767   extmd = calloc (1, sizeof (dbmanager_extmd_t));
   768   if (!extmd)
   769     return -1;
   770 
   771   extmd->path  = strdup (path);
   772   extmd->meta  = strdup (meta);
   773   extmd->data  = strdup (data);
   774   extmd->group = group;
   775 
   776   vh_dbmanager_action_send (handle->dbmanager, FIFO_QUEUE_PRIORITY_HIGH,
   777                             ACTION_DB_EXT_INSERT, extmd);
   778   return 0;
   779 }
   780 
   781 int
   782 valhalla_db_metadata_update (valhalla_t *handle, const char *path,
   783                              const char *meta, const char *data,
   784                              const char *ndata)
   785 {
   786   dbmanager_extmd_t *extmd;
   787 
   788   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   789 
   790   if (!handle || !path || !meta || !data || !ndata)
   791     return -1;
   792 
   793   extmd = calloc (1, sizeof (dbmanager_extmd_t));
   794   if (!extmd)
   795     return -1;
   796 
   797   extmd->path  = strdup (path);
   798   extmd->meta  = strdup (meta);
   799   extmd->data  = strdup (data);
   800   extmd->ndata = strdup (ndata);
   801 
   802   vh_dbmanager_action_send (handle->dbmanager, FIFO_QUEUE_PRIORITY_HIGH,
   803                             ACTION_DB_EXT_UPDATE, extmd);
   804   return 0;
   805 }
   806 
   807 int
   808 valhalla_db_metadata_delete (valhalla_t *handle, const char *path,
   809                              const char *meta, const char *data)
   810 {
   811   dbmanager_extmd_t *extmd;
   812 
   813   vh_log (VALHALLA_MSG_VERBOSE, __FUNCTION__);
   814 
   815   if (!handle || !path || !meta || !data)
   816     return -1;
   817 
   818   extmd = calloc (1, sizeof (dbmanager_extmd_t));
   819   if (!extmd)
   820     return -1;
   821 
   822   extmd->path  = strdup (path);
   823   extmd->meta  = strdup (meta);
   824   extmd->data  = strdup (data);
   825 
   826   vh_dbmanager_action_send (handle->dbmanager, FIFO_QUEUE_PRIORITY_HIGH,
   827                             ACTION_DB_EXT_DELETE, extmd);
   828   return 0;
   829 }