avio_list_dir.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. /*
  2. * Copyright (c) 2014 Lukasz Marek
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy
  5. * of this software and associated documentation files (the "Software"), to deal
  6. * in the Software without restriction, including without limitation the rights
  7. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. * copies of the Software, and to permit persons to whom the Software is
  9. * furnished to do so, subject to the following conditions:
  10. *
  11. * The above copyright notice and this permission notice shall be included in
  12. * all copies or substantial portions of the Software.
  13. *
  14. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  17. * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. * THE SOFTWARE.
  21. */
  22. /**
  23. * @file libavformat AVIOContext list directory API usage example
  24. * @example avio_list_dir.c
  25. *
  26. * Show how to list directories through the libavformat AVIOContext API.
  27. */
  28. #include <libavcodec/avcodec.h>
  29. #include <libavformat/avformat.h>
  30. #include <libavformat/avio.h>
  31. static const char *type_string(int type)
  32. {
  33. switch (type) {
  34. case AVIO_ENTRY_DIRECTORY:
  35. return "<DIR>";
  36. case AVIO_ENTRY_FILE:
  37. return "<FILE>";
  38. case AVIO_ENTRY_BLOCK_DEVICE:
  39. return "<BLOCK DEVICE>";
  40. case AVIO_ENTRY_CHARACTER_DEVICE:
  41. return "<CHARACTER DEVICE>";
  42. case AVIO_ENTRY_NAMED_PIPE:
  43. return "<PIPE>";
  44. case AVIO_ENTRY_SYMBOLIC_LINK:
  45. return "<LINK>";
  46. case AVIO_ENTRY_SOCKET:
  47. return "<SOCKET>";
  48. case AVIO_ENTRY_SERVER:
  49. return "<SERVER>";
  50. case AVIO_ENTRY_SHARE:
  51. return "<SHARE>";
  52. case AVIO_ENTRY_WORKGROUP:
  53. return "<WORKGROUP>";
  54. case AVIO_ENTRY_UNKNOWN:
  55. default:
  56. break;
  57. }
  58. return "<UNKNOWN>";
  59. }
  60. static int list_op(const char *input_dir)
  61. {
  62. AVIODirEntry *entry = NULL;
  63. AVIODirContext *ctx = NULL;
  64. int cnt, ret;
  65. char filemode[4], uid_and_gid[20];
  66. if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) {
  67. av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
  68. goto fail;
  69. }
  70. cnt = 0;
  71. for (;;) {
  72. if ((ret = avio_read_dir(ctx, &entry)) < 0) {
  73. av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret));
  74. goto fail;
  75. }
  76. if (!entry)
  77. break;
  78. if (entry->filemode == -1) {
  79. snprintf(filemode, 4, "???");
  80. } else {
  81. snprintf(filemode, 4, "%3"PRIo64, entry->filemode);
  82. }
  83. snprintf(uid_and_gid, 20, "%"PRId64"(%"PRId64")", entry->user_id, entry->group_id);
  84. if (cnt == 0)
  85. av_log(NULL, AV_LOG_INFO, "%-9s %12s %30s %10s %s %16s %16s %16s\n",
  86. "TYPE", "SIZE", "NAME", "UID(GID)", "UGO", "MODIFIED",
  87. "ACCESSED", "STATUS_CHANGED");
  88. av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %10s %s %16"PRId64" %16"PRId64" %16"PRId64"\n",
  89. type_string(entry->type),
  90. entry->size,
  91. entry->name,
  92. uid_and_gid,
  93. filemode,
  94. entry->modification_timestamp,
  95. entry->access_timestamp,
  96. entry->status_change_timestamp);
  97. avio_free_directory_entry(&entry);
  98. cnt++;
  99. };
  100. fail:
  101. avio_close_dir(&ctx);
  102. return ret;
  103. }
  104. static void usage(const char *program_name)
  105. {
  106. fprintf(stderr, "usage: %s input_dir\n"
  107. "API example program to show how to list files in directory "
  108. "accessed through AVIOContext.\n", program_name);
  109. }
  110. int main(int argc, char *argv[])
  111. {
  112. int ret;
  113. av_log_set_level(AV_LOG_DEBUG);
  114. if (argc < 2) {
  115. usage(argv[0]);
  116. return 1;
  117. }
  118. avformat_network_init();
  119. ret = list_op(argv[1]);
  120. avformat_network_deinit();
  121. return ret < 0 ? 1 : 0;
  122. }