diff options
author | Anton Khirnov <anton@khirnov.net> | 2020-01-10 12:10:09 +0100 |
---|---|---|
committer | Anton Khirnov <anton@khirnov.net> | 2020-03-16 09:23:37 +0100 |
commit | 15546f772c686412d3e823ff8db266796541846e (patch) | |
tree | 874c4b3afe3c6a51d011eace420100b1311b1e1e /doc/examples/avio_list_dir.c | |
parent | 7f0a7e3e63ff0d342772cbc7e6e5d652900d6710 (diff) | |
download | ffmpeg-15546f772c686412d3e823ff8db266796541846e.tar.gz |
examples/avio_dir_cmd: drop support for move/delete operations
They use non-public functions, which is unacceptable for a public API
example. Rename the example back to avio_list_dir.
This effectively reverts c84d208c275d6a43b3c3421d38772179abf8acee and
767d780ec001167b2fd8f6cfe4ef78a3a8b1e34c.
Diffstat (limited to 'doc/examples/avio_list_dir.c')
-rw-r--r-- | doc/examples/avio_list_dir.c | 130 |
1 files changed, 130 insertions, 0 deletions
diff --git a/doc/examples/avio_list_dir.c b/doc/examples/avio_list_dir.c new file mode 100644 index 0000000000..3073baaefa --- /dev/null +++ b/doc/examples/avio_list_dir.c @@ -0,0 +1,130 @@ +/* + * Copyright (c) 2014 Lukasz Marek + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include <libavcodec/avcodec.h> +#include <libavformat/avformat.h> +#include <libavformat/avio.h> + +static const char *type_string(int type) +{ + switch (type) { + case AVIO_ENTRY_DIRECTORY: + return "<DIR>"; + case AVIO_ENTRY_FILE: + return "<FILE>"; + case AVIO_ENTRY_BLOCK_DEVICE: + return "<BLOCK DEVICE>"; + case AVIO_ENTRY_CHARACTER_DEVICE: + return "<CHARACTER DEVICE>"; + case AVIO_ENTRY_NAMED_PIPE: + return "<PIPE>"; + case AVIO_ENTRY_SYMBOLIC_LINK: + return "<LINK>"; + case AVIO_ENTRY_SOCKET: + return "<SOCKET>"; + case AVIO_ENTRY_SERVER: + return "<SERVER>"; + case AVIO_ENTRY_SHARE: + return "<SHARE>"; + case AVIO_ENTRY_WORKGROUP: + return "<WORKGROUP>"; + case AVIO_ENTRY_UNKNOWN: + default: + break; + } + return "<UNKNOWN>"; +} + +static int list_op(const char *input_dir) +{ + AVIODirEntry *entry = NULL; + AVIODirContext *ctx = NULL; + int cnt, ret; + char filemode[4], uid_and_gid[20]; + + if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret)); + goto fail; + } + + cnt = 0; + for (;;) { + if ((ret = avio_read_dir(ctx, &entry)) < 0) { + av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret)); + goto fail; + } + if (!entry) + break; + if (entry->filemode == -1) { + snprintf(filemode, 4, "???"); + } else { + snprintf(filemode, 4, "%3"PRIo64, entry->filemode); + } + snprintf(uid_and_gid, 20, "%"PRId64"(%"PRId64")", entry->user_id, entry->group_id); + if (cnt == 0) + av_log(NULL, AV_LOG_INFO, "%-9s %12s %30s %10s %s %16s %16s %16s\n", + "TYPE", "SIZE", "NAME", "UID(GID)", "UGO", "MODIFIED", + "ACCESSED", "STATUS_CHANGED"); + av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %10s %s %16"PRId64" %16"PRId64" %16"PRId64"\n", + type_string(entry->type), + entry->size, + entry->name, + uid_and_gid, + filemode, + entry->modification_timestamp, + entry->access_timestamp, + entry->status_change_timestamp); + avio_free_directory_entry(&entry); + cnt++; + }; + + fail: + avio_close_dir(&ctx); + return ret; +} + +static void usage(const char *program_name) +{ + fprintf(stderr, "usage: %s input_dir\n" + "API example program to show how to list files in directory " + "accessed through AVIOContext.\n", program_name); +} + +int main(int argc, char *argv[]) +{ + int ret; + + av_log_set_level(AV_LOG_DEBUG); + + if (argc < 2) { + usage(argv[0]); + return 1; + } + + avformat_network_init(); + + ret = list_op(argv[1]); + + avformat_network_deinit(); + + return ret < 0 ? 1 : 0; +} |