<commit_before>/**
 * This file is part of the CernVM File System.
 */

#ifndef CVMFS_SSL_H_
#define CVMFS_SSL_H_

#include "ssl.h"

#include <cstdlib>
#include <string>
#include <vector>

#include "duplex_curl.h"
#include "util/posix.h"

#ifdef CVMFS_NAMESPACE_GUARD
namespace CVMFS_NAMESPACE_GUARD {
#endif

unsigned int count_ssl_certificates(std::string directory) {
  if (!DirectoryExists(directory)) return 0;

  std::vector<std::string> dotpem = FindFilesBySuffix(directory, ".pem");
  std::vector<std::string> dotcrt = FindFilesBySuffix(directory, ".crt");

  return dotpem.size() + dotcrt.size();
}

bool AddSSLCertificates(CURL *handle) {
  bool add_system_certificates = false;
  std::vector<std::string> cadirs;
  const char *cadir = getenv("X509_CERT_DIR");
  if (!cadir || !*cadir) {
    cadir = "/etc/grid-security/certificates";
  }
  bool certificate_already_added = false;
  if (count_ssl_certificates(cadir) > 0) {
      CURLcode res = curl_easy_setopt(handle, CURLOPT_CAPATH, cadir);
      if (CURLE_OK == res) {
        certificate_already_added = true;
      }
  }
  const char *cabundle = getenv("X509_CERT_BUNDLE");
  if (cabundle && *cabundle) {
    CURLcode res = curl_easy_setopt(handle, CURLOPT_CAINFO, cabundle);
    if (CURLE_OK == res) {
      certificate_already_added = true;
    }
  }

  if (certificate_already_added == false && add_system_certificates) {
    // most systems store the certificates here
    cadirs.push_back("/etc/ssl/certs/");

    cadirs.push_back("/etc/pki/tls/certs/");
    cadirs.push_back("/etc/ssl/");
    cadirs.push_back("/etc/pki/tls/");
    cadirs.push_back("/etc/pki/ca-trust/extracted/pem/");
    cadirs.push_back("/etc/ssl/");

    for (std::vector<std::string>::const_iterator cadir = cadirs.begin();
         cadir != cadirs.end(); ++cadir) {
      if (count_ssl_certificates(*cadir) > 0) {
        CURLcode res =
            curl_easy_setopt(handle, CURLOPT_CAPATH, (*cadir).c_str());
        if (CURLE_OK == res) {<commit_msg>remane function count_ssl_certificates -> HasCertificates<commit_after>/**
 * This file is part of the CernVM File System.
 */

#ifndef CVMFS_SSL_H_
#define CVMFS_SSL_H_

#include "ssl.h"

#include <cstdlib>
#include <string>
#include <vector>

#include "duplex_curl.h"
#include "util/posix.h"

#ifdef CVMFS_NAMESPACE_GUARD
namespace CVMFS_NAMESPACE_GUARD {
#endif

bool HasCertificates(std::string directory) {
  if (!DirectoryExists(directory)) return 0;

  std::vector<std::string> dotpem = FindFilesBySuffix(directory, ".pem");
  std::vector<std::string> dotcrt = FindFilesBySuffix(directory, ".crt");

  return (dotpem.size() + dotcrt.size()) > 0;
}

bool AddSSLCertificates(CURL *handle) {
  bool add_system_certificates = false;
  std::vector<std::string> cadirs;
  const char *cadir = getenv("X509_CERT_DIR");
  if (!cadir || !*cadir) {
    cadir = "/etc/grid-security/certificates";
  }
  bool certificate_already_added = false;
  if (HasCertificates(cadir)) {
      CURLcode res = curl_easy_setopt(handle, CURLOPT_CAPATH, cadir);
      if (CURLE_OK == res) {
        certificate_already_added = true;
      }
  }
  const char *cabundle = getenv("X509_CERT_BUNDLE");
  if (cabundle && *cabundle) {
    CURLcode res = curl_easy_setopt(handle, CURLOPT_CAINFO, cabundle);
    if (CURLE_OK == res) {
      certificate_already_added = true;
    }
  }

  if (certificate_already_added == false && add_system_certificates) {
    // most systems store the certificates here
    cadirs.push_back("/etc/ssl/certs/");

    cadirs.push_back("/etc/pki/tls/certs/");
    cadirs.push_back("/etc/ssl/");
    cadirs.push_back("/etc/pki/tls/");
    cadirs.push_back("/etc/pki/ca-trust/extracted/pem/");
    cadirs.push_back("/etc/ssl/");

    for (std::vector<std::string>::const_iterator cadir = cadirs.begin();
         cadir != cadirs.end(); ++cadir) {
      if (HasCertificates(*cadir)) {
        CURLcode res =
            curl_easy_setopt(handle, CURLOPT_CAPATH, (*cadir).c_str());
        if (CURLE_OK == res) {
{'fraction_non_alphanumeric': 0.14656679151061175, 'fraction_numerical': 0.004244694132334582, 'mean_word_length': 4.001248439450687, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4063222', 'n_tokens_mistral': 1523, 'n_tokens_neox': 1461, 'n_words': 269}