<commit_before>#pragma once

#include "constants.hpp"
#include "filesystem.hpp"
#include <fcntl.h>
#include <sstream>
#include <string>
#include <sys/file.h>

namespace krbn {
class process_utility final {
public:
  static bool lock_single_application(const std::string& pid_file_path) {
    auto pid_directory = filesystem::dirname(pid_file_path);
    if (pid_directory.empty()) {
      throw std::runtime_error("failed to get user pid directory");
    }

    filesystem::create_directory_with_intermediate_directories(pid_directory, 0700);
    if (!filesystem::is_directory(pid_directory)) {
      throw std::runtime_error("failed to create pid directory");
    }

    auto fd = open(pid_file_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0600);
    if (fd < 0) {
      throw std::runtime_error("failed to create pid file");
    }

    if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
      get_single_application_lock_pid_file_descriptor() = fd;

      std::stringstream ss;
      ss << getpid() << std::endl;
      auto string = ss.str();
      write(fd, string.c_str(), string.size());

      return true;
    }<commit_msg>change pid directory permission (0700 -> 0755)<commit_after>#pragma once

#include "constants.hpp"
#include "filesystem.hpp"
#include <fcntl.h>
#include <sstream>
#include <string>
#include <sys/file.h>

namespace krbn {
class process_utility final {
public:
  static bool lock_single_application(const std::string& pid_file_path) {
    auto pid_directory = filesystem::dirname(pid_file_path);
    if (pid_directory.empty()) {
      throw std::runtime_error("failed to get user pid directory");
    }

    filesystem::create_directory_with_intermediate_directories(pid_directory, 0755);
    if (!filesystem::is_directory(pid_directory)) {
      throw std::runtime_error("failed to create pid directory");
    }

    auto fd = open(pid_file_path.c_str(), O_WRONLY | O_CREAT | O_TRUNC, 0644);
    if (fd < 0) {
      throw std::runtime_error("failed to create pid file");
    }

    if (flock(fd, LOCK_EX | LOCK_NB) == 0) {
      get_single_application_lock_pid_file_descriptor() = fd;

      std::stringstream ss;
      ss << getpid() << std::endl;
      auto string = ss.str();
      write(fd, string.c_str(), string.size());

      return true;
    }
{'fraction_non_alphanumeric': 0.12869105332745703, 'fraction_numerical': 0.0123402379903041, 'mean_word_length': 4.044444444444444, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 12, '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': '7365587', 'n_tokens_mistral': 776, 'n_tokens_neox': 738, 'n_words': 171}