github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/c-deps/libroach/utils.cc (about)

     1  // Copyright 2017 The Cockroach Authors.
     2  //
     3  // Use of this software is governed by the Business Source License
     4  // included in the file licenses/BSL.txt.
     5  //
     6  // As of the Change Date specified in that file, in accordance with
     7  // the Business Source License, use of this software will be governed
     8  // by the Apache License, Version 2.0, included in the file
     9  // licenses/APL.txt.
    10  
    11  #include "utils.h"
    12  
    13  static const std::string kTempFileNameSuffix = ".crdbtmp";
    14  
    15  rocksdb::Status SafeWriteStringToFile(rocksdb::Env* env, rocksdb::Directory* dir,
    16                                        const std::string& filename, const std::string& contents) {
    17    std::string tmpname = filename + kTempFileNameSuffix;
    18    auto status = rocksdb::WriteStringToFile(env, contents, tmpname, true /* should_sync */);
    19    if (status.ok()) {
    20      status = env->RenameFile(tmpname, filename);
    21    }
    22    if (!status.ok()) {
    23      env->DeleteFile(tmpname);
    24      return status;
    25    }
    26    return dir->Fsync();
    27  }
    28  
    29  std::string PathAppend(const std::string& path1, const std::string& path2) {
    30    if (path2.size() == 0) {
    31      return path1;
    32    }
    33    if ((path1.size() > 0 && path1[path1.size() - 1] == '/') || path2[0] == '/') {
    34      // Separator is present at end of path1, or beginning of path2).
    35      return path1 + path2;
    36    }
    37    return path1 + '/' + path2;
    38  }