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#include "File.h"
30#include <wtf/FileMetadata.h>
31#include <wtf/FileSystem.h>
32
33namespace WebCore {
34
35BlobDataFileReference::BlobDataFileReference(const String& path)
36 : m_path(path)
37{
38}
39
40BlobDataFileReference::~BlobDataFileReference()
41{
42#if ENABLE(FILE_REPLACEMENT)
43 if (!m_replacementPath.isNull())
44 FileSystem::deleteFile(m_replacementPath);
45#endif
46}
47
48const String& BlobDataFileReference::path()
49{
50#if ENABLE(FILE_REPLACEMENT)
51 if (m_replacementShouldBeGenerated)
52 generateReplacementFile();
53
54 if (!m_replacementPath.isNull())
55 return m_replacementPath;
56#endif
57
58 return m_path;
59}
60
61unsigned long long BlobDataFileReference::size()
62{
63#if ENABLE(FILE_REPLACEMENT)
64 if (m_replacementShouldBeGenerated)
65 generateReplacementFile();
66#endif
67
68 return m_size;
69}
70
71Optional<WallTime> BlobDataFileReference::expectedModificationTime()
72{
73#if ENABLE(FILE_REPLACEMENT)
74 // We do not currently track modifications for generated files, because we have a snapshot.
75 // Unfortunately, this is inconsistent with regular file handling - File objects should be invalidated when underlying files change.
76 if (m_replacementShouldBeGenerated || !m_replacementPath.isNull())
77 return WTF::nullopt;
78#endif
79 return m_expectedModificationTime;
80}
81
82void BlobDataFileReference::startTrackingModifications()
83{
84 // This is not done automatically by the constructor, because BlobDataFileReference is
85 // also used to pass paths around before registration. Only registered blobs need to pay
86 // the cost of tracking file modifications.
87
88#if ENABLE(FILE_REPLACEMENT)
89 m_replacementShouldBeGenerated = File::shouldReplaceFile(m_path);
90#endif
91
92 // FIXME: Some platforms provide better ways to listen for file system object changes, consider using these.
93 auto metadata = FileSystem::fileMetadataFollowingSymlinks(m_path);
94 if (!metadata)
95 return;
96
97 m_expectedModificationTime = metadata.value().modificationTime;
98
99#if ENABLE(FILE_REPLACEMENT)
100 if (m_replacementShouldBeGenerated)
101 return;
102#endif
103
104 m_size = metadata.value().length;
105}
106
107void BlobDataFileReference::prepareForFileAccess()
108{
109}
110
111void BlobDataFileReference::revokeFileAccess()
112{
113}
114
115}
116