Rizin
unix-like reverse engineering framework and cli tools
journal.c
Go to the documentation of this file.
1 // SPDX-FileCopyrightText: 2011-2016 pancake <pancake@nopcode.org>
2 // SPDX-License-Identifier: MIT
3 
4 #include "sdb.h"
5 #include <fcntl.h>
6 
7 static const char *sdb_journal_filename(Sdb *s) {
8  return (s && s->name)
9  ? sdb_fmt("%s.journal", s->name)
10  : NULL;
11 }
12 
14  if (s->journal == -1) {
15  return false;
16  }
17  close(s->journal);
18  s->journal = -1;
20  return true;
21 }
22 
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 }
36 
37 // TODO boolify and save changes somewhere else? or just dont count that?
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 }
80 
81 RZ_API bool sdb_journal_log(Sdb *s, const char *key, const char *val) {
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 }
95 
97  if (s->journal != -1) {
98  return !ftruncate(s->journal, 0);
99  }
100  return false;
101 }
102 
104  const char *filename = sdb_journal_filename(s);
106  if (filename) {
107  return !unlink(filename);
108  }
109  return false;
110 }
size_t len
Definition: 6502dis.c:15
ut16 val
Definition: armass64_const.h:6
#define RZ_API
#define NULL
Definition: cris-opc.c:27
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 static fork write
Definition: sflib.h:33
static static fork const void static count close
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
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 char * sdb_fmt(const char *fmt,...)
Definition: fmt.c:26
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char * filename
Definition: ioapi.h:137
static const char * sdb_journal_filename(Sdb *s)
Definition: journal.c:7
RZ_API bool sdb_journal_open(Sdb *s)
Definition: journal.c:23
RZ_API int sdb_journal_load(Sdb *s)
Definition: journal.c:38
RZ_API bool sdb_journal_clear(Sdb *s)
Definition: journal.c:96
RZ_API bool sdb_journal_log(Sdb *s, const char *key, const char *val)
Definition: journal.c:81
RZ_API bool sdb_journal_unlink(Sdb *s)
Definition: journal.c:103
RZ_API bool sdb_journal_close(Sdb *s)
Definition: journal.c:13
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char static mode unlink
Definition: sflib.h:41
static RzSocket * s
Definition: rtr.c:28
RZ_API int sdb_set(Sdb *s, const char *key, const char *val, ut32 cas)
Definition: sdb.c:611
#define O_CREAT
Definition: sftypes.h:489
#define O_RDWR
Definition: sftypes.h:488
#define O_APPEND
Definition: sftypes.h:493
Definition: sdb.h:63
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