Exemplo n.º 1
0
	addrinfo_ptr getaddrinfo(const char * host, const char * service, std::error_code & err)
	{
		addrinfo_type hints;

		std::memset(&hints, 0, sizeof(hints));
		hints.ai_family = AF_UNSPEC;
		hints.ai_protocol = IPPROTO_TCP;
		hints.ai_socktype = SOCK_STREAM;

		int res;
		addrinfo_type * ptr;
		do res = ::getaddrinfo(host, service, &hints, &ptr); while (res == EAI_AGAIN);
		if (res == 0)
		{
			err.clear();
			return addrinfo_ptr(ptr);
		}

		if (res == EAI_SYSTEM)
		{
			err.assign(errno, std::generic_category());
			return addrinfo_ptr(nullptr);
		}
		else
		{
			err.assign(res, gai_error_category());
			return addrinfo_ptr(nullptr);
		}
	}
Exemplo n.º 2
0
space_info space(const std::string& path, std::error_code& ec)
{
  using WINDOWS::ToW;

  ec.clear();
  space_info sp;
  auto pathW = ToW(path);

  ULARGE_INTEGER capacity;
  ULARGE_INTEGER available;
  ULARGE_INTEGER free;
  auto result = GetDiskFreeSpaceExW(pathW.c_str(), &available, &capacity, &free);

  if (result == FALSE)
  {
    ec.assign(GetLastError(), std::system_category());
    sp.available = static_cast<uintmax_t>(-1);
    sp.capacity = static_cast<uintmax_t>(-1);
    sp.free = static_cast<uintmax_t>(-1);
    return sp;
  }

  sp.available = static_cast<uintmax_t>(available.QuadPart);
  sp.capacity = static_cast<uintmax_t>(capacity.QuadPart);
  sp.free = static_cast<uintmax_t>(free.QuadPart);

  return sp;
}
Exemplo n.º 3
0
  bool copy_file(const path& from,
                 const path& to,
                 copy_options options,
                 std::error_code& ec) noexcept
  {
    auto t = status(to, ec);
    if (!is_regular_file(from) ||
        (t.type() != file_type::not_found &&
         (t.type() != file_type::regular || equivalent(from, to) ||
          (options &
           (copy_options::skip_existing | copy_options::overwrite_existing |
            copy_options::update_existing)) == copy_options::none))) {
      ec.assign(EEXIST, std::system_category());
    } else {
      if (t.type() == file_type::not_found ||
          (options & copy_options::overwrite_existing) != copy_options::none ||
          ((options & copy_options::update_existing) != copy_options::none &&
           last_write_time(from) > last_write_time(to))) {
        std::ifstream src(from, std::ios::binary);
        std::ofstream dst(to, std::ios::binary | std::ios::trunc);

        dst << src.rdbuf();
        if (errno != 0) {
          ec = {errno, std::system_category()};
        } else {
          ec.clear();
          return true;
        }
      }
    }
    return false;
  }
Exemplo n.º 4
0
	std::string FormatError(std::error_code err)
	{
#if BOOST_OS_WINDOWS
		if (err.category() == std::system_category())
			err.assign(err.value(), ext::system_utf8_category());
#endif

		return FormatErrorImpl(err);
	}
Exemplo n.º 5
0
std::vector<char> File::ReadAllBytes(std::error_code& ec)
{
    std::unique_ptr<HANDLE, CloseHandleDeleter> handle(CreateFile(
        m_path,
        GENERIC_READ,
        FILE_SHARE_READ,
        NULL,
        OPEN_EXISTING,
        FILE_ATTRIBUTE_NORMAL,
        NULL));

    if (handle.get() == INVALID_HANDLE_VALUE)
    {
        ec.assign(GetLastError(), std::system_category());
        return ByteBuffer();
    }

    ByteBuffer result;
    DWORD read = 0;

    do
    {
        char tmp[1024];
        read = 0;

        if (!ReadFile(
            handle.get(),
            tmp,
            ARRAYSIZE(tmp),
            &read,
            NULL))
        {
            ec.assign(GetLastError(), std::system_category());
            return ByteBuffer();
        }

        result.insert(
            result.end(),
            tmp,
            tmp + read);
    } while (read > 0);

    return result;
}
Exemplo n.º 6
0
    static inline
    void
    unpack(const msgpack::object& source, std::error_code& target) {
        int category_id;
        int ec;

        type_traits<sequence_type>::unpack(source, category_id, ec);

        target.assign(ec, error::registrar::map(category_id));
    }
Exemplo n.º 7
0
  void DIter::read_dir(const path& p,
                       directory_options options,
                       std::error_code& ec)
  {
    _entries = std::make_shared<std::vector<DEntr>>();

    bool skip_denied = (options & directory_options::skip_permission_denied) !=
                       directory_options::none;

    errno = 0;
    auto* dir = opendir(p.c_str());
    if (dir == nullptr) {
      ec.assign(errno, std::system_category());
      return;
    }

    std::string name;
    dirent* de;
    do {
      errno = 0;
      de = readdir(dir);
      if (errno == EACCES && skip_denied) {
        continue;
      }
      if (!de) continue;
      name = de->d_name;
      if (name == "." || name == "..") continue;
      _entries->emplace_back(p / de->d_name);
    } while (de != nullptr);

    closedir(dir);

    if (errno != 0) {
      ec.assign(errno, std::system_category());
    }

    if (_entries->size() == 0) {
      _ptr = nullptr;
    } else {
      _ptr = _entries->data();
    }
  }
 /// Manually close the file descriptor
 void close(std::error_code& ec) noexcept
 {
     ec.clear();
     errno = 0;
     if (::close(fd_) == 0) {
         fd_ = -1;
         delete_ = false;
     } else {
         ec.assign(errno, std::system_category());
     }
 }
//#############################################################################
void basic_thread_pool_impl::create_pool(
    uint32_t size, std::error_code& ec)
  {
  if (size == 0)
    {
    return;
    }

  std::lock_guard<std::mutex> pool_lock(pool_mutex_);
  if (pool_.size() > 0 || pool_size_ != 0)
    {
    ec.assign(EALREADY, std::system_category());
    return;
    }

  for (uint32_t i = 0; i < size; i++)
    {
    thread_handler_.create_thread(
      std::bind(
        &basic_thread_pool_impl::on_thread_created, shared_from_this(),
          std::placeholders::_1, std::placeholders::_2));
    }
  }