[PATCH] Add av_file_read(), replace cmdutils.h:read_file().

Stefano Sabatini stefano.sabatini-lala
Fri Nov 26 01:27:58 CET 2010


---
 cmdutils.c         |   24 --------------------
 cmdutils.h         |   11 ---------
 ffmpeg.c           |    3 +-
 libavutil/Makefile |    2 +
 libavutil/file.c   |   60 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 libavutil/file.h   |   41 +++++++++++++++++++++++++++++++++++
 6 files changed, 105 insertions(+), 36 deletions(-)
 create mode 100644 libavutil/file.c
 create mode 100644 libavutil/file.h

diff --git a/cmdutils.c b/cmdutils.c
index b2e6ee0..b5bf09f 100644
--- a/cmdutils.c
+++ b/cmdutils.c
@@ -697,30 +697,6 @@ int read_yesno(void)
     return yesno;
 }
 
-int read_file(const char *filename, char **bufptr, size_t *size)
-{
-    FILE *f = fopen(filename, "rb");
-
-    if (!f) {
-        fprintf(stderr, "Cannot read file '%s': %s\n", filename, strerror(errno));
-        return AVERROR(errno);
-    }
-    fseek(f, 0, SEEK_END);
-    *size = ftell(f);
-    fseek(f, 0, SEEK_SET);
-    *bufptr = av_malloc(*size + 1);
-    if (!*bufptr) {
-        fprintf(stderr, "Could not allocate file buffer\n");
-        fclose(f);
-        return AVERROR(ENOMEM);
-    }
-    fread(*bufptr, 1, *size, f);
-    (*bufptr)[*size++] = '\0';
-
-    fclose(f);
-    return 0;
-}
-
 void init_pts_correction(PtsCorrectionContext *ctx)
 {
     ctx->num_faulty_pts = ctx->num_faulty_dts = 0;
diff --git a/cmdutils.h b/cmdutils.h
index 9fb7845..32fab74 100644
--- a/cmdutils.h
+++ b/cmdutils.h
@@ -226,17 +226,6 @@ void show_pix_fmts(void);
  */
 int read_yesno(void);
 
-/**
- * Read the file with name filename, and put its content in a newly
- * allocated 0-terminated buffer.
- *
- * @param bufptr location where pointer to buffer is returned
- * @param size   location where size of buffer is returned
- * @return 0 in case of success, a negative value corresponding to an
- * AVERROR error code in case of failure.
- */
-int read_file(const char *filename, char **bufptr, size_t *size);
-
 typedef struct {
     int64_t num_faulty_pts; /// Number of incorrect PTS values so far
     int64_t num_faulty_dts; /// Number of incorrect DTS values so far
diff --git a/ffmpeg.c b/ffmpeg.c
index e58e7b5..d61b35b 100644
--- a/ffmpeg.c
+++ b/ffmpeg.c
@@ -41,6 +41,7 @@
 #include "libavcore/samplefmt.h"
 #include "libavutil/colorspace.h"
 #include "libavutil/fifo.h"
+#include "libavutil/file.h"
 #include "libavutil/intreadwrite.h"
 #include "libavutil/pixdesc.h"
 #include "libavutil/avstring.h"
@@ -2258,7 +2259,7 @@ static int transcode(AVFormatContext **output_files,
                 } else {
                     char  *logbuffer;
                     size_t logbuffer_size;
-                    if (read_file(logfilename, &logbuffer, &logbuffer_size) < 0) {
+                    if (av_file_read(logfilename, &logbuffer, &logbuffer_size, NULL) < 0) {
                         fprintf(stderr, "Error reading log file '%s' for pass-2 encoding\n", logfilename);
                         ffmpeg_exit(1);
                     }
diff --git a/libavutil/Makefile b/libavutil/Makefile
index e9ac965..fe0302c 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -15,6 +15,7 @@ HEADERS = adler32.h                                                     \
           error.h                                                       \
           eval.h                                                        \
           fifo.h                                                        \
+          file.h                                                        \
           intfloat_readwrite.h                                          \
           intreadwrite.h                                                \
           lfg.h                                                         \
@@ -42,6 +43,7 @@ OBJS = adler32.o                                                        \
        error.o                                                          \
        eval.o                                                           \
        fifo.o                                                           \
+       file.o                                                           \
        intfloat_readwrite.o                                             \
        inverse.o                                                        \
        lfg.o                                                            \
diff --git a/libavutil/file.c b/libavutil/file.c
new file mode 100644
index 0000000..491bcde
--- /dev/null
+++ b/libavutil/file.c
@@ -0,0 +1,60 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <fcntl.h>
+#include <unistd.h>
+#include <sys/stat.h>
+#include "file.h"
+
+int av_file_read(const char *filename, char **bufptr, size_t *size, void *log_ctx)
+{
+    int fd = open(filename, O_RDONLY);
+    struct stat st;
+    off_t off_size;
+
+    if (fd < 0) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "Cannot read file '%s': %s\n", filename, strerror(errno));
+        return AVERROR(errno);
+    }
+
+    if (lstat(filename, &st) < 0) {
+        close(fd);
+        return AVERROR(errno);
+    }
+
+    off_size = st.st_size;
+    if (off_size >= SIZE_MAX) {
+        av_log(log_ctx, AV_LOG_ERROR,
+               "File size for file '%s' is too big\n", filename);
+        close(fd);
+        return AVERROR(EINVAL);
+    }
+    *size = off_size;
+
+    *bufptr = av_malloc(*size + 1);
+    if (!*bufptr) {
+        close(fd);
+        return AVERROR(ENOMEM);
+    }
+    read(fd, *bufptr, *size);
+    (*bufptr)[*size++] = '\0';
+
+    close(fd);
+    return 0;
+}
diff --git a/libavutil/file.h b/libavutil/file.h
new file mode 100644
index 0000000..1e74e9d
--- /dev/null
+++ b/libavutil/file.h
@@ -0,0 +1,41 @@
+/*
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef AVUTIL_FILE_H
+#define AVUTIL_FILE_H
+
+#include "avutil.h"
+
+/**
+ * @file misc file utilities
+ */
+
+/**
+ * Read the file with name filename, and put its content in a newly
+ * allocated 0-terminated buffer. If filename references a link, the
+ * content of the linked file is read.
+ *
+ * @param bufptr location where pointer to buffer is returned
+ * @param size   location where size of buffer is returned
+ * @param log_ctx context used for logging
+ * @return 0 in case of success, a negative value corresponding to an
+ * AVERROR error code in case of failure
+ */
+int av_file_read(const char *filename, char **bufptr, size_t *size, void *log_ctx);
+
+#endif /* AVUTIL_FILE_H */
-- 
1.7.1


--X1bOJ3K7DJ5YkBrT--



More information about the ffmpeg-devel mailing list