Rizin
unix-like reverse engineering framework and cli tools
journal.c File Reference
#include "sdb.h"
#include <fcntl.h>

Go to the source code of this file.

Functions

static const char * sdb_journal_filename (Sdb *s)
 
RZ_API bool sdb_journal_close (Sdb *s)
 
RZ_API bool sdb_journal_open (Sdb *s)
 
RZ_API int sdb_journal_load (Sdb *s)
 
RZ_API bool sdb_journal_log (Sdb *s, const char *key, const char *val)
 
RZ_API bool sdb_journal_clear (Sdb *s)
 
RZ_API bool sdb_journal_unlink (Sdb *s)
 

Function Documentation

◆ sdb_journal_clear()

RZ_API bool sdb_journal_clear ( Sdb s)

Definition at line 96 of file journal.c.

96  {
97  if (s->journal != -1) {
98  return !ftruncate(s->journal, 0);
99  }
100  return false;
101 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence ftruncate
Definition: sflib.h:113
static RzSocket * s
Definition: rtr.c:28

References ftruncate, and s.

Referenced by sdb_config(), and sdb_sync().

◆ sdb_journal_close()

RZ_API bool sdb_journal_close ( Sdb s)

Definition at line 13 of file journal.c.

13  {
14  if (s->journal == -1) {
15  return false;
16  }
17  close(s->journal);
18  s->journal = -1;
20  return true;
21 }
static static fork const void static count close
Definition: sflib.h:33
static const char * sdb_journal_filename(Sdb *s)
Definition: journal.c:7
static static fork const void static count static fd const char static mode unlink
Definition: sflib.h:41

References close, s, sdb_journal_filename(), and unlink.

Referenced by sdb_config(), sdb_fini(), and sdb_journal_unlink().

◆ sdb_journal_filename()

static const char* sdb_journal_filename ( Sdb s)
static

Definition at line 7 of file journal.c.

7  {
8  return (s && s->name)
9  ? sdb_fmt("%s.journal", s->name)
10  : NULL;
11 }
#define NULL
Definition: cris-opc.c:27
RZ_API char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26

References NULL, s, and sdb_fmt().

Referenced by sdb_journal_close(), sdb_journal_open(), and sdb_journal_unlink().

◆ sdb_journal_load()

RZ_API int sdb_journal_load ( Sdb s)

Definition at line 38 of file journal.c.

38  {
39  int rr, sz, fd, changes = 0;
40  char *eq, *str, *cur, *ptr = NULL;
41  if (!s) {
42  return 0;
43  }
44  fd = s->journal;
45  if (fd == -1) {
46  return 0;
47  }
48  sz = lseek(fd, 0, SEEK_END);
49  if (sz < 1) {
50  return 0;
51  }
52  lseek(fd, 0, SEEK_SET);
53  str = malloc(sz + 1);
54  if (!str) {
55  return 0;
56  }
57  rr = read(fd, str, sz);
58  if (rr < 0) {
59  free(str);
60  return 0;
61  }
62  str[sz] = 0;
63  for (cur = str;;) {
64  ptr = strchr(cur, '\n');
65  if (!ptr) {
66  break;
67  }
68  *ptr = 0;
69  eq = strchr(cur, '=');
70  if (eq) {
71  *eq++ = 0;
72  sdb_set(s, cur, eq, 0);
73  changes++;
74  }
75  cur = ptr + 1;
76  }
77  free(str);
78  return changes;
79 }
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep lseek
Definition: sflib.h:113
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
void * malloc(size_t size)
Definition: malloc.c:123
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
static const z80_opcode fd[]
Definition: z80_tab.h:997
#define SEEK_SET
Definition: zip.c:88
#define SEEK_END
Definition: zip.c:84
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References eq, fd, free(), lseek, malloc(), NULL, read(), s, sdb_set(), SEEK_END, SEEK_SET, and cmd_descs_generate::str.

Referenced by sdb_config().

◆ sdb_journal_log()

RZ_API bool sdb_journal_log ( Sdb s,
const char *  key,
const char *  val 
)

Definition at line 81 of file journal.c.

81  {
82  if (s->journal == -1) {
83  return false;
84  }
85  const char *str = sdb_fmt("%s=%s\n", key, val);
86  int len = strlen(str);
87  if (write(s->journal, str, len) != len) {
88  return false;
89  }
90 #if HAVE_HEADER_SYS_MMAN_H
91  (void)fsync(s->journal);
92 #endif
93  return true;
94 }
size_t len
Definition: 6502dis.c:15
ut16 val
Definition: armass64_const.h:6
static static fork write
Definition: sflib.h:33
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len key
Definition: sflib.h:118
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg fsync
Definition: sflib.h:79

References fsync, key, len, s, sdb_fmt(), cmd_descs_generate::str, val, and write.

Referenced by sdb_set_internal().

◆ sdb_journal_open()

RZ_API bool sdb_journal_open ( Sdb s)

Definition at line 23 of file journal.c.

23  {
24  const char *filename;
25  if (!s || !s->name) {
26  return false;
27  }
29  if (!filename) {
30  return false;
31  }
32  close(s->journal);
33  s->journal = open(filename, O_CREAT | O_RDWR | O_APPEND, 0600);
34  return s->journal != -1;
35 }
const char * filename
Definition: ioapi.h:137
#define O_CREAT
Definition: sftypes.h:489
#define O_RDWR
Definition: sftypes.h:488
#define O_APPEND
Definition: sftypes.h:493

References close, O_APPEND, O_CREAT, O_RDWR, s, and sdb_journal_filename().

Referenced by sdb_config().

◆ sdb_journal_unlink()

RZ_API bool sdb_journal_unlink ( Sdb s)

Definition at line 103 of file journal.c.

103  {
104  const char *filename = sdb_journal_filename(s);
106  if (filename) {
107  return !unlink(filename);
108  }
109  return false;
110 }
RZ_API bool sdb_journal_close(Sdb *s)
Definition: journal.c:13

References s, sdb_journal_close(), sdb_journal_filename(), and unlink.