Functions | Variables

datafiles.c File Reference

#include "services.h"
#include "datafiles.h"
#include <fcntl.h>
Include dependency graph for datafiles.c:

Go to the source code of this file.

Functions

int get_file_version (dbFILE *f)
int write_file_version (dbFILE *f, uint32 version)
static dbFILEopen_db_read (const char *service, const char *filename)
static dbFILEopen_db_write (const char *service, const char *filename, uint32 version)
dbFILEopen_db (const char *service, const char *filename, const char *mode, uint32 version)
void restore_db (dbFILE *f)
void close_db (dbFILE *f)
int read_int16 (uint16 *ret, dbFILE *f)
int write_int16 (uint16 val, dbFILE *f)
int read_int32 (uint32 *ret, dbFILE *f)
int write_int32 (uint32 val, dbFILE *f)
int read_ptr (void **ret, dbFILE *f)
int write_ptr (const void *ptr, dbFILE *f)
int read_string (char **ret, dbFILE *f)
int write_string (const char *s, dbFILE *f)
static void rename_database (char *name, char *ext)
static void remove_backups (void)
void backup_databases (void)
void ModuleDatabaseBackup (char *dbname)
void ModuleRemoveBackups (char *dbname)

Variables

static int curday = 0
static time_t lastwarn = 0

Function Documentation

void backup_databases ( void   ) 
void close_db ( dbFILE f  ) 

Close a database file. If the file was opened for write, remove the backup we (may have) created earlier.

Parameters:
dbFile struct
Returns:
void

Definition at line 373 of file datafiles.c.

References dbFILE_::backupfp, dbFILE_::backupname, dbFILE_::filename, dbFILE_::fp, and dbFILE_::mode.

int get_file_version ( dbFILE f  ) 

Return the version number on the file. Return 0 if there is no version number or the number doesn't make sense (i.e. less than 1 or greater than FILE_VERSION).

Parameters:
f dbFile Struct Member
Returns:
int 0 if failure, 1 > is the version number

Definition at line 30 of file datafiles.c.

References alog(), dbFILE_::filename, dbFILE_::fp, log_perror(), and version.

Referenced by load_bs_dbase(), load_cs_dbase(), load_exceptions(), load_hs_dbase(), load_news(), load_ns_dbase(), load_ns_req_db(), load_old_akill(), load_old_ns_dbase(), and load_os_dbase().

void ModuleDatabaseBackup ( char *  dbname  ) 
void ModuleRemoveBackups ( char *  dbname  ) 

Definition at line 755 of file datafiles.c.

References KeepBackups, and snprintf().

Referenced by ModuleDatabaseBackup().

dbFILE* open_db ( const char *  service,
const char *  filename,
const char *  mode,
uint32  version 
)

Open a database file for reading (*mode == 'r') or writing (*mode == 'w'). Return the stream pointer, or NULL on error. When opening for write, it is an error for rename() to return an error (when backing up the original file) other than ENOENT, if NO_BACKUP_OKAY is not defined; it is an error if the version number cannot be written to the file; and it is a fatal error if opening the file for write fails and the backup was successfully made but cannot be restored.

Parameters:
service If error whom to return the error as
filename File to open as the database
mode Mode for writting or reading
version Database Version
Returns:
dbFile struct

Definition at line 295 of file datafiles.c.

References open_db_read(), and open_db_write().

Referenced by load_bs_dbase(), load_cs_dbase(), load_exceptions(), load_hs_dbase(), load_news(), load_ns_dbase(), load_ns_req_db(), load_old_akill(), load_old_ns_dbase(), load_os_dbase(), save_bs_dbase(), save_cs_dbase(), save_exceptions(), save_hs_dbase(), save_news(), save_ns_dbase(), save_ns_req_dbase(), and save_os_dbase().

static dbFILE* open_db_read ( const char *  service,
const char *  filename 
) [static]

Open the database for reading

Parameters:
service If error whom to return the error as
filename File to open as the database
Returns:
dbFile struct

Definition at line 85 of file datafiles.c.

References anope_cmd_global(), dbFILE_::backupfp, dbFILE_::filename, dbFILE_::fp, lastwarn, log_perror(), dbFILE_::mode, scalloc(), strerror(), strscpy(), and WarningTimeout.

Referenced by main(), and open_db().

static dbFILE* open_db_write ( const char *  service,
const char *  filename,
uint32  version 
) [static]

Open the database for writting

Parameters:
service If error whom to return the error as
filename File to open as the database
version Database Version
Returns:
dbFile struct

Definition at line 136 of file datafiles.c.

References alog(), anope_cmd_global(), dbFILE_::backupfp, dbFILE_::backupname, debug, dbFILE_::filename, dbFILE_::fp, log_perror(), dbFILE_::mode, NoBackupOkay, scalloc(), snprintf(), strscpy(), and write_file_version().

