FFmpeg
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
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 #include <libavcodec/avcodec.h>
24 #include <libavformat/avformat.h>
25 #include <libavformat/avio.h>
26 
27 static const char *type_string(int type)
28 {
29  switch (type) {
31  return "<DIR>";
32  case AVIO_ENTRY_FILE:
33  return "<FILE>";
35  return "<BLOCK DEVICE>";
37  return "<CHARACTER DEVICE>";
39  return "<PIPE>";
41  return "<LINK>";
42  case AVIO_ENTRY_SOCKET:
43  return "<SOCKET>";
44  case AVIO_ENTRY_SERVER:
45  return "<SERVER>";
46  case AVIO_ENTRY_SHARE:
47  return "<SHARE>";
49  return "<WORKGROUP>";
50  case AVIO_ENTRY_UNKNOWN:
51  default:
52  break;
53  }
54  return "<UNKNOWN>";
55 }
56 
57 int main(int argc, char *argv[])
58 {
59  const char *input_dir = NULL;
60  AVIODirEntry *entry = NULL;
61  AVIODirContext *ctx = NULL;
62  int cnt, ret;
63  char filemode[4], uid_and_gid[20];
64 
66 
67  if (argc != 2) {
68  fprintf(stderr, "usage: %s input_dir\n"
69  "API example program to show how to list files in directory "
70  "accessed through AVIOContext.\n", argv[0]);
71  return 1;
72  }
73  input_dir = argv[1];
74 
75  /* register codecs and formats and other lavf/lavc components*/
78 
79  if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) {
80  av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
81  goto fail;
82  }
83 
84  cnt = 0;
85  for (;;) {
86  if ((ret = avio_read_dir(ctx, &entry)) < 0) {
87  av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret));
88  goto fail;
89  }
90  if (!entry)
91  break;
92  if (entry->filemode == -1) {
93  snprintf(filemode, 4, "???");
94  } else {
95  snprintf(filemode, 4, "%3"PRIo64, entry->filemode);
96  }
97  snprintf(uid_and_gid, 20, "%"PRId64"(%"PRId64")", entry->user_id, entry->group_id);
98  if (cnt == 0)
99  av_log(NULL, AV_LOG_INFO, "%-9s %12s %30s %10s %s %16s %16s %16s\n",
100  "TYPE", "SIZE", "NAME", "UID(GID)", "UGO", "MODIFIED",
101  "ACCESSED", "STATUS_CHANGED");
102  av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %10s %s %16"PRId64" %16"PRId64" %16"PRId64"\n",
103  type_string(entry->type),
104  entry->size,
105  entry->name,
106  uid_and_gid,
107  filemode,
108  entry->modification_timestamp,
109  entry->access_timestamp,
110  entry->status_change_timestamp);
112  cnt++;
113  };
114 
115  fail:
116  avio_close_dir(&ctx);
118 
119  return ret < 0 ? 1 : 0;
120 }
#define NULL
Definition: coverity.c:32
Buffered I/O operations.
int64_t filemode
Unix file mode, -1 if unknown.
Definition: avio.h:92
void av_log_set_level(int level)
Set the log level.
Definition: log.c:382
Describes single entry of the directory.
Definition: avio.h:78
int64_t modification_timestamp
Time of last modification in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:84
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
Get next directory entry.
Definition: avio.c:461
int avformat_network_init(void)
Do global initialization of network components.
Definition: utils.c:4123
#define av_log(a,...)
char * name
Filename.
Definition: avio.h:79
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
Libavcodec external API header.
void avio_free_directory_entry(AVIODirEntry **entry)
Free entry allocated by avio_read_dir().
Definition: avio.c:489
int64_t access_timestamp
Time of last access in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:86
ret
Definition: avfilter.c:974
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
int main(int argc, char *argv[])
Definition: avio_list_dir.c:57
int64_t size
File size in bytes, -1 if unknown.
Definition: avio.h:83
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:4136
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
int type
Type of the entry.
Definition: avio.h:80
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
Open directory for reading.
Definition: avio.c:424
GLint GLenum type
Definition: opengl_enc.c:105
int64_t group_id
Group ID of owner, -1 if unknown.
Definition: avio.h:91
static const char * type_string(int type)
Definition: avio_list_dir.c:27
#define snprintf
Definition: snprintf.h:34
Main libavformat public API header.
int64_t status_change_timestamp
Time of last status change in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:88
int avio_close_dir(AVIODirContext **s)
Close directory.
Definition: avio.c:474
int64_t user_id
User ID of owner, -1 if unknown.
Definition: avio.h:90
void av_register_all(void)
Initialize libavformat and register all the muxers, demuxers and protocols.
Definition: allformats.c:51