LCOV - code coverage report
Current view: top level - src/leveldb/db - filename.cc (source / functions) Coverage Total Hit
Test: test_bitcoin_coverage.info Lines: 82.6 % 69 57
Test Date: 2026-02-04 04:43:42 Functions: 75.0 % 12 9
Branches: 53.0 % 66 35

             Branch data     Line data    Source code
       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 be
       3                 :             : // found in the LICENSE file. See the AUTHORS file for names of contributors.
       4                 :             : 
       5                 :             : #include "db/filename.h"
       6                 :             : 
       7                 :             : #include <ctype.h>
       8                 :             : #include <stdio.h>
       9                 :             : 
      10                 :             : #include "db/dbformat.h"
      11                 :             : #include "leveldb/env.h"
      12                 :             : #include "util/logging.h"
      13                 :             : 
      14                 :             : namespace leveldb {
      15                 :             : 
      16                 :             : // A utility routine: write "data" to the named file and Sync() it.
      17                 :             : Status WriteStringToFileSync(Env* env, const Slice& data,
      18                 :             :                              const std::string& fname);
      19                 :             : 
      20                 :        1697 : static std::string MakeFileName(const std::string& dbname, uint64_t number,
      21                 :             :                                 const char* suffix) {
      22                 :        1697 :   char buf[100];
      23                 :        1697 :   snprintf(buf, sizeof(buf), "/%06llu.%s",
      24                 :             :            static_cast<unsigned long long>(number), suffix);
      25                 :        1697 :   return dbname + buf;
      26                 :             : }
      27                 :             : 
      28                 :         558 : std::string LogFileName(const std::string& dbname, uint64_t number) {
      29         [ -  + ]:         558 :   assert(number > 0);
      30                 :         558 :   return MakeFileName(dbname, number, "log");
      31                 :             : }
      32                 :             : 
      33                 :         229 : std::string TableFileName(const std::string& dbname, uint64_t number) {
      34         [ -  + ]:         229 :   assert(number > 0);
      35                 :         229 :   return MakeFileName(dbname, number, "ldb");
      36                 :             : }
      37                 :             : 
      38                 :           0 : std::string SSTTableFileName(const std::string& dbname, uint64_t number) {
      39         [ #  # ]:           0 :   assert(number > 0);
      40                 :           0 :   return MakeFileName(dbname, number, "sst");
      41                 :             : }
      42                 :             : 
      43                 :        1820 : std::string DescriptorFileName(const std::string& dbname, uint64_t number) {
      44         [ -  + ]:        1820 :   assert(number > 0);
      45                 :        1820 :   char buf[100];
      46                 :        1820 :   snprintf(buf, sizeof(buf), "/MANIFEST-%06llu",
      47                 :             :            static_cast<unsigned long long>(number));
      48                 :        1820 :   return dbname + buf;
      49                 :             : }
      50                 :             : 
      51                 :        1888 : std::string CurrentFileName(const std::string& dbname) {
      52                 :        1888 :   return dbname + "/CURRENT";
      53                 :             : }
      54                 :             : 
      55                 :         507 : std::string LockFileName(const std::string& dbname) { return dbname + "/LOCK"; }
      56                 :             : 
      57                 :         910 : std::string TempFileName(const std::string& dbname, uint64_t number) {
      58         [ -  + ]:         910 :   assert(number > 0);
      59                 :         910 :   return MakeFileName(dbname, number, "dbtmp");
      60                 :             : }
      61                 :             : 
      62                 :           0 : std::string InfoLogFileName(const std::string& dbname) {
      63                 :           0 :   return dbname + "/LOG";
      64                 :             : }
      65                 :             : 
      66                 :             : // Return the name of the old info log file for "dbname".
      67                 :           0 : std::string OldInfoLogFileName(const std::string& dbname) {
      68                 :           0 :   return dbname + "/LOG.old";
      69                 :             : }
      70                 :             : 
      71                 :             : // Owned filenames have the form:
      72                 :             : //    dbname/CURRENT
      73                 :             : //    dbname/LOCK
      74                 :             : //    dbname/LOG
      75                 :             : //    dbname/LOG.old
      76                 :             : //    dbname/MANIFEST-[0-9]+
      77                 :             : //    dbname/[0-9]+.(log|sst|ldb)
      78                 :        4277 : bool ParseFileName(const std::string& filename, uint64_t* number,
      79                 :             :                    FileType* type) {
      80         [ -  + ]:        4277 :   Slice rest(filename);
      81         [ +  + ]:        4277 :   if (rest == "CURRENT") {
      82                 :        1008 :     *number = 0;
      83                 :        1008 :     *type = kCurrentFile;
      84         [ +  + ]:        3269 :   } else if (rest == "LOCK") {
      85                 :         259 :     *number = 0;
      86                 :         259 :     *type = kDBLockFile;
      87   [ +  -  -  + ]:        3010 :   } else if (rest == "LOG" || rest == "LOG.old") {
      88                 :           0 :     *number = 0;
      89                 :           0 :     *type = kInfoLogFile;
      90         [ +  + ]:        3010 :   } else if (rest.starts_with("MANIFEST-")) {
      91                 :        1497 :     rest.remove_prefix(strlen("MANIFEST-"));
      92                 :        1497 :     uint64_t num;
      93         [ +  - ]:        1497 :     if (!ConsumeDecimalNumber(&rest, &num)) {
      94                 :             :       return false;
      95                 :             :     }
      96         [ +  - ]:        1497 :     if (!rest.empty()) {
      97                 :             :       return false;
      98                 :             :     }
      99                 :        1497 :     *type = kDescriptorFile;
     100                 :        1497 :     *number = num;
     101                 :             :   } else {
     102                 :             :     // Avoid strtoull() to keep filename format independent of the
     103                 :             :     // current locale
     104                 :        1513 :     uint64_t num;
     105         [ +  + ]:        1513 :     if (!ConsumeDecimalNumber(&rest, &num)) {
     106                 :             :       return false;
     107                 :             :     }
     108                 :         968 :     Slice suffix = rest;
     109         [ +  + ]:         968 :     if (suffix == Slice(".log")) {
     110                 :         656 :       *type = kLogFile;
     111   [ +  -  -  + ]:         312 :     } else if (suffix == Slice(".sst") || suffix == Slice(".ldb")) {
     112                 :         312 :       *type = kTableFile;
     113         [ #  # ]:           0 :     } else if (suffix == Slice(".dbtmp")) {
     114                 :           0 :       *type = kTempFile;
     115                 :             :     } else {
     116                 :             :       return false;
     117                 :             :     }
     118                 :         968 :     *number = num;
     119                 :             :   }
     120                 :             :   return true;
     121                 :             : }
     122                 :             : 
     123                 :         910 : Status SetCurrentFile(Env* env, const std::string& dbname,
     124                 :             :                       uint64_t descriptor_number) {
     125                 :             :   // Remove leading "dbname/" and add newline to manifest file name
     126                 :         910 :   std::string manifest = DescriptorFileName(dbname, descriptor_number);
     127         [ -  + ]:         910 :   Slice contents = manifest;
     128   [ +  -  -  + ]:        1820 :   assert(contents.starts_with(dbname + "/"));
     129         [ -  + ]:         910 :   contents.remove_prefix(dbname.size() + 1);
     130         [ +  - ]:         910 :   std::string tmp = TempFileName(dbname, descriptor_number);
     131   [ +  -  +  -  :        2730 :   Status s = WriteStringToFileSync(env, contents.ToString() + "\n", tmp);
             -  +  +  - ]
     132         [ +  - ]:         910 :   if (s.ok()) {
     133   [ +  -  +  -  :         910 :     s = env->RenameFile(tmp, CurrentFileName(dbname));
                   -  + ]
     134                 :             :   }
     135         [ -  + ]:         910 :   if (!s.ok()) {
     136         [ #  # ]:           0 :     env->DeleteFile(tmp);
     137                 :             :   }
     138                 :         910 :   return s;
     139                 :         910 : }
     140                 :             : 
     141                 :             : }  // namespace leveldb
        

Generated by: LCOV version 2.0-1