datadir.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. /*
  2. * QEMU firmware and keymap file search
  3. *
  4. * Copyright (c) 2003-2020 QEMU contributors
  5. *
  6. * Permission is hereby granted, free of charge, to any person obtaining a copy
  7. * of this software and associated documentation files (the "Software"), to deal
  8. * in the Software without restriction, including without limitation the rights
  9. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. * copies of the Software, and to permit persons to whom the Software is
  11. * furnished to do so, subject to the following conditions:
  12. *
  13. * The above copyright notice and this permission notice shall be included in
  14. * all copies or substantial portions of the Software.
  15. *
  16. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  19. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. * THE SOFTWARE.
  23. */
  24. #include "qemu/osdep.h"
  25. #include "qemu/datadir.h"
  26. #include "qemu/cutils.h"
  27. #include "trace.h"
  28. static const char *data_dir[16];
  29. static int data_dir_idx;
  30. char *qemu_find_file(int type, const char *name)
  31. {
  32. int i;
  33. const char *subdir;
  34. char *buf;
  35. /* Try the name as a straight path first */
  36. if (access(name, R_OK) == 0) {
  37. trace_load_file(name, name);
  38. return g_strdup(name);
  39. }
  40. switch (type) {
  41. case QEMU_FILE_TYPE_BIOS:
  42. subdir = "";
  43. break;
  44. case QEMU_FILE_TYPE_KEYMAP:
  45. subdir = "keymaps/";
  46. break;
  47. default:
  48. abort();
  49. }
  50. for (i = 0; i < data_dir_idx; i++) {
  51. buf = g_strdup_printf("%s/%s%s", data_dir[i], subdir, name);
  52. if (access(buf, R_OK) == 0) {
  53. trace_load_file(name, buf);
  54. return buf;
  55. }
  56. g_free(buf);
  57. }
  58. return NULL;
  59. }
  60. void qemu_add_data_dir(char *path)
  61. {
  62. int i;
  63. if (path == NULL) {
  64. return;
  65. }
  66. if (data_dir_idx == ARRAY_SIZE(data_dir)) {
  67. return;
  68. }
  69. for (i = 0; i < data_dir_idx; i++) {
  70. if (strcmp(data_dir[i], path) == 0) {
  71. g_free(path); /* duplicate */
  72. return;
  73. }
  74. }
  75. data_dir[data_dir_idx++] = path;
  76. }
  77. void qemu_add_default_firmwarepath(void)
  78. {
  79. static const char * const dirs[] = {
  80. CONFIG_QEMU_FIRMWAREPATH
  81. NULL
  82. };
  83. size_t i;
  84. /* add configured firmware directories */
  85. for (i = 0; dirs[i] != NULL; i++) {
  86. qemu_add_data_dir(get_relocated_path(dirs[i]));
  87. }
  88. /* try to find datadir relative to the executable path */
  89. qemu_add_data_dir(get_relocated_path(CONFIG_QEMU_DATADIR));
  90. }
  91. void qemu_list_data_dirs(void)
  92. {
  93. int i;
  94. for (i = 0; i < data_dir_idx; i++) {
  95. printf("%s\n", data_dir[i]);
  96. }
  97. }