| 1 | /* |
| 2 | * Copyright (C) 2013 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. ``AS IS'' AND ANY |
| 14 | * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 | * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 | * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 | * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 | * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | */ |
| 25 | |
| 26 | #include "config.h" |
| 27 | #include "OriginLock.h" |
| 28 | |
| 29 | namespace WebCore { |
| 30 | |
| 31 | String OriginLock::lockFileNameForPath(String originPath) |
| 32 | { |
| 33 | return FileSystem::pathByAppendingComponent(originPath, ".lock"_s ); |
| 34 | } |
| 35 | |
| 36 | OriginLock::OriginLock(String originPath) |
| 37 | : m_lockFileName(lockFileNameForPath(originPath).isolatedCopy()) |
| 38 | #if USE(FILE_LOCK) |
| 39 | , m_lockHandle(FileSystem::invalidPlatformFileHandle) |
| 40 | #endif |
| 41 | { |
| 42 | } |
| 43 | |
| 44 | OriginLock::~OriginLock() = default; |
| 45 | |
| 46 | void OriginLock::lock() |
| 47 | { |
| 48 | m_mutex.lock(); |
| 49 | |
| 50 | #if USE(FILE_LOCK) |
| 51 | m_lockHandle = FileSystem::openAndLockFile(m_lockFileName, FileSystem::FileOpenMode::Write); |
| 52 | if (m_lockHandle == FileSystem::invalidPlatformFileHandle) { |
| 53 | // The only way we can get here is if the directory containing the lock |
| 54 | // has been deleted or we were given a path to a non-existant directory. |
| 55 | // In that case, there's nothing we can do but cleanup and return. |
| 56 | m_mutex.unlock(); |
| 57 | return; |
| 58 | } |
| 59 | #endif |
| 60 | } |
| 61 | |
| 62 | void OriginLock::unlock() |
| 63 | { |
| 64 | #if USE(FILE_LOCK) |
| 65 | // If the file descriptor was uninitialized, then that means the directory |
| 66 | // containing the lock has been deleted before we opened the lock file, or |
| 67 | // we were given a path to a non-existant directory. Which, in turn, means |
| 68 | // that there's nothing to unlock. |
| 69 | if (m_lockHandle == FileSystem::invalidPlatformFileHandle) |
| 70 | return; |
| 71 | |
| 72 | FileSystem::unlockAndCloseFile(m_lockHandle); |
| 73 | m_lockHandle = FileSystem::invalidPlatformFileHandle; |
| 74 | #endif |
| 75 | |
| 76 | m_mutex.unlock(); |
| 77 | } |
| 78 | |
| 79 | void OriginLock::deleteLockFile(String originPath) |
| 80 | { |
| 81 | UNUSED_PARAM(originPath); |
| 82 | #if USE(FILE_LOCK) |
| 83 | String lockFileName = OriginLock::lockFileNameForPath(originPath); |
| 84 | FileSystem::deleteFile(lockFileName); |
| 85 | #endif |
| 86 | } |
| 87 | |
| 88 | } // namespace WebCore |
| 89 | |