Rizin
unix-like reverse engineering framework and cli tools
gzlog.h File Reference

Go to the source code of this file.

Typedefs

typedef void gzlog
 

Functions

gzloggzlog_open (char *path)
 
int gzlog_write (gzlog *log, void *data, size_t len)
 
int gzlog_compress (gzlog *log)
 
int gzlog_close (gzlog *log)
 

Typedef Documentation

◆ gzlog

typedef void gzlog

Definition at line 52 of file gzlog.h.

Function Documentation

◆ gzlog_close()

int gzlog_close ( gzlog log)

Definition at line 1044 of file gzlog.c.

1045 {
1046  struct log *log = logd;
1047 
1048  /* check arguments */
1049  if (log == NULL || strcmp(log->id, LOGID))
1050  return -3;
1051 
1052  /* close the log file and release the lock */
1053  log_close(log);
1054 
1055  /* free structure and return */
1056  if (log->path != NULL)
1057  free(log->path);
1058  strcpy(log->id, "bad");
1059  free(log);
1060  return 0;
1061 }
#define NULL
Definition: cris-opc.c:27
void log_close(struct log *log)
Definition: gzlog.c:798
#define LOGID
Definition: gzlog.c:288
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
Definition: gzlog.c:289
char * path
Definition: gzlog.c:292
char id[4]
Definition: gzlog.c:290

References free(), log::id, log_close(), LOGID, NULL, and log::path.

◆ gzlog_compress()

int gzlog_compress ( gzlog log)

Definition at line 910 of file gzlog.c.

911 {
912  int fd, ret;
913  uint block;
914  size_t len, next;
915  unsigned char *data, buf[5];
916  struct log *log = logd;
917 
918  /* check arguments */
919  if (log == NULL || strcmp(log->id, LOGID))
920  return -3;
921 
922  /* see if we lost the lock -- if so get it again and reload the extra
923  field information (it probably changed), recover last operation if
924  necessary */
925  if (log_check(log) && log_open(log))
926  return -1;
927 
928  /* create space for uncompressed data */
929  len = ((size_t)(log->last - log->first) & ~(((size_t)1 << 10) - 1)) +
930  log->stored;
931  if ((data = malloc(len)) == NULL)
932  return -2;
933 
934  /* do statement here is just a cheap trick for error handling */
935  do {
936  /* read in the uncompressed data */
937  if (lseek(log->fd, log->first - 1, SEEK_SET) < 0)
938  break;
939  next = 0;
940  while (next < len) {
941  if (read(log->fd, buf, 5) != 5)
942  break;
943  block = PULL2(buf + 1);
944  if (next + block > len ||
945  read(log->fd, (char *)data + next, block) != block)
946  break;
947  next += block;
948  }
949  if (lseek(log->fd, 0, SEEK_CUR) != log->last + 4 + log->stored)
950  break;
951  log_touch(log);
952 
953  /* write the uncompressed data to the .add file */
954  strcpy(log->end, ".add");
955  fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
956  if (fd < 0)
957  break;
958  ret = (size_t)write(fd, data, len) != len;
959  if (ret | close(fd))
960  break;
961  log_touch(log);
962 
963  /* write the dictionary for the next compress to the .temp file */
964  strcpy(log->end, ".temp");
965  fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
966  if (fd < 0)
967  break;
968  next = DICT > len ? len : DICT;
969  ret = (size_t)write(fd, (char *)data + len - next, next) != next;
970  if (ret | close(fd))
971  break;
972  log_touch(log);
973 
974  /* roll back to compressed data, mark the compress in progress */
975  log->last = log->first;
976  log->stored = 0;
977  if (log_mark(log, COMPRESS_OP))
978  break;
979  BAIL(7);
980 
981  /* compress and append the data (clears mark) */
982  ret = log_compress(log, data, len);
983  free(data);
984  return ret;
985  } while (0);
986 
987  /* broke out of do above on i/o error */
988  free(data);
989  return -1;
990 }
size_t len
Definition: 6502dis.c:15
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 lseek
Definition: sflib.h:113
int log_mark(struct log *log, int op)
Definition: gzlog.c:437
#define BAIL(n)
Definition: gzlog.c:254
unsigned int uint
Definition: gzlog.c:242
int log_check(struct log *log)
Definition: gzlog.c:384
void log_touch(struct log *log)
Definition: gzlog.c:372
#define PULL2(p)
Definition: gzlog.c:278
#define COMPRESS_OP
Definition: gzlog.c:274
int log_open(struct log *log)
Definition: gzlog.c:815
int log_compress(struct log *log, unsigned char *data, size_t len)
Definition: gzlog.c:608
#define DICT
Definition: gzlog.c:269
voidpf void * buf
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123
#define O_WRONLY
Definition: sftypes.h:487
#define O_CREAT
Definition: sftypes.h:489
int size_t
Definition: sftypes.h:40
#define O_TRUNC
Definition: sftypes.h:492
uint stored
Definition: gzlog.c:296
off_t first
Definition: gzlog.c:294
off_t last
Definition: gzlog.c:297
int fd
Definition: gzlog.c:291
char * end
Definition: gzlog.c:293
static const z80_opcode fd[]
Definition: z80_tab.h:997
#define SEEK_SET
Definition: zip.c:88
#define SEEK_CUR
Definition: zip.c:80
int read(izstream &zs, T *x, Items items)
Definition: zstream.h:115

