1/*
2Copyright (c) 2012, Broadcom Europe Ltd
3All rights reserved.
4
5Redistribution and use in source and binary forms, with or without
6modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the copyright holder nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
15
16THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
20DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26*/
27
28#include <stdlib.h>
29#include <string.h>
30
31#include "containers/containers.h"
32#include "containers/core/containers_private.h"
33#include "containers/core/containers_utils.h"
34#include "containers/core/containers_writer_utils.h"
35#include "vcos.h"
36
37#include <stdio.h>
38
39/*****************************************************************************/
40static VC_CONTAINER_STATUS_T vc_container_writer_extraio_create(VC_CONTAINER_T *context, const char *uri,
41 VC_CONTAINER_WRITER_EXTRAIO_T *extraio)
42{
43 VC_CONTAINER_STATUS_T status = VC_CONTAINER_SUCCESS;
44 VC_CONTAINER_PARAM_UNUSED(context);
45
46 extraio->io = vc_container_io_open( uri, VC_CONTAINER_IO_MODE_WRITE, &status );
47 extraio->refcount = 0;
48 extraio->temp = 0;
49 return status;
50}
51
52/*****************************************************************************/
53VC_CONTAINER_STATUS_T vc_container_writer_extraio_create_null(VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *extraio)
54{
55 return vc_container_writer_extraio_create(context, "null://", extraio);
56}
57
58/*****************************************************************************/
59VC_CONTAINER_STATUS_T vc_container_writer_extraio_create_temp(VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *extraio)
60{
61 VC_CONTAINER_STATUS_T status = VC_CONTAINER_SUCCESS;
62 unsigned int length = strlen(context->priv->io->uri) + 5;
63 char *uri = malloc(length);
64 if(!uri) return VC_CONTAINER_ERROR_OUT_OF_MEMORY;
65
66 snprintf(uri, length, "%s.tmp", context->priv->io->uri);
67 status = vc_container_writer_extraio_create(context, uri, extraio);
68 free(uri);
69 extraio->temp = true;
70
71 if(status == VC_CONTAINER_SUCCESS && !context->priv->tmp_io)
72 context->priv->tmp_io = extraio->io;
73
74 return status;
75}
76
77/*****************************************************************************/
78VC_CONTAINER_STATUS_T vc_container_writer_extraio_delete(VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *extraio)
79{
80 VC_CONTAINER_STATUS_T status;
81 char *uri = extraio->temp ? vcos_strdup(extraio->io->uri) : 0;
82
83 while(extraio->refcount) vc_container_writer_extraio_disable(context, extraio);
84 status = vc_container_io_close( extraio->io );
85
86 /* coverity[check_return] On failure the worst case is a file or directory is not removed */
87 if(uri) remove(uri);
88 if(uri) free(uri);
89
90 if(context->priv->tmp_io == extraio->io)
91 context->priv->tmp_io = 0;
92
93 return status;
94}
95
96/*****************************************************************************/
97int64_t vc_container_writer_extraio_enable(VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *extraio)
98{
99 VC_CONTAINER_IO_T *tmp;
100
101 if(!extraio->refcount)
102 {
103 vc_container_io_seek(extraio->io, INT64_C(0));
104 tmp = context->priv->io;
105 context->priv->io = extraio->io;
106 extraio->io = tmp;
107 }
108 return extraio->refcount++;
109}
110
111/*****************************************************************************/
112int64_t vc_container_writer_extraio_disable(VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *extraio)
113{
114 VC_CONTAINER_IO_T *tmp;
115
116 if(extraio->refcount)
117 {
118 extraio->refcount--;
119 if(!extraio->refcount)
120 {
121 tmp = context->priv->io;
122 context->priv->io = extraio->io;
123 extraio->io = tmp;
124 }
125 }
126 return extraio->refcount;
127}
128