2018-05-30 10:38:20 +02: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 11:12:19 +02:00
|
|
|
|
|
|
|
size_t size(const fd &);
|
2018-08-29 04:31:58 +02:00
|
|
|
size_t block_size(const fd &);
|
2018-08-21 07:44:39 +02:00
|
|
|
string_view uuid(const fd &, const mutable_buffer &);
|
2018-05-30 10:38:20 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Object for maintaining state to an open file or directory. Instances can
|
|
|
|
/// be used with various functions around ircd::fs.
|
|
|
|
struct ircd::fs::fd
|
|
|
|
{
|
|
|
|
struct opts;
|
|
|
|
|
|
|
|
int fdno{-1};
|
|
|
|
|
|
|
|
public:
|
2018-11-01 01:54:57 +01:00
|
|
|
operator const int &() const;
|
|
|
|
operator bool() const;
|
|
|
|
bool operator!() const;
|
2018-05-30 10:38:20 +02: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;
|
|
|
|
~fd() noexcept(false);
|
|
|
|
};
|
|
|
|
|
|
|
|
struct ircd::fs::fd::opts
|
|
|
|
{
|
2018-11-02 07:35:52 +01:00
|
|
|
/// std openmode passed from ctor.
|
|
|
|
std::ios::openmode mode {std::ios::openmode(0)};
|
|
|
|
|
2018-05-30 13:30:52 +02:00
|
|
|
/// open(2) flags. Usually generated from ios::open_mode ctor.
|
2018-05-30 10:38:20 +02:00
|
|
|
ulong flags {0};
|
2018-05-30 13:30:52 +02:00
|
|
|
|
|
|
|
/// open(2) mode_t mode used for file creation.
|
2018-05-30 10:38:20 +02:00
|
|
|
ulong mask {0};
|
2018-05-30 13:30:52 +02:00
|
|
|
|
|
|
|
/// Seek to end after open. This exists to convey the flag for open_mode.
|
2018-05-30 10:38:20 +02:00
|
|
|
bool ate {false};
|
2018-05-30 13:30:52 +02:00
|
|
|
|
|
|
|
/// (O_DIRECT) Direct IO bypassing the operating system caches.
|
2018-05-30 13:12:27 +02:00
|
|
|
bool direct {false};
|
2018-05-30 13:30:52 +02:00
|
|
|
|
|
|
|
/// (O_CLOEXEC) Close this descriptor on an exec().
|
2018-05-30 13:12:27 +02:00
|
|
|
bool cloexec {true};
|
2018-05-30 13:30:52 +02:00
|
|
|
|
|
|
|
/// Prevents file from being created if it doesn't exist. This clears
|
|
|
|
/// any implied O_CREAT from the open_mode ctor and in flags too.
|
2018-05-30 13:21:27 +02:00
|
|
|
bool nocreate {false};
|
2018-05-30 10:38:20 +02:00
|
|
|
|
2018-05-30 13:30:52 +02:00
|
|
|
/// Construct options from an std::ios::open_mode bitmask.
|
2018-09-13 12:01:06 +02:00
|
|
|
opts(const std::ios::openmode &);
|
2018-05-30 10:38:20 +02:00
|
|
|
opts() = default;
|
|
|
|
};
|
2018-11-01 01:54:57 +01: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;
|
|
|
|
}
|