Referenced by main(), and open_db().

int read_int16 ( uint16 ret,
dbFILE f 
)

Read and write 2- and 4-byte quantities, pointers, and strings. All multibyte values are stored in big-endian order (most significant byte first). A pointer is stored as a byte, either 0 if NULL or 1 if not, and read pointers are returned as either (void *)0 or (void *)1. A string is stored with a 2-byte unsigned length (including the trailing ) first; a length of 0 indicates that the string pointer is NULL. Written strings are truncated silently at 65534 bytes, and are always null-terminated.

Parameters:
ret 16bit integer to write
dbFile struct
Returns:
-1 on error, 0 otherwise.

Definition at line 405 of file datafiles.c.

References dbFILE_::fp.

Referenced by load_bs_dbase(), load_cs_dbase(), load_exceptions(), load_news(), load_ns_dbase(), load_old_akill(), load_old_ns_dbase(), load_os_dbase(), main(), and read_string().

int read_int32 ( uint32 ret,
dbFILE f 
)

Read a unsigned 32bit integer

Parameters:
ret unsigned 32bit integer to read
dbFile struct
Returns:
-1 on error, 0 otherwise.

Definition at line 444 of file datafiles.c.

References dbFILE_::fp.

Referenced by load_bs_dbase(), load_cs_dbase(), load_exceptions(), load_hs_dbase_v2(), load_hs_dbase_v3(), load_lang(), load_news(), load_ns_dbase(), load_ns_req_db(), load_old_akill(), load_old_ns_dbase(), load_os_dbase(), and main().

int read_ptr ( void **  ret,
dbFILE f 
)

Read Pointer

Parameters:
ret pointer to read
dbFile struct
Returns:
-1 on error, 0 otherwise.

Definition at line 489 of file datafiles.c.

References c, and dbFILE_::fp.

int read_string ( char **  ret,
dbFILE f 
)

Read String

Parameters:
ret string
dbFile struct
Returns:
-1 on error, 0 otherwise.

Definition at line 525 of file datafiles.c.

References dbFILE_::fp, read_int16(), and scalloc().

static void remove_backups ( void   )  [static]

Removes old databases

Returns:
void

Definition at line 601 of file datafiles.c.

References BotDBName, ChanDBName, ExceptionDBName, HostDBName, KeepBackups, NewsDBName, NickDBName, NSEmailReg, OperDBName, PreNickDBName, s_BotServ, s_HostServ, and snprintf().

Referenced by backup_databases().

static void rename_database ( char *  name,
char *  ext 
) [static]

Renames a database

Parameters:
name Database to name
ext Extention
Returns:
void

Definition at line 581 of file datafiles.c.

References alog(), anope_cmd_global(), s_OperServ, and snprintf().

Referenced by backup_databases(), and ModuleDatabaseBackup().

void restore_db ( dbFILE f  ) 

Restore the database file to its condition before open_db(). This is identical to close_db() for files open for reading; however, for files open for writing, we first attempt to restore any backup file before closing files.

Parameters:
dbFile struct
Returns:
void

Definition at line 318 of file datafiles.c.

References dbFILE_::backupfp, dbFILE_::backupname, dbFILE_::filename, dbFILE_::fp, log_perror(), and dbFILE_::mode.

int write_file_version ( dbFILE f,
uint32  version 
)

Write the current version number to the file.

Parameters:
f dbFile Struct Member
Returns:
0 on error, 1 on success.

Definition at line 62 of file datafiles.c.

References dbFILE_::filename, dbFILE_::fp, and log_perror().

int write_int16 ( uint16  val,
dbFILE f 
)

Write a 16bit integer

Parameters:
ret 16bit integer to write
dbFile struct
Returns:
-1 on error, 0 otherwise.

Definition at line 426 of file datafiles.c.

References dbFILE_::fp.

int write_int32 ( uint32  val,
dbFILE f 
)

Write a unsigned 32bit integer

Parameters:
ret unsigned 32bit integer to write
dbFile struct
Returns:
-1 on error, 0 otherwise.

Definition at line 467 of file datafiles.c.

References dbFILE_::fp.

int write_ptr ( const void *  ptr,
dbFILE f 
)

Write Pointer

Parameters:
ret pointer to write
dbFile struct
Returns:
-1 on error, 0 otherwise.

Definition at line 509 of file datafiles.c.

References dbFILE_::fp.

int write_string ( const char *  s,
dbFILE f 
)

Write String

Parameters:
ret string
dbFile struct
Returns:
-1 on error, 0 otherwise.

Definition at line 554 of file datafiles.c.

References dbFILE_::fp, and write_int16().


Variable Documentation

int curday = 0 [static]

Definition at line 18 of file datafiles.c.

Referenced by backup_databases(), and ModuleDatabaseBackup().

time_t lastwarn = 0 [static]