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#if ENABLE(SQL_DATABASE) 30 31#include "FileSystem.h" 32#include <wtf/PassOwnPtr.h> 33 34namespace WebCore { 35 36String OriginLock::lockFileNameForPath(String originPath) 37{ 38 return pathByAppendingComponent(originPath, String(".lock")); 39} 40 41OriginLock::OriginLock(String originPath) 42 : m_lockFileName(lockFileNameForPath(originPath).isolatedCopy()) 43#if USE(FILE_LOCK) 44 , m_lockHandle(invalidPlatformFileHandle) 45#endif 46{ 47} 48 49OriginLock::~OriginLock() 50{ 51} 52 53void OriginLock::lock() 54{ 55 m_mutex.lock(); 56 57#if USE(FILE_LOCK) 58 m_lockHandle = openFile(m_lockFileName, OpenForWrite); 59 if (m_lockHandle == invalidPlatformFileHandle) { 60 // The only way we can get here is if the directory containing the lock 61 // has been deleted or we were given a path to a non-existant directory. 62 // In that case, there's nothing we can do but cleanup and return. 63 m_mutex.unlock(); 64 return; 65 } 66 67 lockFile(m_lockHandle, LockExclusive); 68#endif 69} 70 71void OriginLock::unlock() 72{ 73#if USE(FILE_LOCK) 74 // If the file descriptor was uninitialized, then that means the directory 75 // containing the lock has been deleted before we opened the lock file, or 76 // we were given a path to a non-existant directory. Which, in turn, means 77 // that there's nothing to unlock. 78 if (m_lockHandle == invalidPlatformFileHandle) 79 return; 80 81 unlockFile(m_lockHandle); 82 83 closeFile(m_lockHandle); 84 m_lockHandle = invalidPlatformFileHandle; 85#endif 86 87 m_mutex.unlock(); 88} 89 90void OriginLock::deleteLockFile(String originPath) 91{ 92 UNUSED_PARAM(originPath); 93#if USE(FILE_LOCK) 94 String lockFileName = OriginLock::lockFileNameForPath(originPath); 95 deleteFile(lockFileName); 96#endif 97} 98 99} // namespace WebCore 100 101#endif // ENABLE(SQL_DATABASE) 102