<commit_before>/*
 * Copyright (c) 2010-2011 by <NAME>, Zuse Institute Berlin
 *               2011-2012 by <NAME>, Zuse Institute Berlin
 *
 * Licensed under the BSD License, see LICENSE file for details.
 *
 */

#include "libxtreemfs/helper.h"

#include <cstdio>
#include <cstdlib>
#include <stdint.h>
#ifdef __APPLE__
#include <sys/utsname.h>
#endif  // __APPLE__
#ifdef WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif  // WIN32

#include <boost/lexical_cast.hpp>
#include <string>

#include "libxtreemfs/options.h"
#include "libxtreemfs/xtreemfs_exception.h"
#include "rpc/sync_callback.h"
#include "util/logging.h"
#include "xtreemfs/GlobalTypes.pb.h"
#include "xtreemfs/MRC.pb.h"
#include "xtreemfs/OSD.pb.h"

using namespace std;
using namespace xtreemfs::pbrpc;
using namespace xtreemfs::util;

namespace xtreemfs {

int CompareOSDWriteResponses(
    const xtreemfs::pbrpc::OSDWriteResponse* new_response,
    const xtreemfs::pbrpc::OSDWriteResponse* current_response) {
  if (new_response == NULL && current_response == NULL) {
    return 0;
  } else if (new_response != NULL && current_response == NULL) {
    // new_response > current_response.
    return 1;
  } else if (new_response == NULL && current_response != NULL) {
    // new_response < current_response.
    return -1;
  } else if (
      new_response->truncate_epoch() > current_response->truncate_epoch() ||
      (new_response->truncate_epoch() == current_response->truncate_epoch()
       && new_response->size_in_bytes() > current_response->size_in_bytes())) {
    // new_response > current_response.
    return 1;
  } else if (
      new_response->truncate_epoch() < current_response->truncate_epoch() ||
      (new_response->truncate_epoch() == current_response->truncate_epoch()
       && new_response->size_in_bytes() < current_response->size_in_bytes())) {
    // new_response < current_response.
    return -1;
  } else {
    // new_response == current_response.
    return 0;
  }
}

/** The XCap contains the Volume UUID and File ID concatenated by a ":". */
uint64_t ExtractFileIdFromXCap(const xtreemfs::pbrpc::XCap& xcap) {
  string string = xcap.file_id();

  int start = string.find(":") + 1;
  int length = string.length() - start;
  return boost::lexical_cast<uint64_t>(
      string.substr(start, length));
}

std::string ResolveParentDirectory(const std::string& path) {
  int last_slash = path.find_last_of("/");
  if (path == "/" || last_slash == 0) {
    return "/";
  } else {
    return path.substr(0, last_slash);
  }
}

std::string GetBasename(const std::string& path) {
  int last_slash = path.find_last_of("/");
  if (path == "/") {
    return "/";
  } else {
    // We don't allow path to have a trailing "/".
    assert(last_slash != (path.length() - 1));

    return path.substr(last_slash + 1);
  }
}

std::string ConcatenatePath(const std::string& directory,
                            const std::string& file) {
  // handle .. and .
  if (file == ".") {
    return directory;
  } else if (file == "..") {
    if (directory == "/") {
      return directory;
    }
    return directory.substr(0, directory.find_last_of("/"));
  }

  if (directory == "/") {
    return "/" + file;
  } else {
    return directory + "/" + file;
  }
}

std::string GetOSDUUIDFromXlocSet(const xtreemfs::pbrpc::XLocSet& xlocs,
                                  uint32_t replica_index,
                                  uint32_t stripe_index) {
  if (xlocs.replicas_size() == 0) {
    Logging::log->getLog(LEVEL_ERROR)
       << "GetOSDUUIDFromXlocSet: Empty replicas list in XlocSet: "
       <<  xlocs.DebugString() << endl;
    return "";
  }

  const xtreemfs::pbrpc::Replica& replica = xlocs.replicas(replica_index);
  if (replica.osd_uuids_size() == 0) {
    Logging::log->getLog(LEVEL_ERROR)
        << "GetOSDUUIDFromXlocSet: No head OSD available in XlocSet:"
        <<  xlocs.DebugString() << endl;
    return "";
  }

  return replica.osd_uuids(stripe_index);
}

std::string GetOSDUUIDFromXlocSet(
    const xtreemfs::pbrpc::XLocSet& xlocs) {
  // Get the UUID for the first replica (r=0) and the head OSD (i.e. the first
  // stripe, s=0).
  return GetOSDUUIDFromXlocSet(xlocs, 0, 0);
}

/**
 * By default this function does read random data from /dev/urandom and falls
 * back to using C's rand() if /dev/random is not available.
 */
void GenerateVersion4UUID(std::string* result) {
  FILE *urandom = fopen("/dev/urandom", "r");
  if (!urandom) {
    // Use rand() instead if /dev/urandom not available.<commit_msg>client: helper.cpp: Fixed wrong comment where the word "stripe" was used, but "chunk" was meant.<commit_after>/*
 * Copyright (c) 2010-2011 by <NAME>, Zuse Institute Berlin
 *               2011-2012 by <NAME>, Zuse Institute Berlin
 *
 * Licensed under the BSD License, see LICENSE file for details.
 *
 */

#include "libxtreemfs/helper.h"

#include <cstdio>
#include <cstdlib>
#include <stdint.h>
#ifdef __APPLE__
#include <sys/utsname.h>
#endif  // __APPLE__
#ifdef WIN32
#define NOMINMAX
#define WIN32_LEAN_AND_MEAN
#include <windows.h>
#endif  // WIN32

#include <boost/lexical_cast.hpp>
#include <string>

#include "libxtreemfs/options.h"
#include "libxtreemfs/xtreemfs_exception.h"
#include "rpc/sync_callback.h"
#include "util/logging.h"
#include "xtreemfs/GlobalTypes.pb.h"
#include "xtreemfs/MRC.pb.h"
#include "xtreemfs/OSD.pb.h"

using namespace std;
using namespace xtreemfs::pbrpc;
using namespace xtreemfs::util;

namespace xtreemfs {

int CompareOSDWriteResponses(
    const xtreemfs::pbrpc::OSDWriteResponse* new_response,
    const xtreemfs::pbrpc::OSDWriteResponse* current_response) {
  if (new_response == NULL && current_response == NULL) {
    return 0;
  } else if (new_response != NULL && current_response == NULL) {
    // new_response > current_response.
    return 1;
  } else if (new_response == NULL && current_response != NULL) {
    // new_response < current_response.
    return -1;
  } else if (
      new_response->truncate_epoch() > current_response->truncate_epoch() ||
      (new_response->truncate_epoch() == current_response->truncate_epoch()
       && new_response->size_in_bytes() > current_response->size_in_bytes())) {
    // new_response > current_response.
    return 1;
  } else if (
      new_response->truncate_epoch() < current_response->truncate_epoch() ||
      (new_response->truncate_epoch() == current_response->truncate_epoch()
       && new_response->size_in_bytes() < current_response->size_in_bytes())) {
    // new_response < current_response.
    return -1;
  } else {
    // new_response == current_response.
    return 0;
  }
}

/** The XCap contains the Volume UUID and File ID concatenated by a ":". */
uint64_t ExtractFileIdFromXCap(const xtreemfs::pbrpc::XCap& xcap) {
  string string = xcap.file_id();

  int start = string.find(":") + 1;
  int length = string.length() - start;
  return boost::lexical_cast<uint64_t>(
      string.substr(start, length));
}

std::string ResolveParentDirectory(const std::string& path) {
  int last_slash = path.find_last_of("/");
  if (path == "/" || last_slash == 0) {
    return "/";
  } else {
    return path.substr(0, last_slash);
  }
}

std::string GetBasename(const std::string& path) {
  int last_slash = path.find_last_of("/");
  if (path == "/") {
    return "/";
  } else {
    // We don't allow path to have a trailing "/".
    assert(last_slash != (path.length() - 1));

    return path.substr(last_slash + 1);
  }
}

std::string ConcatenatePath(const std::string& directory,
                            const std::string& file) {
  // handle .. and .
  if (file == ".") {
    return directory;
  } else if (file == "..") {
    if (directory == "/") {
      return directory;
    }
    return directory.substr(0, directory.find_last_of("/"));
  }

  if (directory == "/") {
    return "/" + file;
  } else {
    return directory + "/" + file;
  }
}

std::string GetOSDUUIDFromXlocSet(const xtreemfs::pbrpc::XLocSet& xlocs,
                                  uint32_t replica_index,
                                  uint32_t stripe_index) {
  if (xlocs.replicas_size() == 0) {
    Logging::log->getLog(LEVEL_ERROR)
       << "GetOSDUUIDFromXlocSet: Empty replicas list in XlocSet: "
       <<  xlocs.DebugString() << endl;
    return "";
  }

  const xtreemfs::pbrpc::Replica& replica = xlocs.replicas(replica_index);
  if (replica.osd_uuids_size() == 0) {
    Logging::log->getLog(LEVEL_ERROR)
        << "GetOSDUUIDFromXlocSet: No head OSD available in XlocSet:"
        <<  xlocs.DebugString() << endl;
    return "";
  }

  return replica.osd_uuids(stripe_index);
}

std::string GetOSDUUIDFromXlocSet(
    const xtreemfs::pbrpc::XLocSet& xlocs) {
  // Get the UUID for the first replica (r=0) and the head OSD (i.e. the first
  // chunk, c=0).
  return GetOSDUUIDFromXlocSet(xlocs, 0, 0);
}

/**
 * By default this function does read random data from /dev/urandom and falls
 * back to using C's rand() if /dev/random is not available.
 */
void GenerateVersion4UUID(std::string* result) {
  FILE *urandom = fopen("/dev/urandom", "r");
  if (!urandom) {
    // Use rand() instead if /dev/urandom not available.
{'fraction_non_alphanumeric': 0.14383860414394767, 'fraction_numerical': 0.010687022900763359, 'mean_word_length': 3.3361702127659574, 'pattern_counts': {'":': 4, '<': 55, '<?xml version=': 0, '>': 59, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6620934', 'n_tokens_mistral': 3284, 'n_tokens_neox': 3181, 'n_words': 713}