<commit_before>/*
 * Copyright (C) 2008-2011, <NAME>.
 *
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the
 * implied warranties of fitness for a particular purpose.
 *
 * See the LICENSE file for more information.
 */

#include <kernel/config.h>
#if HAVE_SYS_INOTIFY_H
# include <sys/inotify.h>
#endif

#include <boost/filesystem.hpp>

#include <libport/cassert>
#include <libport/compiler.hh>
#include <libport/dirent.h>
#include <libport/exception.hh>
#include <libport/format.hh>
#include <libport/lockable.hh>
#include <libport/path.hh>
#include <libport/sys/types.h>
#include <libport/thread.hh>

#include <kernel/userver.hh>

#include <urbi/object/date.hh>
#include <urbi/object/directory.hh>
#include <urbi/object/global.hh>
#include <urbi/object/path.hh>
#include <object/symbols.hh>

#include <urbi/runner/raise.hh>

#include <stdio.h>

namespace boostfs = boost::filesystem;

using libport::BlockLock;

namespace urbi
{
  namespace object
  {
    /*----------.
    | Helpers.  |
    `----------*/

    ATTRIBUTE_NORETURN
    static void
    raise_boost_fs_error(boostfs::filesystem_error& e)
    {
      FRAISE("%s", libport::format_boost_fs_error(e.what()));
    }

    static void
    check_directory(const rPath& path)
    {
      if (!path->is_dir())
        FRAISE("not a directory: \"%s\"", path->as_string());
    }

    static void
    check_exists(const rPath& path)
    {
      if (!path->exists())
        FRAISE("directory does not exist: \"%s\"", path->as_string());
    }

    ATTRIBUTE_NORETURN
    static void
    raise_file_exists(const rPath& path)
    {
      FRAISE("file exists: \"%s\"", path->as_string());
    }

    ATTRIBUTE_NORETURN
    static void
    raise_directory_exists(const rPath& path)
    {
      FRAISE("directory exists: \"%s\"", path->as_string());
    }

    static void
    check_nexists(const rPath& path)
    {
      if (path->exists())
      {
        if (path->is_dir())
          raise_directory_exists(path);
        else
          raise_file_exists(path);
      }
    }

    static void
    check_empty(const Directory& dir)
    {
      if (!dir.empty())
        FRAISE("directory not empty: \"%s\"", dir.as_string());
    }

    /*--------------.
    | C++ methods.  |
    `--------------*/

    Directory::value_type
    Directory::value_get()
    {
      return path_;
    }


    /*---------------------.
    | urbiscript methods.  |
    `---------------------*/

#if HAVE_SYS_INOTIFY_H
    // Event polling thread
    typedef std::pair<rObject, rObject> _watch_map_elem_t;
    typedef boost::unordered_map<unsigned, _watch_map_elem_t> _watch_map_t;
    static _watch_map_t _watch_map;
    static libport::Lockable _watch_map_lock;
    static int _watch_fd;

