2018-05-30 01:38:20 -07:00
|
|
|
// Matrix Construct
|
|
|
|
//
|
|
|
|
// Copyright (C) Matrix Construct Developers, Authors & Contributors
|
|
|
|
// Copyright (C) 2016-2018 Jason Volk <jason@zemos.net>
|
|
|
|
//
|
|
|
|
// Permission to use, copy, modify, and/or distribute this software for any
|
|
|
|
// purpose with or without fee is hereby granted, provided that the above
|
|
|
|
// copyright notice and this permission notice is present in all copies. The
|
|
|
|
// full license for this software is available in the LICENSE file.
|
|
|
|
|
|
|
|
#pragma once
|
|
|
|
#define HAVE_IRCD_FS_FD_H
|
|
|
|
|
|
|
|
namespace ircd::fs
|
|
|
|
{
|
|
|
|
struct fd;
|
2018-05-30 02:12:19 -07:00
|
|
|
|
|
|
|
size_t size(const fd &);
|
2018-08-28 19:31:58 -07:00
|
|
|
size_t block_size(const fd &);
|
2019-01-25 10:35:39 -08:00
|
|
|
ulong fstype(const fd &);
|
2019-01-01 18:35:09 -08:00
|
|
|
ulong device(const fd &);
|
2019-05-08 19:08:28 -07:00
|
|
|
uint64_t write_life(const fd &) noexcept;
|
2019-05-03 19:53:05 -07:00
|
|
|
void write_life(const fd &, const uint64_t &);
|
2019-07-12 13:34:18 -07:00
|
|
|
size_t advise(const fd &, const int &, const size_t & = 0, const opts & = opts_default);
|
|
|
|
size_t evict(const fd &, const size_t &size = 0, const opts & = opts_default);
|
2018-05-30 01:38:20 -07:00
|
|
|
}
|
|
|
|
|
2019-01-05 17:18:43 -08:00
|
|
|
/// File Desc++ptor. This is simply a native fd (i.e. integer) with c++ object
|
|
|
|
/// semantics.
|
2018-05-30 01:38:20 -07:00
|
|
|
struct ircd::fs::fd
|
|
|
|
{
|
|
|
|
struct opts;
|
|
|
|
|
2020-05-20 17:56:02 -07:00
|
|
|
int fdno {-1};
|
2018-05-30 01:38:20 -07:00
|
|
|
|
|
|
|
public:
|
2018-10-31 17:54:57 -07:00
|
|
|
operator const int &() const;
|
|
|
|
operator bool() const;
|
|
|
|
bool operator!() const;
|
2019-04-22 09:09:20 -07:00
|
|
|
opts options() const;
|
2018-05-30 01:38:20 -07:00
|
|
|
|
2019-04-22 08:55:42 -07:00
|
|
|
int release() noexcept;
|
|
|
|
|
2019-03-31 23:57:35 -07:00
|
|
|
explicit fd(const int &);
|
2020-05-20 18:20:47 -07:00
|
|
|
fd(const int &dirfd, const string_view &path, const opts &);
|
2018-05-30 01:38:20 -07:00
|
|
|
fd(const string_view &path, const opts &);
|
|
|
|
fd(const string_view &path);
|
|
|
|
fd() = default;
|
|
|
|
fd(fd &&) noexcept;
|
|
|
|
fd(const fd &) = delete;
|
|
|
|
fd &operator=(fd &&) noexcept;
|
|
|
|
fd &operator=(const fd &) = delete;
|
2019-04-22 09:16:11 -07:00
|
|
|
~fd() noexcept;
|
2018-05-30 01:38:20 -07:00
|
|
|
};
|
|
|
|
|
2020-05-20 17:56:02 -07:00
|
|
|
/// Descriptor options (open options)
|
2018-05-30 01:38:20 -07:00
|
|
|
struct ircd::fs::fd::opts
|
2020-05-24 14:31:29 -07:00
|
|
|
:fs::opts
|
2018-05-30 01:38:20 -07:00
|
|
|
{
|
2018-12-08 15:17:13 -08:00
|
|
|
static conf::item<bool> direct_io_enable;
|
|
|
|
|
2018-11-01 23:35:52 -07:00
|
|
|
/// std openmode passed from ctor.
|
|
|
|
std::ios::openmode mode {std::ios::openmode(0)};
|
|
|
|
|
2018-05-30 04:30:52 -07:00
|
|
|
/// open(2) flags. Usually generated from ios::open_mode ctor.
|
2018-05-30 01:38:20 -07:00
|
|
|
ulong flags {0};
|
2018-05-30 04:30:52 -07:00
|
|
|
|
|
|
|
/// open(2) mode_t mode used for file creation.
|
2018-05-30 01:38:20 -07:00
|
|
|
ulong mask {0};
|
2018-05-30 04:30:52 -07:00
|
|
|
|
|
|
|
/// Seek to end after open. This exists to convey the flag for open_mode.
|
2018-05-30 01:38:20 -07:00
|
|
|
bool ate {false};
|
2018-05-30 04:30:52 -07:00
|
|
|
|
|
|
|
/// (O_DIRECT) Direct IO bypassing the operating system caches.
|
2018-05-30 04:12:27 -07:00
|
|
|
bool direct {false};
|
2018-05-30 04:30:52 -07:00
|
|
|
|
|
|
|
/// (O_CLOEXEC) Close this descriptor on an exec().
|
2018-05-30 04:12:27 -07:00
|
|
|
bool cloexec {true};
|
2018-05-30 04:30:52 -07:00
|
|
|
|
2020-09-28 22:14:13 -07:00
|
|
|
/// Allows file to be created if it doesn't exist. Set this to false to
|
|
|
|
/// prevent file from being created when opened with a write-mode.
|
|
|
|
bool create {true};
|
2018-05-30 01:38:20 -07:00
|
|
|
|
2020-09-28 22:30:11 -07:00
|
|
|
/// Allows file to opened if and only if it doesn't exist and will be
|
|
|
|
/// created by this open().
|
|
|
|
bool exclusive {false};
|
|
|
|
|
2020-05-09 18:34:50 -07:00
|
|
|
/// Advise for random access (ignored when direct=true)
|
|
|
|
bool random {false};
|
|
|
|
|
|
|
|
/// Advise for sequential access (ignored when direct=true)
|
|
|
|
bool sequential {false};
|
|
|
|
|
|
|
|
/// Advise for dontneed access (ignored when direct=true)
|
|
|
|
bool dontneed {false};
|
|
|
|
|
2018-05-30 04:30:52 -07:00
|
|
|
/// Construct options from an std::ios::open_mode bitmask.
|
2018-09-13 03:01:06 -07:00
|
|
|
opts(const std::ios::openmode &);
|
2018-05-30 01:38:20 -07:00
|
|
|
opts() = default;
|
|
|
|
};
|
2018-10-31 17:54:57 -07:00
|
|
|
|
|
|
|
inline bool
|
|
|
|
ircd::fs::fd::operator!()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return !bool(*this);
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::fs::fd::operator
|
|
|
|
bool()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return int(*this) >= 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
inline ircd::fs::fd::operator
|
|
|
|
const int &()
|
|
|
|
const
|
|
|
|
{
|
|
|
|
return fdno;
|
|
|
|
}
|