pianod2
multisource multiuser scriptable networked music player
Macros | Functions | Variables
fileio.cpp File Reference

Read/write documents and do filesystem interaction. More...

#include <config.h>
#include <cstdio>
#include <cstring>
#include <cstdint>
#include <cstdlib>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <pwd.h>
#include <grp.h>
#include <iostream>
#include <fstream>
#include <stdexcept>
#include <parsnip/parsnip.h>
#include "logging.h"
Include dependency graph for fileio.cpp:

Macros

#define SYSCONFDIR   "/etc"
 

Functions

static bool running_as_root (void)
 
static char * select_nobody_user (const char *nobody_name, const char *group_names)
 
static void drop_root_privs (void)
 
static void precreateDirectory (const std::string &directory, bool own)
 Ensure the pianod configuration directory is owned by the user we become when dropping root privileges. More...
 
static std::string getConfigDirectory (const char *location, const char *home, const char *package)
 Determine the configuration directory in accordance with the Free Desktop Base Directory Specification. More...
 
const std::string & setupConfigDirectory (const char *location, const char *package, const char *nobody_name, const char *nobody_groups_list)
 Choose and configure a directory for configuration files. More...
 
bool carefullyWriteFile (std::string filename, Parsnip::Data &document)
 Write a document into a file. More...
 
const Parsnip::Data retrieveJsonFile (std::string filename, int minimum_version)
 Restore from a file. More...
 

Variables

static const char * FileCreator = "author"
 
static const char * FileCreatorVersion = "version"
 
static std::string config_directory
 
static bool have_nobody = false
 
static struct passwd nobody
 
static gid_t * nobody_groups
 
static int nobody_groups_count
 

Detailed Description

Read/write documents and do filesystem interaction.

Macro Definition Documentation

◆ SYSCONFDIR

#define SYSCONFDIR   "/etc"

Function Documentation

◆ carefullyWriteFile()

bool carefullyWriteFile ( std::string  filename,
Parsnip::Data document 
)

Write a document into a file.

Write into a temporary file, and when complete, then rename files so as to keep as backup while moving the new file into place. If compression is supported, writes compressed file; otherwise, writes uncompressed JSON. This is set at compile time, with no run-time choice.

Parameters
filenameThe filename, without path.
documentThe document to write. Top level must be a document.
Warning
document parameter is altered to contain authoring package identification.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ drop_root_privs()

static void drop_root_privs ( void  )
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ getConfigDirectory()

static std::string getConfigDirectory ( const char *  location,
const char *  home,
const char *  package 
)
static

Determine the configuration directory in accordance with the Free Desktop Base Directory Specification.

See also
http://standards.freedesktop.org/basedir-spec/basedir-spec-0.6.html
Parameters
locationPut config files in this directory if specified.
homeUse this directory instead of $HOME if specified.
packageThis package's name.
Returns
Directory with package name and a trailing '/' appended.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ precreateDirectory()

static void precreateDirectory ( const std::string &  directory,
bool  own 
)
static

Ensure the pianod configuration directory is owned by the user we become when dropping root privileges.

Here is the caller graph for this function:

◆ retrieveJsonFile()

const Parsnip::Data retrieveJsonFile ( std::string  filename,
int  minimum_version 
)

Restore from a file.

Tries .gz (if supported) first, then falls back to uncompressed version.

Parameters
filenameThe filename, without path.
minimum_versionThe lowest file format version number supported. If the input file has a version number lower than this, a DataFormatException is thrown.
Returns
A parsed JSON document.
Exceptions
bad_alloc,io_base::failure,ora Parsnip serialization exception.
Here is the call graph for this function:
Here is the caller graph for this function:

◆ running_as_root()

static bool running_as_root ( void  )
static
Here is the caller graph for this function:

◆ select_nobody_user()

static char* select_nobody_user ( const char *  nobody_name,
const char *  group_names 
)
static
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setupConfigDirectory()

const std::string& setupConfigDirectory ( const char *  location,
const char *  package,
const char *  nobody_name,
const char *  nobody_groups_list 
)

Choose and configure a directory for configuration files.

Parameters
locationPathname to use, or nullptr to use default.
packageName of this package.
nobody_nameName of user to run as, if running as root.
nobody_groups_listList of groups to adopt before giving up root privs.
Returns
Path of the chosen configuration directory, which will exist and be read/write accessible.
Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ config_directory

std::string config_directory
static

◆ FileCreator

const char* FileCreator = "author"
static

◆ FileCreatorVersion

const char* FileCreatorVersion = "version"
static

◆ have_nobody

bool have_nobody = false
static

◆ nobody

struct passwd nobody
static

◆ nobody_groups

gid_t* nobody_groups
static

◆ nobody_groups_count

int nobody_groups_count
static