1
1
Fork 0
mirror of https://github.com/NixOS/nix.git synced 2025-11-12 05:26:02 +01:00

Cleanup config headers

There are two big changes:

1. Public and private config is now separated. Configuration variables
   that are only used internally do not go in a header which is
   installed.

   (Additionally, libutil has a unix-specific private config header,
   which should only be used in unix-specific code. This keeps things a
   bit more organized, in a purely private implementation-internal way.)

2. Secondly, there is no more `-include`. There are very few config
   items that need to be publically exposed, so now it is feasible to
   just make the headers that need them just including the (public)
   configuration header.

And there are also a few more small cleanups on top of those:

- The configuration files have better names.

- The few CPP variables that remain exposed in the public headers are
  now also renamed to always start with `NIX_`. This ensures they should
  not conflict with variables defined elsewhere.

- We now always use `#if` and not `#ifdef`/`#ifndef` for our
  configuration variables, which helps avoid bugs by requiring that
  variables must be defined in all cases.
This commit is contained in:
John Ericson 2025-03-28 13:24:50 -04:00
parent 5a8dedc45c
commit c204e307ac
59 changed files with 333 additions and 385 deletions

View file

@ -7,6 +7,9 @@
#include <unistd.h>
#include <poll.h>
#include "util-config-private.hh"
#include "util-unix-config-private.hh"
namespace nix {
namespace {

View file

@ -1,10 +1,72 @@
#include <cerrno>
#include <climits>
#include <cstdio>
#include <cstdlib>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/time.h>
#include <unistd.h>
#include "nix/file-system.hh"
#include "util-unix-config-private.hh"
namespace nix {
namespace fs {
using namespace std::filesystem;
}
Descriptor openDirectory(const std::filesystem::path & path)
{
return open(path.c_str(), O_RDONLY | O_DIRECTORY);
}
void setWriteTime(const fs::path & path, time_t accessedTime, time_t modificationTime, std::optional<bool> optIsSymlink)
{
// Would be nice to use std::filesystem unconditionally, but
// doesn't support access time just modification time.
//
// System clock vs File clock issues also make that annoying.
#if HAVE_UTIMENSAT && HAVE_DECL_AT_SYMLINK_NOFOLLOW
struct timespec times[2] = {
{
.tv_sec = accessedTime,
.tv_nsec = 0,
},
{
.tv_sec = modificationTime,
.tv_nsec = 0,
},
};
if (utimensat(AT_FDCWD, path.c_str(), times, AT_SYMLINK_NOFOLLOW) == -1)
throw SysError("changing modification time of %s (using `utimensat`)", path);
#else
struct timeval times[2] = {
{
.tv_sec = accessedTime,
.tv_usec = 0,
},
{
.tv_sec = modificationTime,
.tv_usec = 0,
},
};
# if HAVE_LUTIMES
if (lutimes(path.c_str(), times) == -1)
throw SysError("changing modification time of %s", path);
# else
bool isSymlink = optIsSymlink ? *optIsSymlink : fs::is_symlink(path);
if (!isSymlink) {
if (utimes(path.c_str(), times) == -1)
throw SysError("changing modification time of %s (not a symlink)", path);
} else {
throw Error("Cannot change modification time of symlink %s", path);
}
# endif
#endif
}
}

View file

@ -1,3 +1,53 @@
include_dirs += include_directories('.')
configdata_unix = configuration_data()
configdata_unix.set(
'HAVE_DECL_AT_SYMLINK_NOFOLLOW',
cxx.has_header_symbol('fcntl.h', 'AT_SYMLINK_NOFOLLOW').to_int(),
description : 'Optionally used for changing the files and symlinks.'
)
# Check for each of these functions, and create a define like `#define
# HAVE_CLOSE_RANGE 1`.
check_funcs_unix = [
[
'close_range',
'For closing many file descriptors after forking.',
],
[
'lutimes',
'Optionally used for changing the mtime of symlinks.',
],
[
'pipe2',
'Optionally used for creating pipes on Unix.',
],
[
'strsignal',
'Optionally used to get more information about processes failing due to a signal on Unix.',
],
[
'sysconf',
'Optionally used to try to close more file descriptors (e.g. before forking) on Unix.',
],
[
'utimensat',
'Optionally used for changing the mtime of files and symlinks.',
],
]
foreach funcspec : check_funcs_unix
define_name = 'HAVE_' + funcspec[0].underscorify().to_upper()
define_value = cxx.has_function(funcspec[0]).to_int()
configdata_unix.set(define_name, define_value, description: funcspec[1])
endforeach
config_unix_priv_h = configure_file(
configuration : configdata_unix,
output : 'util-unix-config-private.hh',
)
sources += config_unix_priv_h
sources += files(
'environment-variables.cc',
'file-descriptor.cc',

View file

@ -28,6 +28,9 @@
# include <sys/mman.h>
#endif
#include "util-config-private.hh"
#include "util-unix-config-private.hh"
namespace nix {