1
2
3
4
5
6
7
8
9
10
11
12
|
/*
* gzlog.c
* Copyright (C) 2004, 2008 Mark Adler, all rights reserved
* For conditions of distribution and use, see copyright notice in gzlog.h
* version 2.0, 25 Apr 2008
*/
/*
gzlog provides a mechanism for frequently appending short strings to a gzip
file that is efficient both in execution time and compression ratio. The
strategy is to write the short strings in an uncompressed form to the end of
the gzip file, only compressing when the amount of uncompressed data has
|
|
|
|
1
2
3
4
5
6
7
8
9
10
11
12
|
/*
* gzlog.c
* Copyright (C) 2004, 2008, 2012 Mark Adler, all rights reserved
* For conditions of distribution and use, see copyright notice in gzlog.h
* version 2.2, 14 Aug 2012
*/
/*
gzlog provides a mechanism for frequently appending short strings to a gzip
file that is efficient both in execution time and compression ratio. The
strategy is to write the short strings in an uncompressed form to the end of
the gzip file, only compressing when the amount of uncompressed data has
|
| ︙ | | | ︙ | |
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
|
log_log(log, op, "start");
/* load foo.add file if expected and present */
if (op == APPEND_OP || op == COMPRESS_OP) {
strcpy(log->end, ".add");
if (stat(log->path, &st) == 0 && st.st_size) {
len = (size_t)(st.st_size);
if (len != st.st_size || (data = malloc(st.st_size)) == NULL) {
log_log(log, op, "allocation failure");
return -2;
}
if ((fd = open(log->path, O_RDONLY, 0)) < 0) {
log_log(log, op, ".add file read failure");
return -1;
}
ret = read(fd, data, len) != len;
close(fd);
if (ret) {
log_log(log, op, ".add file read failure");
return -1;
}
log_log(log, op, "loaded .add file");
}
|
>
|
|
|
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
|
log_log(log, op, "start");
/* load foo.add file if expected and present */
if (op == APPEND_OP || op == COMPRESS_OP) {
strcpy(log->end, ".add");
if (stat(log->path, &st) == 0 && st.st_size) {
len = (size_t)(st.st_size);
if ((off_t)len != st.st_size ||
(data = malloc(st.st_size)) == NULL) {
log_log(log, op, "allocation failure");
return -2;
}
if ((fd = open(log->path, O_RDONLY, 0)) < 0) {
log_log(log, op, ".add file read failure");
return -1;
}
ret = (size_t)read(fd, data, len) != len;
close(fd);
if (ret) {
log_log(log, op, ".add file read failure");
return -1;
}
log_log(log, op, "loaded .add file");
}
|
| ︙ | | | ︙ | |
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
|
int fd, ret;
uint block;
size_t len, next;
unsigned char *data, buf[5];
struct log *log = logd;
/* check arguments */
if (log == NULL || strcmp(log->id, LOGID) || len < 0)
return -3;
/* see if we lost the lock -- if so get it again and reload the extra
field information (it probably changed), recover last operation if
necessary */
if (log_check(log) && log_open(log))
return -1;
|
|
|
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
|
int fd, ret;
uint block;
size_t len, next;
unsigned char *data, buf[5];
struct log *log = logd;
/* check arguments */
if (log == NULL || strcmp(log->id, LOGID))
return -3;
/* see if we lost the lock -- if so get it again and reload the extra
field information (it probably changed), recover last operation if
necessary */
if (log_check(log) && log_open(log))
return -1;
|
| ︙ | | | ︙ | |
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
|
log_touch(log);
/* write the uncompressed data to the .add file */
strcpy(log->end, ".add");
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
break;
ret = write(fd, data, len) != len;
if (ret | close(fd))
break;
log_touch(log);
/* write the dictionary for the next compress to the .temp file */
strcpy(log->end, ".temp");
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
break;
next = DICT > len ? len : DICT;
ret = write(fd, (char *)data + len - next, next) != next;
if (ret | close(fd))
break;
log_touch(log);
/* roll back to compressed data, mark the compress in progress */
log->last = log->first;
log->stored = 0;
|
|
|
|
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
log_touch(log);
/* write the uncompressed data to the .add file */
strcpy(log->end, ".add");
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
break;
ret = (size_t)write(fd, data, len) != len;
if (ret | close(fd))
break;
log_touch(log);
/* write the dictionary for the next compress to the .temp file */
strcpy(log->end, ".temp");
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
break;
next = DICT > len ? len : DICT;
ret = (size_t)write(fd, (char *)data + len - next, next) != next;
if (ret | close(fd))
break;
log_touch(log);
/* roll back to compressed data, mark the compress in progress */
log->last = log->first;
log->stored = 0;
|
| ︙ | | | ︙ | |
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
|
-3: invalid log pointer argument */
int gzlog_write(gzlog *logd, void *data, size_t len)
{
int fd, ret;
struct log *log = logd;
/* check arguments */
if (log == NULL || strcmp(log->id, LOGID) || len < 0)
return -3;
if (data == NULL || len == 0)
return 0;
/* see if we lost the lock -- if so get it again and reload the extra
field information (it probably changed), recover last operation if
necessary */
if (log_check(log) && log_open(log))
return -1;
/* create and write .add file */
strcpy(log->end, ".add");
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
return -1;
ret = write(fd, data, len) != len;
if (ret | close(fd))
return -1;
log_touch(log);
/* mark log file with append in progress */
if (log_mark(log, APPEND_OP))
return -1;
|
|
|
|
|
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
|
-3: invalid log pointer argument */
int gzlog_write(gzlog *logd, void *data, size_t len)
{
int fd, ret;
struct log *log = logd;
/* check arguments */
if (log == NULL || strcmp(log->id, LOGID))
return -3;
if (data == NULL || len <= 0)
return 0;
/* see if we lost the lock -- if so get it again and reload the extra
field information (it probably changed), recover last operation if
necessary */
if (log_check(log) && log_open(log))
return -1;
/* create and write .add file */
strcpy(log->end, ".add");
fd = open(log->path, O_WRONLY | O_CREAT | O_TRUNC, 0644);
if (fd < 0)
return -1;
ret = (size_t)write(fd, data, len) != len;
if (ret | close(fd))
return -1;
log_touch(log);
/* mark log file with append in progress */
if (log_mark(log, APPEND_OP))
return -1;
|
| ︙ | | | ︙ | |