To avoid truncation of filename with additional .tmp extension.
Unified · Split
| @@ -28,6 +28,7 @@ · 27 unmodified lines | ||
| 28 | 28 | #include "libavutil/avstring.h" |
| 29 | 29 | #include "libavutil/dict.h" |
| 30 | 30 | #include "libavutil/log.h" |
| 31 | +#include "libavutil/mem.h" | |
| 31 | 32 | #include "libavutil/opt.h" |
| 32 | 33 | #include "libavutil/pixdesc.h" |
| 33 | 34 | #include "libavutil/time_internal.h" |
| @@ -42,8 +43,6 @@ typedef struct VideoMuxData { · 8 unmodified lines | ||
| 42 | 43 | int start_img_number; |
| 43 | 44 | int img_number; |
| 44 | 45 | int split_planes; /**< use independent file for each Y, U, V plane */ |
| 45 | − char tmp[4][1024]; | |
| 46 | − char target[4][1024]; | |
| 47 | 46 | int update; |
| 48 | 47 | int use_strftime; |
| 49 | 48 | int frame_pts; |
| @@ -141,11 +140,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 91 unmodified lines | ||
| 141 | 140 | { |
| 142 | 141 | VideoMuxData *img = s->priv_data; |
| 143 | 142 | AVIOContext *pb[4] = {0}; |
| 143 | + char* target[4] = {0}; | |
| 144 | + char* tmp[4] = {0}; | |
| 144 | 145 | char filename[1024]; |
| 145 | 146 | AVCodecParameters *par = s->streams[pkt->stream_index]->codecpar; |
| 146 | 147 | const AVPixFmtDescriptor *desc = av_pix_fmt_desc_get(par->format); |
| 147 | 148 | int ret, i; |
| 148 | − int nb_renames = 0; | |
| 149 | 149 | AVDictionary *options = NULL; |
| 150 | 150 | |
| 151 | 151 | if (img->update) { |
| @@ -180,10 +180,16 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 28 unmodified lines | ||
| 180 | 180 | } |
| 181 | 181 | for (i = 0; i < 4; i++) { |
| 182 | 182 | av_dict_copy(&options, img->protocol_opts, 0); |
| 183 | − snprintf(img->tmp[i], sizeof(img->tmp[i]), "%s.tmp", filename); | |
| 184 | − av_strlcpy(img->target[i], filename, sizeof(img->target[i])); | |
| 185 | − if (s->io_open(s, &pb[i], img->use_rename ? img->tmp[i] : filename, AVIO_FLAG_WRITE, &options) < 0) { | |
| 186 | − av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", img->use_rename ? img->tmp[i] : filename); | |
| 183 | + if (img->use_rename) { | |
| 184 | + tmp[i] = av_asprintf("%s.tmp", filename); | |
| 185 | + target[i] = av_strdup(filename); | |
| 186 | + if (!tmp[i] || !target[i]) { | |
| 187 | + ret = AVERROR(ENOMEM); | |
| 188 | + goto fail; | |
| 189 | + } | |
| 190 | + } | |
| 191 | + if (s->io_open(s, &pb[i], tmp[i] ? tmp[i] : filename, AVIO_FLAG_WRITE, &options) < 0) { | |
| 192 | + av_log(s, AV_LOG_ERROR, "Could not open file : %s\n", tmp[i] ? tmp[i] : filename); | |
| 187 | 193 | ret = AVERROR(EIO); |
| 188 | 194 | goto fail; |
| 189 | 195 | } |
| @@ -197,8 +203,6 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 7 unmodified lines | ||
| 197 | 203 | break; |
| 198 | 204 | filename[strlen(filename) - 1] = "UVAx"[i]; |
| 199 | 205 | } |
| 200 | − if (img->use_rename) | |
| 201 | − nb_renames = i + 1; | |
| 202 | 206 | |
| 203 | 207 | if (img->split_planes) { |
| 204 | 208 | int ysize = par->width * par->height; |
| @@ -223,10 +227,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 18 unmodified lines | ||
| 223 | 227 | if (ret < 0) |
| 224 | 228 | goto fail; |
| 225 | 229 | |
| 226 | − for (i = 0; i < nb_renames; i++) { | |
| 227 | − int ret = ff_rename(img->tmp[i], img->target[i], s); | |
| 230 | + for (i = 0; i < 4 && tmp[i]; i++) { | |
| 231 | + int ret = ff_rename(tmp[i], target[i], s); | |
| 228 | 232 | if (ret < 0) |
| 229 | − return ret; | |
| 233 | + goto fail; | |
| 234 | + av_freep(&tmp[i]); | |
| 235 | + av_freep(&target[i]); | |
| 230 | 236 | } |
| 231 | 237 | |
| 232 | 238 | img->img_number++; |
| @@ -234,9 +240,12 @@ static int write_packet(AVFormatContext *s, AVPacket *pkt) · 1 unmodified line | ||
| 234 | 240 | |
| 235 | 241 | fail: |
| 236 | 242 | av_dict_free(&options); |
| 237 | − for (i = 0; i < FF_ARRAY_ELEMS(pb); i++) | |
| 243 | + for (i = 0; i < FF_ARRAY_ELEMS(pb); i++) { | |
| 244 | + av_freep(&tmp[i]); | |
| 245 | + av_freep(&target[i]); | |
| 238 | 246 | if (pb[i]) |
| 239 | 247 | ff_format_io_close(s, &pb[i]); |
| 248 | + } | |
| 240 | 249 | return ret; |
| 241 | 250 | } |
| 242 | 251 | |