Loading...
Searching...
No Matches
avio_list_dir.c

Show how to list directories through the libavformat AVIOContext API.

Show how to list directories through the libavformat AVIOContext API.

/*
* 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.
*/
/**
* @file libavformat AVIOContext list directory API usage example
* @example avio_list_dir.c
*
* Show how to list directories through the libavformat AVIOContext API.
*/
#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;
}
avcodec.h
Libavcodec external API header.
avformat.h
Main libavformat public API header.
avio.h
Buffered I/O operations.
AVIODirContext
struct AVIODirContext AVIODirContext
Definition avio.h:104
avio_close_dir
int avio_close_dir(AVIODirContext **s)
Close directory.
avio_open_dir
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
Open directory for reading.
avio_free_directory_entry
void avio_free_directory_entry(AVIODirEntry **entry)
Free entry allocated by avio_read_dir().
avio_read_dir
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
Get next directory entry.
AVIO_ENTRY_UNKNOWN
@ AVIO_ENTRY_UNKNOWN
Definition avio.h:68
AVIO_ENTRY_NAMED_PIPE
@ AVIO_ENTRY_NAMED_PIPE
Definition avio.h:72
AVIO_ENTRY_WORKGROUP
@ AVIO_ENTRY_WORKGROUP
Definition avio.h:78
AVIO_ENTRY_SERVER
@ AVIO_ENTRY_SERVER
Definition avio.h:76
AVIO_ENTRY_SHARE
@ AVIO_ENTRY_SHARE
Definition avio.h:77
AVIO_ENTRY_BLOCK_DEVICE
@ AVIO_ENTRY_BLOCK_DEVICE
Definition avio.h:69
AVIO_ENTRY_SYMBOLIC_LINK
@ AVIO_ENTRY_SYMBOLIC_LINK
Definition avio.h:73
AVIO_ENTRY_DIRECTORY
@ AVIO_ENTRY_DIRECTORY
Definition avio.h:71
AVIO_ENTRY_CHARACTER_DEVICE
@ AVIO_ENTRY_CHARACTER_DEVICE
Definition avio.h:70
AVIO_ENTRY_FILE
@ AVIO_ENTRY_FILE
Definition avio.h:75
AVIO_ENTRY_SOCKET
@ AVIO_ENTRY_SOCKET
Definition avio.h:74
main
int main(int argc, char *argv[])
Definition avio_list_dir.c:119
usage
static void usage(const char *program_name)
Definition avio_list_dir.c:112
type_string
static const char * type_string(int type)
Definition avio_list_dir.c:34
list_op
static int list_op(const char *input_dir)
Definition avio_list_dir.c:64
avformat_network_deinit
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
avformat_network_init
int avformat_network_init(void)
Do global initialization of network libraries.
av_err2str
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition error.h:122
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:201
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition log.h:191
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:180
av_log_set_level
void av_log_set_level(int level)
Set the log level.
av_log
void av_log(void *avcl, int level, const char *fmt,...) av_printf_format(3
Send the specified message to the log if the level is less than or equal to the current av_log_level.
AVIODirEntry
Describes single entry of the directory.
Definition avio.h:87
AVIODirEntry::user_id
int64_t user_id
User ID of owner, -1 if unknown.
Definition avio.h:99
AVIODirEntry::type
int type
Type of the entry.
Definition avio.h:89
AVIODirEntry::access_timestamp
int64_t access_timestamp
Time of last access in microseconds since unix epoch, -1 if unknown.
Definition avio.h:95
AVIODirEntry::status_change_timestamp
int64_t status_change_timestamp
Time of last status change in microseconds since unix epoch, -1 if unknown.
Definition avio.h:97
AVIODirEntry::size
int64_t size
File size in bytes, -1 if unknown.
Definition avio.h:92
AVIODirEntry::group_id
int64_t group_id
Group ID of owner, -1 if unknown.
Definition avio.h:100
AVIODirEntry::name
char * name
Filename.
Definition avio.h:88
AVIODirEntry::modification_timestamp
int64_t modification_timestamp
Time of last modification in microseconds since unix epoch, -1 if unknown.
Definition avio.h:93
AVIODirEntry::filemode
int64_t filemode
Unix file mode, -1 if unknown.
Definition avio.h:101