Sleuth Kit Java Bindings (JNI) 4.14.0
Java bindings for using The Sleuth Kit
Loading...
Searching...
No Matches
LockResources.java
Go to the documentation of this file.
1/*
2 * Sleuth Kit Data Model
3 *
4 * Copyright 2024 Basis Technology Corp.
5 * Contact: carrier <at> sleuthkit <dot> org
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19package org.sleuthkit.datamodel;
20
21import java.io.File;
22import java.io.IOException;
23import java.io.RandomAccessFile;
24import java.nio.channels.FileChannel;
25import java.nio.channels.FileLock;
26import java.nio.channels.OverlappingFileLockException;
27
31class LockResources implements AutoCloseable {
32
33 private static final String LOCK_FILE_PREFIX = ".lock_";
34
35 private File lockFile = null;
36 private RandomAccessFile lockFileRaf = null;
37 private FileChannel lockFileChannel = null;
38 private FileLock lockFileLock = null;
39
48 LockResources(File lockFile, RandomAccessFile lockFileRaf, FileChannel lockFileChannel, FileLock lockFileLock) {
49 this.lockFile = lockFile;
50 this.lockFileRaf = lockFileRaf;
51 this.lockFileChannel = lockFileChannel;
52 this.lockFileLock = lockFileLock;
53 }
54
68 static LockResources tryAcquireFileLock(String caseDir, String dbName, String applicationName) throws ConcurrentDbAccessException, IOException, OverlappingFileLockException {
69 // get the lock file path
70 String lockFileName = LOCK_FILE_PREFIX + (dbName == null ? "tskdb" : dbName);
71 File lockFile = new File(caseDir, lockFileName);
72 // make directories leading up to that
73 lockFile.getParentFile().mkdirs();
74
75 // if the lock file exists
76 if (lockFile.isFile() && !lockFile.canWrite()) {
77 // get the random access file as read only
78 RandomAccessFile lockFileRaf = new RandomAccessFile(lockFile, "r");
79 throw ConcurrentDbAccessException.createForFile(lockFile.getAbsolutePath(), lockFileRaf);
80 } else {
81 RandomAccessFile lockFileRaf = new RandomAccessFile(lockFile, "rw");
82 FileChannel lockFileChannel = lockFileRaf.getChannel();
83 FileLock lockFileLock = lockFileChannel == null
84 ? null
85 : lockFileChannel.tryLock(1024L, 1L, false);
86
87 if (lockFileLock != null) {
88 lockFileRaf.setLength(0);
89 String limitedAppName = applicationName.length() > 500 ? applicationName.substring(0, 500) : applicationName;
90 lockFileRaf.writeChars(limitedAppName);
91 return new LockResources(lockFile, lockFileRaf, lockFileChannel, lockFileLock);
92 } else {
93 throw ConcurrentDbAccessException.createForFile(lockFile.getAbsolutePath(), lockFileRaf);
94 }
95 }
96 }
97
98 @Override
99 public void close() throws Exception {
100 // close lock file resources in reverse acquisition order
101 if (this.lockFileLock != null) {
102 this.lockFileLock.close();
103 this.lockFileLock = null;
104 }
105
106 if (this.lockFileChannel != null) {
107 this.lockFileChannel.close();
108 this.lockFileChannel = null;
109 }
110
111 if (this.lockFileRaf != null) {
112 this.lockFileRaf.close();
113 this.lockFileRaf = null;
114 }
115
116 if (this.lockFile != null) {
117 this.lockFile.delete();
118 this.lockFile = null;
119 }
120 }
121}

Copyright © 2011-2024 Brian Carrier. (carrier -at- sleuthkit -dot- org)
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.