AVIODirEntry 目录信息
2020-11-27 本文已影响0人
NHFX
#include <QCoreApplication>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>
#include <libavformat/avio.h>
}
// clang -g -o list dir_demo.c `pkg-config --libs liavformat libavutil`
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, "Connot 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);
}
// 打印文件信息, PRId64 是 64的宏信息
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, // SIZE
entry->name, // 名称
uid_and_gid, //
filemode,
entry->modification_timestamp,
entry->access_timestamp,
entry->status_change_timestamp);
// 要进行entry的释放
avio_free_directory_entry(&entry);
cnt++;
}
fail:
//关闭文件夹
avio_close_dir(&ctx);
return ret;
}
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
int ret;
QString dir_path = "/Users/autozi01/workspc";
av_log_set_level(AV_LOG_DEBUG);
avformat_network_init();
ret = list_op(dir_path.toStdString().c_str());
avformat_network_deinit();
return ret < 0 ? 1 : 0;
// return a.exec();
}