avio_dir_cmd.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. static int list_op(const char *input_dir)
  55. {
  56. AVIODirEntry *entry = NULL;
  57. AVIODirContext *ctx = NULL;
  58. int cnt, ret;
  59. char filemode[4], uid_and_gid[20];
  60. if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) {
  61. av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
  62. goto fail;
  63. }
  64. cnt = 0;
  65. for (;;) {
  66. if ((ret = avio_read_dir(ctx, &entry)) < 0) {
  67. av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret));
  68. goto fail;
  69. }
  70. if (!entry)
  71. break;
  72. if (entry->filemode == -1) {
  73. snprintf(filemode, 4, "???");
  74. } else {
  75. snprintf(filemode, 4, "%3"PRIo64, entry->filemode);
  76. }
  77. snprintf(uid_and_gid, 20, "%"PRId64"(%"PRId64")", entry->user_id, entry->group_id);
  78. if (cnt == 0)
  79. av_log(NULL, AV_LOG_INFO, "%-9s %12s %30s %10s %s %16s %16s %16s\n",
  80. "TYPE", "SIZE", "NAME", "UID(GID)", "UGO", "MODIFIED",
  81. "ACCESSED", "STATUS_CHANGED");
  82. av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %10s %s %16"PRId64" %16"PRId64" %16"PRId64"\n",
  83. type_string(entry->type),
  84. entry->size,
  85. entry->name,
  86. uid_and_gid,
  87. filemode,
  88. entry->modification_timestamp,
  89. entry->access_timestamp,
  90. entry->status_change_timestamp);
  91. avio_free_directory_entry(&entry);
  92. cnt++;
  93. };
  94. fail:
  95. avio_close_dir(&ctx);
  96. return ret;
  97. }
  98. static int del_op(const char *url)
  99. {
  100. int ret = avpriv_io_delete(url);
  101. if (ret < 0)
  102. av_log(NULL, AV_LOG_ERROR, "Cannot delete '%s': %s.\n", url, av_err2str(ret));
  103. return ret;
  104. }
  105. static int move_op(const char *src, const char *dst)
  106. {
  107. int ret = avpriv_io_move(src, dst);
  108. if (ret < 0)
  109. av_log(NULL, AV_LOG_ERROR, "Cannot move '%s' into '%s': %s.\n", src, dst, av_err2str(ret));
  110. return ret;
  111. }
  112. static void usage(const char *program_name)
  113. {
  114. fprintf(stderr, "usage: %s OPERATION entry1 [entry2]\n"
  115. "API example program to show how to manipulate resources "
  116. "accessed through AVIOContext.\n"
  117. "OPERATIONS:\n"
  118. "list list content of the directory\n"
  119. "move rename content in directory\n"
  120. "del delete content in directory\n",
  121. program_name);
  122. }
  123. int main(int argc, char *argv[])
  124. {
  125. const char *op = NULL;
  126. int ret;
  127. av_log_set_level(AV_LOG_DEBUG);
  128. if (argc < 2) {
  129. usage(argv[0]);
  130. return 1;
  131. }
  132. avformat_network_init();
  133. op = argv[1];
  134. if (strcmp(op, "list") == 0) {
  135. if (argc < 3) {
  136. av_log(NULL, AV_LOG_INFO, "Missing argument for list operation.\n");
  137. ret = AVERROR(EINVAL);
  138. } else {
  139. ret = list_op(argv[2]);
  140. }
  141. } else if (strcmp(op, "del") == 0) {
  142. if (argc < 3) {
  143. av_log(NULL, AV_LOG_INFO, "Missing argument for del operation.\n");
  144. ret = AVERROR(EINVAL);
  145. } else {
  146. ret = del_op(argv[2]);
  147. }
  148. } else if (strcmp(op, "move") == 0) {
  149. if (argc < 4) {
  150. av_log(NULL, AV_LOG_INFO, "Missing argument for move operation.\n");
  151. ret = AVERROR(EINVAL);
  152. } else {
  153. ret = move_op(argv[2], argv[3]);
  154. }
  155. } else {
  156. av_log(NULL, AV_LOG_INFO, "Invalid operation %s\n", op);
  157. ret = AVERROR(EINVAL);
  158. }
  159. avformat_network_deinit();
  160. return ret < 0 ? 1 : 0;
  161. }