FFmpeg  4.0
avio_dir_cmd.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 static int list_op(const char *input_dir)
58 {
59  AVIODirEntry *entry = NULL;
61  int cnt, ret;
62  char filemode[4], uid_and_gid[20];
63 
64  if ((ret = avio_open_dir(&ctx, input_dir, NULL)) < 0) {
65  av_log(NULL, AV_LOG_ERROR, "Cannot open directory: %s.\n", av_err2str(ret));
66  goto fail;
67  }
68 
69  cnt = 0;
70  for (;;) {
71  if ((ret = avio_read_dir(ctx, &entry)) < 0) {
72  av_log(NULL, AV_LOG_ERROR, "Cannot list directory: %s.\n", av_err2str(ret));
73  goto fail;
74  }
75  if (!entry)
76  break;
77  if (entry->filemode == -1) {
78  snprintf(filemode, 4, "???");
79  } else {
80  snprintf(filemode, 4, "%3"PRIo64, entry->filemode);
81  }
82  snprintf(uid_and_gid, 20, "%"PRId64"(%"PRId64")", entry->user_id, entry->group_id);
83  if (cnt == 0)
84  av_log(NULL, AV_LOG_INFO, "%-9s %12s %30s %10s %s %16s %16s %16s\n",
85  "TYPE", "SIZE", "NAME", "UID(GID)", "UGO", "MODIFIED",
86  "ACCESSED", "STATUS_CHANGED");
87  av_log(NULL, AV_LOG_INFO, "%-9s %12"PRId64" %30s %10s %s %16"PRId64" %16"PRId64" %16"PRId64"\n",
88  type_string(entry->type),
89  entry->size,
90  entry->name,
91  uid_and_gid,
92  filemode,
94  entry->access_timestamp,
97  cnt++;
98  };
99 
100  fail:
101  avio_close_dir(&ctx);
102  return ret;
103 }
104 
105 static int del_op(const char *url)
106 {
107  int ret = avpriv_io_delete(url);
108  if (ret < 0)
109  av_log(NULL, AV_LOG_ERROR, "Cannot delete '%s': %s.\n", url, av_err2str(ret));
110  return ret;
111 }
112 
113 static int move_op(const char *src, const char *dst)
114 {
115  int ret = avpriv_io_move(src, dst);
116  if (ret < 0)
117  av_log(NULL, AV_LOG_ERROR, "Cannot move '%s' into '%s': %s.\n", src, dst, av_err2str(ret));
118  return ret;
119 }
120 
121 
122 static void usage(const char *program_name)
123 {
124  fprintf(stderr, "usage: %s OPERATION entry1 [entry2]\n"
125  "API example program to show how to manipulate resources "
126  "accessed through AVIOContext.\n"
127  "OPERATIONS:\n"
128  "list list content of the directory\n"
129  "move rename content in directory\n"
130  "del delete content in directory\n",
131  program_name);
132 }
133 
134 int main(int argc, char *argv[])
135 {
136  const char *op = NULL;
137  int ret;
138 
140 
141  if (argc < 2) {
142  usage(argv[0]);
143  return 1;
144  }
145 
147 
148  op = argv[1];
149  if (strcmp(op, "list") == 0) {
150  if (argc < 3) {
151  av_log(NULL, AV_LOG_INFO, "Missing argument for list operation.\n");
152  ret = AVERROR(EINVAL);
153  } else {
154  ret = list_op(argv[2]);
155  }
156  } else if (strcmp(op, "del") == 0) {
157  if (argc < 3) {
158  av_log(NULL, AV_LOG_INFO, "Missing argument for del operation.\n");
159  ret = AVERROR(EINVAL);
160  } else {
161  ret = del_op(argv[2]);
162  }
163  } else if (strcmp(op, "move") == 0) {
164  if (argc < 4) {
165  av_log(NULL, AV_LOG_INFO, "Missing argument for move operation.\n");
166  ret = AVERROR(EINVAL);
167  } else {
168  ret = move_op(argv[2], argv[3]);
169  }
170  } else {
171  av_log(NULL, AV_LOG_INFO, "Invalid operation %s\n", op);
172  ret = AVERROR(EINVAL);
173  }
174 
176 
177  return ret < 0 ? 1 : 0;
178 }
#define NULL
Definition: coverity.c:32
static int list_op(const char *input_dir)
Definition: avio_dir_cmd.c:57
Buffered I/O operations.
int64_t filemode
Unix file mode, -1 if unknown.
Definition: avio.h:100
void av_log_set_level(int level)
Set the log level.
Definition: log.c:385
Describes single entry of the directory.
Definition: avio.h:86
#define src
Definition: vp8dsp.c:254
static void usage(const char *program_name)
Definition: avio_dir_cmd.c:122
int avpriv_io_move(const char *url_src, const char *url_dst)
Move or rename a resource.
Definition: avio.c:499
int64_t modification_timestamp
Time of last modification in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:92
int avio_read_dir(AVIODirContext *s, AVIODirEntry **next)
Get next directory entry.
Definition: avio.c:575
int avformat_network_init(void)
Do global initialization of network libraries.
Definition: utils.c:4922
static int move_op(const char *src, const char *dst)
Definition: avio_dir_cmd.c:113
int main(int argc, char *argv[])
Definition: avio_dir_cmd.c:134
#define av_log(a,...)
char * name
Filename.
Definition: avio.h:87
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
#define AVERROR(e)
Definition: error.h:43
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
#define fail()
Definition: checkasm.h:116
void avio_free_directory_entry(AVIODirEntry **entry)
Free entry allocated by avio_read_dir().
Definition: avio.c:603
int64_t access_timestamp
Time of last access in microseconds since unix epoch, -1 if unknown.
Definition: avio.h:94
#define av_err2str(errnum)
Convenience macro, the return value should be used only directly in function arguments but never stan...
Definition: error.h:119
AVFormatContext * ctx
Definition: movenc.c:48
int64_t size
File size in bytes, -1 if unknown.
Definition: avio.h:91
int avformat_network_deinit(void)
Undo the initialization done by avformat_network_init.
Definition: utils.c:4934
#define AV_LOG_INFO
Standard information.
Definition: log.h:187
Libavcodec external API header.
static int del_op(const char *url)
Definition: avio_dir_cmd.c:105
int type
Type of the entry.
Definition: avio.h:88
int avio_open_dir(AVIODirContext **s, const char *url, AVDictionary **options)
Open directory for reading.
Definition: avio.c:537
int64_t group_id
Group ID of owner, -1 if unknown.
Definition: avio.h:99
cl_device_type type
#define snprintf
Definition: snprintf.h:34
static const char * type_string(int type)
Definition: avio_dir_cmd.c:27
static int op(uint8_t **dst, const uint8_t *dst_end, GetByteContext *gb, int pixel, int count, int *x, int width, int linesize)
Perform decode operation.
Definition: anm.c:78
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:96
int avio_close_dir(AVIODirContext **s)
Close directory.
Definition: avio.c:588
int64_t user_id
User ID of owner, -1 if unknown.
Definition: avio.h:98
int avpriv_io_delete(const char *url)
Delete a resource.
Definition: avio.c:521
const char program_name[]
program name, defined by the program for show_version().
Definition: ffmpeg.c:109