You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

121 lines
3.9KB

  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. #include <libavcodec/avcodec.h>
  23. #include <libavformat/avformat.h>
  24. #include <libavformat/avio.h>
  25. static const char *type_string(int type)
  26. {
  27. switch (type) {
  28. case AVIO_ENTRY_DIRECTORY:
  29. return "<DIR>";
  30. case AVIO_ENTRY_FILE:
  31. return "<FILE>";
  32. case AVIO_ENTRY_BLOCK_DEVICE:
  33. return "<BLOCK DEVICE>";
  34. case AVIO_ENTRY_CHARACTER_DEVICE:
  35. return "<CHARACTER DEVICE>";
  36. case AVIO_ENTRY_NAMED_PIPE:
  37. return "<PIPE>";
  38. case AVIO_ENTRY_SYMBOLIC_LINK:
  39. return "<LINK>";
  40. case AVIO_ENTRY_SOCKET:
  41. return "<SOCKET>";
  42. case AVIO_ENTRY_SERVER:
  43. return "<SERVER>";
  44. case AVIO_ENTRY_SHARE:
  45. return "<SHARE>";
  46. case AVIO_ENTRY_WORKGROUP:
  47. return "<WORKGROUP>";
  48. case AVIO_ENTRY_UNKNOWN:
  49. default:
  50. break;
  51. }
  52. return "<UNKNOWN>";
  53. }
  54. int main(int argc, char *argv[])
  55. {
  56. const char *input_dir = NULL;
  57. AVIODirEntry *entry = NULL;
  58. AVIODirContext *ctx = NULL;
  59. int cnt, ret;
  60. char filemode[4], uid_and_gid[20];
  61. av_log_set_level(AV_LOG_DEBUG);
  62. if (argc != 2) {
  63. fprintf(stderr, "usage: %s input_dir\n"
  64. "API example program to show how to list files in directory "
  65. "accessed through AVIOContext.\n", argv[0]);
  66. return 1;
  67. }
  68. input_dir = argv[1];
  69. /* register codecs and formats and other lavf/lavc components*/
  70. av_register_all();
  71. avformat_network_init();
  72. if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) {
  73. av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
  74. goto fail;
  75. }
  76. cnt = 0;
  77. for (;;) {
  78. if ((ret = avio_read_dir(ctx, &entry)) < 0) {
  79. av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret));
  80. goto fail;
  81. }
  82. if (!entry)
  83. break;
  84. if (entry->filemode == -1) {
  85. snprintf(filemode, 4, "???");
  86. } else {
  87. snprintf(filemode, 4, "%3"PRIo64, entry->filemode);
  88. }
  89. snprintf(uid_and_gid, 20, "%"PRId64"(%"PRId64")", entry->user_id, entry->group_id);
  90. if (cnt == 0)
  91. av_log(NULL, AV_LOG_INFO, "%-9s %12s %30s %10s %s %16s %16s %16s\n",
  92. "TYPE", "SIZE", "NAME", "UID(GID)", "UGO", "MODIFIED",
  93. "ACCESSED", "STATUS_CHANGED");
  94. av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %10s %s %16"PRId64" %16"PRId64" %16"PRId64"\n",
  95. type_string(entry->type),
  96. entry->size,
  97. entry->name,
  98. uid_and_gid,
  99. filemode,
  100. entry->modification_timestamp,
  101. entry->access_timestamp,
  102. entry->status_change_timestamp);
  103. avio_free_directory_entry(&entry);
  104. cnt++;
  105. };
  106. fail:
  107. avio_close_dir(&ctx);
  108. avformat_network_deinit();
  109. return ret < 0 ? 1 : 0;
  110. }