FFmpeg
avio_list_dir.c
Go to the documentation of this file.
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 
23 /**
24  * @file libavformat AVIOContext list directory API usage example
25  * @example avio_list_dir.c
26  *
27  * Show how to list directories through the libavformat AVIOContext API.
28  */
29 
30 #include <libavcodec/avcodec.h>
31 #include <libavformat/avformat.h>
32 #include <libavformat/avio.h>
33 
34 static const char *type_string(int type)
35 {
36  switch (type) {
38  return "<DIR>";
39  case AVIO_ENTRY_FILE:
40  return "<FILE>";
42  return "<BLOCK DEVICE>";
44  return "<CHARACTER DEVICE>";
46  return "<PIPE>";
48  return "<LINK>";
49  case AVIO_ENTRY_SOCKET:
50  return "<SOCKET>";
51  case AVIO_ENTRY_SERVER:
52  return "<SERVER>";
53  case AVIO_ENTRY_SHARE:
54  return "<SHARE>";
56  return "<WORKGROUP>";
57  case AVIO_ENTRY_UNKNOWN:
58  default:
59  break;
60  }
61  return "<UNKNOWN>";
62 }
63 
64 static int list_op(const char *input_dir)
65 {
66  AVIODirEntry *entry = NULL;
68  int cnt, ret;
69  char filemode[4], uid_and_gid[20];
70 
71  if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) {
72  av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
73  goto fail;
74  }
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);
104  cnt++;
105  };
106 
107  fail:
109  return ret;
110 }
111 
112 static void usage(const char *program_name)
113 {
114  fprintf(stderr, "usage: %s input_dir\n"
115  "API example program to show how to list files in directory "
116  "accessed through AVIOContext.\n", program_name);
117 }
118 
119 int main(int argc, char *argv[])
120 {
121  int ret;
122 
124 
125  if (argc < 2) {
126  usage(argv[0]);
127  return 1;
128  }
129 
131 
132  ret = list_op(argv[1]);
133 
135 
136  return ret < 0 ? 1 : 0;
137 }
usage
static void usage(const char *program_name)
Definition: avio_list_dir.c:112
program_name
const char program_name[]
program name, defined by the program for show_version().
Definition: ffmpeg.c:104
AVIODirEntry::type
int type
Type of the entry.
Definition: avio.h:89
AVIO_ENTRY_NAMED_PIPE
@ AVIO_ENTRY_NAMED_PIPE
Definition: avio.h:72
AVIO_ENTRY_UNKNOWN
@ AVIO_ENTRY_UNKNOWN
Definition: avio.h:68
AVIODirEntry::access_timestamp
int64_t access_timestamp
Time of last access in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:95
AVIO_ENTRY_DIRECTORY
@ AVIO_ENTRY_DIRECTORY
Definition: avio.h:71
fail
#define fail()
Definition: checkasm.h:138
AVIO_ENTRY_CHARACTER_DEVICE
@ AVIO_ENTRY_CHARACTER_DEVICE
Definition: avio.h:70
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
avformat_network_init
int avformat_network_init(void)
Do global initialization of network libraries.
Definition: utils.c:558
avio_free_directory_entry
void avio_free_directory_entry(AVIODirEntry **entry)
Free entry allocated by avio_read_dir().
Definition: avio.c:604
AVIODirEntry::modification_timestamp
int64_t modification_timestamp
Time of last modification in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:93
list_op
static int list_op(const char *input_dir)
Definition: avio_list_dir.c:64
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
AVIO_ENTRY_SYMBOLIC_LINK
@ AVIO_ENTRY_SYMBOLIC_LINK
Definition: avio.h:73
avio_close_dir
int avio_close_dir(AVIODirContext **s)
Close directory.
Definition: avio.c:589
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
ctx
AVFormatContext * ctx
Definition: movenc.c:48
main
int main(int argc, char *argv[])
Definition: avio_list_dir.c:119
NULL
#define NULL
Definition: coverity.c:32
AVIODirEntry::size
int64_t size
File size in bytes, -1 if unknown.
Definition: avio.h:92
AVIODirEntry::name
char * name
Filename.
Definition: avio.h:88
AVIO_ENTRY_FILE
@ AVIO_ENTRY_FILE
Definition: avio.h:75
AVIODirEntry::group_id
int64_t group_id
Group ID of owner, -1 if unknown.
Definition: avio.h:100
avio_read_dir
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
Get next directory entry.
Definition: avio.c:576
AVIODirEntry::filemode
int64_t filemode
Unix file mode, -1 if unknown.
Definition: avio.h:101
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:121
AVIODirEntry
Describes single entry of the directory.
Definition: avio.h:87
avio.h
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
AVIO_ENTRY_SOCKET
@ AVIO_ENTRY_SOCKET
Definition: avio.h:74
AV_LOG_INFO
#define AV_LOG_INFO
Standard information.
Definition: log.h:191
AVIODirEntry::user_id
int64_t user_id
User ID of owner, -1 if unknown.
Definition: avio.h:99
av_log_set_level
void av_log_set_level(int level)
Set the log level.
Definition: log.c:442
avio_open_dir
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
Open directory for reading.
Definition: avio.c:538
avcodec.h
AVIO_ENTRY_BLOCK_DEVICE
@ AVIO_ENTRY_BLOCK_DEVICE
Definition: avio.h:69
ret
ret
Definition: filter_design.txt:187
AVIODirContext
Definition: avio.c:533
type_string
static const char * type_string(int type)
Definition: avio_list_dir.c:34
avformat.h
avformat_network_deinit
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:570
AVIO_ENTRY_WORKGROUP
@ AVIO_ENTRY_WORKGROUP
Definition: avio.h:78
AVIO_ENTRY_SERVER
@ AVIO_ENTRY_SERVER
Definition: avio.h:76
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
snprintf
#define snprintf
Definition: snprintf.h:34
AVIO_ENTRY_SHARE
@ AVIO_ENTRY_SHARE
Definition: avio.h:77