1/*
2 * Copyright (C) 2014 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 *    notice, this list of conditions and the following disclaimer.
9 * 2. 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 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23 * THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26#include "config.h"
27#include "BlobDataFileReference.h"
28
29#if ENABLE(FILE_REPLACEMENT)
30
31#include "FileMetadata.h"
32#include "SoftLinking.h"
33#include <wtf/text/CString.h>
34
35#if defined(__has_include)
36#if __has_include(<Bom/BOMCopier.h>)
37#include <Bom/BOMCopier.h>
38#endif
39#endif
40
41typedef struct _BOMCopier* BOMCopier;
42
43SOFT_LINK_PRIVATE_FRAMEWORK(Bom)
44SOFT_LINK(Bom, BOMCopierNew, BOMCopier, (), ())
45SOFT_LINK(Bom, BOMCopierFree, void, (BOMCopier copier), (copier))
46SOFT_LINK(Bom, BOMCopierCopyWithOptions, int, (BOMCopier copier, const char* fromObj, const char* toObj, CFDictionaryRef options), (copier, fromObj, toObj, options))
47
48#define kBOMCopierOptionCreatePKZipKey CFSTR("createPKZip")
49#define kBOMCopierOptionSequesterResourcesKey CFSTR("sequesterResources")
50#define kBOMCopierOptionKeepParentKey CFSTR("keepParent")
51#define kBOMCopierOptionCopyResourcesKey CFSTR("copyResources")
52
53namespace WebCore {
54
55void BlobDataFileReference::generateReplacementFile()
56{
57    ASSERT(m_replacementPath.isNull());
58    ASSERT(m_replacementShouldBeGenerated);
59
60    prepareForFileAccess();
61
62    RetainPtr<NSFileCoordinator> coordinator = adoptNS([[NSFileCoordinator alloc] initWithFilePresenter:nil]);
63    [coordinator coordinateReadingItemAtURL:[NSURL fileURLWithPath:m_path] options:NSFileCoordinatorReadingWithoutChanges error:nullptr byAccessor:^(NSURL *newURL) {
64        // The archive is put into a subdirectory of temporary directory for historic reasons. Changing this will require WebCore to change at the same time.
65        CString archivePath([NSTemporaryDirectory() stringByAppendingPathComponent:@"WebKitGeneratedFileXXXXXX"].fileSystemRepresentation);
66        if (!mktemp(archivePath.mutableData()))
67            return;
68
69        NSDictionary *options = @{
70            (__bridge id)kBOMCopierOptionCreatePKZipKey : @YES,
71            (__bridge id)kBOMCopierOptionSequesterResourcesKey : @YES,
72            (__bridge id)kBOMCopierOptionKeepParentKey : @YES,
73            (__bridge id)kBOMCopierOptionCopyResourcesKey : @YES,
74        };
75
76        BOMCopier copier = BOMCopierNew();
77        if (!BOMCopierCopyWithOptions(copier, newURL.path.fileSystemRepresentation, archivePath.data(), (__bridge CFDictionaryRef)options))
78            m_replacementPath = String::fromUTF8(archivePath);
79        BOMCopierFree(copier);
80    }];
81
82    m_replacementShouldBeGenerated = false;
83    if (!m_replacementPath.isNull()) {
84        FileMetadata metadata;
85        if (getFileMetadata(m_replacementPath, metadata))
86            m_size = metadata.length;
87    }
88
89    revokeFileAccess();
90}
91
92}
93
94#endif
95