Skip to content

Commit 506a314

Browse files
committed
Create .lock file to prevent re-mounting
1 parent ba3480f commit 506a314

1 file changed

Lines changed: 44 additions & 4 deletions

File tree

chunkdisk.cpp

Lines changed: 44 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -501,6 +501,34 @@ struct ChunkDisk
501501
if (storage_unit != nullptr) SpdStorageUnitDelete(storage_unit);
502502
}
503503

504+
DWORD LockParts()
505+
{
506+
auto num_parts = part_dirname.size();
507+
508+
try
509+
{
510+
for (size_t i = 0; i < num_parts; ++i)
511+
{
512+
auto path = part_dirname[i] + L"\\.lock";
513+
auto h = FileHandle(CreateFileW(
514+
path.data(),
515+
GENERIC_READ | GENERIC_WRITE,
516+
0, nullptr,
517+
CREATE_NEW,
518+
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_DELETE_ON_CLOSE, nullptr));
519+
if (!h) return GetLastError();
520+
521+
part_lock_.emplace_back(std::move(h));
522+
}
523+
}
524+
catch (const bad_alloc&)
525+
{
526+
return ERROR_NOT_ENOUGH_MEMORY;
527+
}
528+
529+
return ERROR_SUCCESS;
530+
}
531+
504532
// read parts and chunks, check consistency
505533
DWORD ReadParts()
506534
{
@@ -1001,6 +1029,8 @@ struct ChunkDisk
10011029
const u32 max_pages = 1; // may exceed if page is being used for I/O
10021030

10031031
private:
1032+
vector<FileHandle> part_lock_; // part index -> .lock
1033+
10041034
SRWLOCK lock_parts_ = SRWLOCK_INIT;
10051035
vector<u64> part_current_; // part index -> # of chunks
10061036
size_t part_current_new_ = 0; // part index for new chunks
@@ -1790,23 +1820,33 @@ int wmain(int argc, wchar_t** argv)
17901820

17911821
unique_ptr<ChunkDisk> cdisk;
17921822
err = ReadChunkDiskFile(ChunkDiskFile, NumThreads, cdisk);
1793-
if (err != ERROR_SUCCESS) {
1823+
if (err != ERROR_SUCCESS)
1824+
{
17941825
logerr(L"error: parsing failed with error %lu", err);
17951826
return err;
17961827
}
1828+
err = cdisk->LockParts();
1829+
if (err != ERROR_SUCCESS)
1830+
{
1831+
logerr(L"error: cannot lock parts: error %lu", err);
1832+
return err;
1833+
}
17971834
err = cdisk->ReadParts();
1798-
if (err != ERROR_SUCCESS) {
1835+
if (err != ERROR_SUCCESS)
1836+
{
17991837
logerr(L"error: cannot initialize ChunkDisk: error %lu", err);
18001838
return err;
18011839
}
18021840
err = CreateChunkDiskStorageUnit(cdisk.get(), !WriteAllowed, PipeName);
1803-
if (err != ERROR_SUCCESS) {
1841+
if (err != ERROR_SUCCESS)
1842+
{
18041843
logerr(L"error: cannot create ChunkDisk: error %lu", err);
18051844
return err;
18061845
}
18071846
SpdStorageUnitSetDebugLog(cdisk->storage_unit, DebugFlags);
18081847
err = SpdStorageUnitStartDispatcher(cdisk->storage_unit, NumThreads);
1809-
if (err != ERROR_SUCCESS) {
1848+
if (err != ERROR_SUCCESS)
1849+
{
18101850
logerr(L"error: cannot start ChunkDisk: error %lu", err);
18111851
return err;
18121852
}

0 commit comments

Comments
 (0)