| 1 | /* |
| 2 | Copyright (c) 2012, Broadcom Europe Ltd |
| 3 | All rights reserved. |
| 4 | |
| 5 | Redistribution and use in source and binary forms, with or without |
| 6 | modification, 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 | |
| 16 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 17 | ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 18 | WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 19 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY |
| 20 | DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 21 | (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 22 | LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
| 23 | ON 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 |
| 25 | SOFTWARE, 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 | /*****************************************************************************/ |
| 40 | static VC_CONTAINER_STATUS_T (VC_CONTAINER_T *context, const char *uri, |
| 41 | VC_CONTAINER_WRITER_EXTRAIO_T *) |
| 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 | /*****************************************************************************/ |
| 53 | VC_CONTAINER_STATUS_T (VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *) |
| 54 | { |
| 55 | return vc_container_writer_extraio_create(context, "null://" , extraio); |
| 56 | } |
| 57 | |
| 58 | /*****************************************************************************/ |
| 59 | VC_CONTAINER_STATUS_T (VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *) |
| 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 | /*****************************************************************************/ |
| 78 | VC_CONTAINER_STATUS_T (VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *) |
| 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 | /*****************************************************************************/ |
| 97 | int64_t (VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *) |
| 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 | /*****************************************************************************/ |
| 112 | int64_t (VC_CONTAINER_T *context, VC_CONTAINER_WRITER_EXTRAIO_T *) |
| 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 | |