sapling/eden/fs/win/mount/RepoConfig.cpp
Puneet Kaushik 991965ecae Update RepoConfig to store Mount id in .eden/config
Summary: This diff creates a Guid as a unique mount id. This mount id will be used to uniquely identify our repo in the Regdb.

Reviewed By: chadaustin

Differential Revision: D18055798

fbshipit-source-id: 01099d7fdf15c325498e83c8b63637dd1d85d028
2019-10-24 16:26:54 -07:00

72 lines
2.2 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#include "folly/portability/Windows.h"
#include <cpptoml.h>
#include <iostream>
#include <memory>
#include <sstream>
#include "eden/fs/win/mount/RepoConfig.h"
#include "eden/fs/win/utils/FileUtils.h"
#include "eden/fs/win/utils/Guid.h"
#include "eden/fs/win/utils/WinError.h"
const std::string kConfigRootPath{"root"};
const std::string kConfigSocketPath{"socket"};
const std::string kConfigClientPath{"client"};
const std::string kConfigMountId{"mountid"};
const std::string kConfigTable{"Config"};
namespace facebook {
namespace eden {
void createRepoConfig(
const AbsolutePath& repoPath,
const AbsolutePath& socket,
const AbsolutePath& client) {
const auto dotEden{repoPath + ".eden"_pc};
if (!CreateDirectoryA(dotEden.c_str(), nullptr)) {
DWORD error = GetLastError();
if (error != ERROR_ALREADY_EXISTS) {
throw makeWin32ErrorExplicit(error, "Failed to create the .eden");
}
} else {
// We should only write this config file once, otherwise it would overwrite
// the mount id.
const auto configFile{dotEden + "config"_pc};
std::shared_ptr<cpptoml::table> rootTable = cpptoml::make_table();
auto configTable = cpptoml::make_table();
configTable->insert(kConfigRootPath, repoPath.c_str());
configTable->insert(kConfigSocketPath, socket.c_str());
configTable->insert(kConfigClientPath, client.c_str());
configTable->insert(kConfigMountId, Guid::generate().toString());
rootTable->insert(kConfigTable, configTable);
std::stringstream stream;
stream << (*rootTable);
std::string contents = stream.str();
writeFile(configFile.c_str(), contents);
}
}
std::string getMountId(const std::string& repoPath) {
std::string configPath{repoPath + "\\.eden\\config"};
auto configRoot = cpptoml::parse_file(configPath);
auto config = configRoot->get_table(kConfigTable);
auto id = config->get_as<std::string>(kConfigMountId);
if (!id) {
throw std::logic_error("Mount id config missing");
}
return *id;
}
} // namespace eden
} // namespace facebook