Inside Bitcoin
Opening the selected file…
Inside Bitcoin
Opening the selected file…
The code behind the network, with room to understand it.
Release published 2026-10-28 · Snapshot fetched 2026-11-01. Latest stable release at fetch time; development branches and Git history are excluded. Only the selected code window and visible folders load here.
1// Copyright (c) 2011 The LevelDB Authors. All rights reserved.2// Use of this source code is governed by a BSD-style license that can be3// found in the LICENSE file. See the AUTHORS file for names of contributors.5#include "db/filename.h"7#include <ctype.h>8#include <stdio.h>10#include "db/dbformat.h"11#include "leveldb/env.h"12#include "util/logging.h"14namespace leveldb {16// A utility routine: write "data" to the named file and Sync() it.17Status WriteStringToFileSync(Env* env, const Slice& data,18const std::string& fname);20static std::string MakeFileName(const std::string& dbname, uint64_t number,21const char* suffix) {22char buf[100];23snprintf(buf, sizeof(buf), "/%06llu.%s",24static_cast<unsigned long long>(number), suffix);25return dbname + buf;26}28std::string LogFileName(const std::string& dbname, uint64_t number) {29assert(number > 0);30return MakeFileName(dbname, number, "log");31}33std::string TableFileName(const std::string& dbname, uint64_t number) {34assert(number > 0);35return MakeFileName(dbname, number, "ldb");36}38std::string SSTTableFileName(const std::string& dbname, uint64_t number) {39assert(number > 0);40return MakeFileName(dbname, number, "sst");41}43std::string DescriptorFileName(const std::string& dbname, uint64_t number) {44assert(number > 0);45char buf[100];46snprintf(buf, sizeof(buf), "/MANIFEST-%06llu",47static_cast<unsigned long long>(number));48return dbname + buf;49}51std::string CurrentFileName(const std::string& dbname) {52return dbname + "/CURRENT";53}55std::string LockFileName(const std::string& dbname) { return dbname + "/LOCK"; }57std::string TempFileName(const std::string& dbname, uint64_t number) {58assert(number > 0);59return MakeFileName(dbname, number, "dbtmp");60}62std::string InfoLogFileName(const std::string& dbname) {63return dbname + "/LOG";64}66// Return the name of the old info log file for "dbname".67std::string OldInfoLogFileName(const std::string& dbname) {68return dbname + "/LOG.old";69}71// Owned filenames have the form:72// dbname/CURRENT73// dbname/LOCK74// dbname/LOG75// dbname/LOG.old76// dbname/MANIFEST-[0-9]+77// dbname/[0-9]+.(log|sst|ldb)78bool ParseFileName(const std::string& filename, uint64_t* number,79FileType* type) {80Slice rest(filename);81if (rest == "CURRENT") {82*number = 0;83*type = kCurrentFile;84} else if (rest == "LOCK") {85*number = 0;86*type = kDBLockFile;87} else if (rest == "LOG" || rest == "LOG.old") {88*number = 0;89*type = kInfoLogFile;90} else if (rest.starts_with("MANIFEST-")) {91rest.remove_prefix(strlen("MANIFEST-"));92uint64_t num;93if (!ConsumeDecimalNumber(&rest, &num)) {94return false;95}96if (!rest.empty()) {97return false;98}99*type = kDescriptorFile;100*number = num;101} else {102// Avoid strtoull() to keep filename format independent of the103// current locale104uint64_t num;105if (!ConsumeDecimalNumber(&rest, &num)) {106return false;107}108Slice suffix = rest;109if (suffix == Slice(".log")) {110*type = kLogFile;111} else if (suffix == Slice(".sst") || suffix == Slice(".ldb")) {112*type = kTableFile;113} else if (suffix == Slice(".dbtmp")) {114*type = kTempFile;115} else {116return false;117}118*number = num;119}120return true;121}123Status SetCurrentFile(Env* env, const std::string& dbname,124uint64_t descriptor_number) {125// Remove leading "dbname/" and add newline to manifest file name126std::string manifest = DescriptorFileName(dbname, descriptor_number);127Slice contents = manifest;128assert(contents.starts_with(dbname + "/"));129contents.remove_prefix(dbname.size() + 1);130std::string tmp = TempFileName(dbname, descriptor_number);131Status s = WriteStringToFileSync(env, contents.ToString() + "\n", tmp);132if (s.ok()) {133s = env->RenameFile(tmp, CurrentFileName(dbname));134}135if (!s.ok()) {136env->DeleteFile(tmp);137}138return s;139}141} // namespace leveldb
Independent explanations by MyBitcoinExplorer. Not affiliated with or endorsed by Bitcoin Core. Guides are educational and do not replace the source or its tests.
Bitcoin Core source © its contributors, distributed under the MIT license. Original notices and bundled third-party licenses are retained in the source tree.