    ATTRIBUTE_NORETURN
    static
    void poll()
    {
      while (true)
      {
        static const size_t name_size_max = 1024;
        static const size_t evt_size = sizeof(inotify_event);
        static const size_t buf_size = evt_size + name_size_max;

        char buffer[buf_size];
        const int len = read(_watch_fd, &buffer, buf_size);
        if (len < 0)
        {
          if (errno == EINTR)
            continue;
          else
            errnoabort("read failed");
        }
        int i = 0;
        {
          libport::Synchronizer::SynchroPoint lock(kernel::server().big_kernel_lock_get());
          while (i < len)
          {
            inotify_event& evt = reinterpret_cast<inotify_event&>(buffer[i]);
            i += evt_size + evt.len;
<commit_msg>Formatting changes.<commit_after>/*
 * Copyright (C) 2008-2011, <NAME>.
 *
 * This software is provided "as is" without warranty of any kind,
 * either expressed or implied, including but not limited to the
 * implied warranties of fitness for a particular purpose.
 *
 * See the LICENSE file for more information.
 */

#include <kernel/config.h>
#if HAVE_SYS_INOTIFY_H
# include <sys/inotify.h>
#endif

#include <boost/filesystem.hpp>

#include <libport/cassert>
#include <libport/compiler.hh>
#include <libport/dirent.h>
#include <libport/exception.hh>
#include <libport/format.hh>
#include <libport/lockable.hh>
#include <libport/path.hh>
#include <libport/sys/types.h>
#include <libport/thread.hh>

#include <kernel/userver.hh>

#include <urbi/object/date.hh>
#include <urbi/object/directory.hh>
#include <urbi/object/global.hh>
#include <urbi/object/path.hh>
#include <object/symbols.hh>

#include <urbi/runner/raise.hh>

#include <stdio.h>

namespace boostfs = boost::filesystem;

using libport::BlockLock;

namespace urbi
{
  namespace object
  {
    /*----------.
    | Helpers.  |
    `----------*/

    ATTRIBUTE_NORETURN
    static void
    raise_boost_fs_error(boostfs::filesystem_error& e)
    {
      FRAISE("%s", libport::format_boost_fs_error(e.what()));
    }

    static void
    check_directory(const rPath& path)
    {
      if (!path->is_dir())
        FRAISE("not a directory: \"%s\"", path->as_string());
    }

    static void
    check_exists(const rPath& path)
    {
      if (!path->exists())
        FRAISE("directory does not exist: \"%s\"", path->as_string());
    }

    ATTRIBUTE_NORETURN
    static void
    raise_file_exists(const rPath& path)
    {
      FRAISE("file exists: \"%s\"", path->as_string());
    }

    ATTRIBUTE_NORETURN
    static void
    raise_directory_exists(const rPath& path)
    {
      FRAISE("directory exists: \"%s\"", path->as_string());
    }

    static void
    check_nexists(const rPath& path)
    {
      if (path->exists())
      {
        if (path->is_dir())
          raise_directory_exists(path);
        else
          raise_file_exists(path);
      }
    }

    static void
    check_empty(const Directory& dir)
    {
      if (!dir.empty())
        FRAISE("directory not empty: \"%s\"", dir.as_string());
    }

    /*--------------.
    | C++ methods.  |
    `--------------*/

    Directory::value_type
    Directory::value_get()
    {
      return path_;
    }


    /*---------------------.
    | urbiscript methods.  |
    `---------------------*/

#if HAVE_SYS_INOTIFY_H
    // Event polling thread
    typedef std::pair<rObject, rObject> _watch_map_elem_t;
    typedef boost::unordered_map<unsigned, _watch_map_elem_t> _watch_map_t;
    static _watch_map_t _watch_map;
    static libport::Lockable _watch_map_lock;
    static int _watch_fd;

    ATTRIBUTE_NORETURN
    static
    void poll()
    {
      while (true)
      {
        static const size_t name_size_max = 1024;
        static const size_t evt_size = sizeof(inotify_event);
        static const size_t buf_size = evt_size + name_size_max;

        char buffer[buf_size];
        const int len = read(_watch_fd, &buffer, buf_size);
        if (len < 0)
        {
          if (errno == EINTR)
            continue;
          else
            errnoabort("read failed");
        }
        int i = 0;
        {
          libport::Synchronizer::SynchroPoint
            lock(kernel::server().big_kernel_lock_get());
          while (i < len)
          {
            inotify_event& evt = reinterpret_cast<inotify_event&>(buffer[i]);
            i += evt_size + evt.len;

{'fraction_non_alphanumeric': 0.1491106170094497, 'fraction_numerical': 0.0038910505836575876, 'mean_word_length': 2.380460310004697, 'pattern_counts': {'":': 0, '<': 55, '<?xml version=': 0, '>': 67, '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': '6165820', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2378, 'n_words': 509}