References BAIL, close, COMPRESS_OP, DICT, log::end, fd, log::fd, log::first, free(), log::id, log::last, len, log_check(), log_compress(), log_mark(), log_open(), log_touch(), LOGID, lseek, malloc(), NULL, O_CREAT, O_TRUNC, O_WRONLY, log::path, PULL2, read(), SEEK_CUR, SEEK_SET, log::stored, and write.

Referenced by gzlog_write().

◆ gzlog_open()

gzlog* gzlog_open ( char *  path)

Definition at line 867 of file gzlog.c.

868 {
869  size_t n;
870  struct log *log;
871 
872  /* check arguments */
873  if (path == NULL || *path == 0)
874  return NULL;
875 
876  /* allocate and initialize log structure */
877  log = malloc(sizeof(struct log));
878  if (log == NULL)
879  return NULL;
880  strcpy(log->id, LOGID);
881  log->fd = -1;
882 
883  /* save path and end of path for name construction */
884  n = strlen(path);
885  log->path = malloc(n + 9); /* allow for ".repairs" */
886  if (log->path == NULL) {
887  free(log);
888  return NULL;
889  }
890  strcpy(log->path, path);
891  log->end = log->path + n;
892 
893  /* gain exclusive access and verify log file -- may perform a
894  recovery operation if needed */
895  if (log_open(log)) {
896  free(log->path);
897  free(log);
898  return NULL;
899  }
900 
901  /* return pointer to log structure */
902  return log;
903 }
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
int n
Definition: mipsasm.c:19
def log(text)

References log::end, log::fd, free(), log::id, test-lz4-list::log(), log_open(), LOGID, malloc(), n, NULL, path, and log::path.

◆ gzlog_write()

int gzlog_write ( gzlog log,
void *  data,
size_t  len 
)

Definition at line 997 of file gzlog.c.

998 {
999  int fd, ret;
1000  struct log *log = logd;
1001 
1002  /* check arguments */
1003  if (log == NULL || strcmp(log->id, LOGID))
1004  return -3;
1005  if (data == NULL || len <= 0)
1006  return 0;
1007 
1008  /* see if we lost the lock -- if so get it again and reload the extra
1009  field information (it probably changed), recover last operation if
1010  necessary */
1011  if (log_check(log) && log_open(log))
1012  return -1;
1013 
1014  /* create and write .add file */
1015  strcpy(log->end, ".add");
1016  fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
1017  if (fd < 0)
1018  return -1;
1019  ret = (size_t)write(fd, data, len) != len;
1020  if (ret | close(fd))
1021  return -1;
1022  log_touch(log);
1023 
1024  /* mark log file with append in progress */
1025  if (log_mark(log, APPEND_OP))
1026  return -1;
1027  BAIL(8);
1028 
1029  /* append data (clears mark) */
1030  if (log_append(log, data, len))
1031  return -1;
1032 
1033  /* check to see if it's time to compress -- if not, then done */
1034  if (((log->last - log->first) >> 10) + (log->stored >> 10) < TRIGGER)
1035  return 0;
1036 
1037  /* time to compress */
1038  return gzlog_compress(log);
1039 }
int gzlog_compress(gzlog *logd)
Definition: gzlog.c:910
int log_append(struct log *log, unsigned char *data, size_t len)
Definition: gzlog.c:501
#define APPEND_OP
Definition: gzlog.c:273
#define TRIGGER
Definition: gzlog.c:266

References APPEND_OP, BAIL, close, log::end, fd, log::first, gzlog_compress(), log::id, log::last, len, log_append(), log_check(), log_mark(), log_open(), log_touch(), LOGID, NULL, O_CREAT, O_TRUNC, O_WRONLY, log::path, log::stored, TRIGGER, and write.