author | Edouard Tisserant |
Mon, 13 Aug 2018 15:00:51 +0200 | |
changeset 2265 | 2de61e2fbf14 |
parent 2227 | f150783ee8e7 |
child 2499 | 68f4f2d4516b |
permissions | -rw-r--r-- |
2178
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
1 |
/* |
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
2 |
This file is part of Beremiz, a Integrated Development Environment for |
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
3 |
programming IEC 61131-3 automates supporting plcopen standard and CanFestival. |
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
4 |
|
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
5 |
See COPYING.runtime |
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
6 |
|
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
7 |
Copyright (C) 2018: Sergey Surkov <surkov.sv@summatechnology.ru> |
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
8 |
Copyright (C) 2018: Andrey Skvortsov <andrej.skvortzov@gmail.com> |
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
9 |
|
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
10 |
*/ |
bd0d13d10b8e
Add copyright headers to retain implementation for GNU/Linux and Win32
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2173
diff
changeset
|
11 |
|
2173
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
12 |
#ifndef HAVE_RETAIN |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
13 |
#include <stdio.h> |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
14 |
#include <stdint.h> |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
15 |
#include <unistd.h> |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
16 |
#include "iec_types.h" |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
17 |
|
2227
f150783ee8e7
Fix some compilation warnings for better ANSI C compliance
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
2178
diff
changeset
|
18 |
int GetRetainSize(void); |
2173
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
19 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
20 |
/* Retain buffer. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
21 |
FILE *retain_buffer; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
22 |
const char rb_file[] = "retain_buffer_file"; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
23 |
const char rb_file_bckp[] = "retain_buffer_file.bak"; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
24 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
25 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
26 |
/* Retain header struct. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
27 |
struct retain_info_t { |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
28 |
uint32_t retain_size; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
29 |
uint32_t hash_size; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
30 |
uint8_t* hash; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
31 |
uint32_t header_offset; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
32 |
uint32_t header_crc; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
33 |
}; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
34 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
35 |
/* Init retain info structure. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
36 |
struct retain_info_t retain_info; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
37 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
38 |
/* CRC lookup table and initial state. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
39 |
uint32_t crc32_table[256]; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
40 |
uint32_t retain_crc; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
41 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
42 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
43 |
/* Generate CRC32 lookup table. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
44 |
void GenerateCRC32Table(void) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
45 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
46 |
unsigned int i, j; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
47 |
/* Use CRC-32-IEEE 802.3 polynomial 0x04C11DB7 (bit reflected). */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
48 |
uint32_t poly = 0xEDB88320; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
49 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
50 |
for (i = 0; i <= 0xFF; i++) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
51 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
52 |
uint32_t c = i; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
53 |
for (j = 0 ; j < 8 ; j++) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
54 |
c = (c & 1) ? (c >> 1 ) ^ poly : (c >> 1); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
55 |
crc32_table[i] = c; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
56 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
57 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
58 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
59 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
60 |
/* Calculate CRC32 for len bytes from pointer buf with init starting value. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
61 |
uint32_t GenerateCRC32Sum(const void* buf, unsigned int len, uint32_t init) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
62 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
63 |
uint32_t crc = ~init; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
64 |
unsigned char* current = (unsigned char*) buf; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
65 |
while (len--) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
66 |
crc = crc32_table[(crc ^ *current++) & 0xFF] ^ (crc >> 8); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
67 |
return ~crc; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
68 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
69 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
70 |
/* Calc CRC32 for retain file byte by byte. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
71 |
int CheckFileCRC(FILE* file_buffer) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
72 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
73 |
/* Set the magic constant for one-pass CRC calc according to ZIP CRC32. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
74 |
const uint32_t magic_number = 0x2144df1c; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
75 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
76 |
/* CRC initial state. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
77 |
uint32_t calc_crc32 = 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
78 |
char data_block = 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
79 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
80 |
while(!feof(file_buffer)){ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
81 |
if (fread(&data_block, sizeof(data_block), 1, file_buffer)) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
82 |
calc_crc32 = GenerateCRC32Sum(&data_block, sizeof(char), calc_crc32); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
83 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
84 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
85 |
/* Compare crc result with a magic number. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
86 |
return (calc_crc32 == magic_number) ? 1 : 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
87 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
88 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
89 |
/* Compare current hash with hash from file byte by byte. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
90 |
int CheckFilehash(void) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
91 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
92 |
int k; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
93 |
int offset = sizeof(retain_info.retain_size); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
94 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
95 |
rewind(retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
96 |
fseek(retain_buffer, offset , SEEK_SET); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
97 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
98 |
uint32_t size; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
99 |
fread(&size, sizeof(size), 1, retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
100 |
if (size != retain_info.hash_size) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
101 |
return 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
102 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
103 |
for(k = 0; k < retain_info.hash_size; k++){ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
104 |
uint8_t file_digit; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
105 |
fread(&file_digit, sizeof(char), 1, retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
106 |
if (file_digit != *(retain_info.hash+k)) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
107 |
return 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
108 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
109 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
110 |
return 1; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
111 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
112 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
113 |
void InitRetain(void) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
114 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
115 |
int i; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
116 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
117 |
/* Generate CRC32 lookup table. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
118 |
GenerateCRC32Table(); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
119 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
120 |
/* Get retain size in bytes */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
121 |
retain_info.retain_size = GetRetainSize(); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
122 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
123 |
/* Hash stored in retain file as array of char in hex digits |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
124 |
(that's why we divide strlen in two). */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
125 |
retain_info.hash_size = PLC_ID ? strlen(PLC_ID)/2 : 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
126 |
//retain_info.hash_size = 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
127 |
retain_info.hash = malloc(retain_info.hash_size); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
128 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
129 |
/* Transform hash string into byte sequence. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
130 |
for (i = 0; i < retain_info.hash_size; i++) { |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
131 |
int byte = 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
132 |
sscanf((PLC_ID + i*2), "%02X", &byte); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
133 |
retain_info.hash[i] = byte; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
134 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
135 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
136 |
/* Calc header offset. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
137 |
retain_info.header_offset = sizeof(retain_info.retain_size) + \ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
138 |
sizeof(retain_info.hash_size) + \ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
139 |
retain_info.hash_size; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
140 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
141 |
/* Set header CRC initial state. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
142 |
retain_info.header_crc = 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
143 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
144 |
/* Calc crc for header. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
145 |
retain_info.header_crc = GenerateCRC32Sum( |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
146 |
&retain_info.retain_size, |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
147 |
sizeof(retain_info.retain_size), |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
148 |
retain_info.header_crc); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
149 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
150 |
retain_info.header_crc = GenerateCRC32Sum( |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
151 |
&retain_info.hash_size, |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
152 |
sizeof(retain_info.hash_size), |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
153 |
retain_info.header_crc); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
154 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
155 |
retain_info.header_crc = GenerateCRC32Sum( |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
156 |
retain_info.hash, |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
157 |
retain_info.hash_size, |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
158 |
retain_info.header_crc); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
159 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
160 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
161 |
void CleanupRetain(void) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
162 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
163 |
/* Free hash memory. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
164 |
free(retain_info.hash); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
165 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
166 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
167 |
int CheckRetainFile(const char * file) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
168 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
169 |
retain_buffer = fopen(file, "rb"); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
170 |
if (retain_buffer) { |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
171 |
/* Check CRC32 and hash. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
172 |
if (CheckFileCRC(retain_buffer)) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
173 |
if (CheckFilehash()) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
174 |
return 1; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
175 |
fclose(retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
176 |
retain_buffer = NULL; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
177 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
178 |
return 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
179 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
180 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
181 |
int CheckRetainBuffer(void) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
182 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
183 |
retain_buffer = NULL; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
184 |
if (!retain_info.retain_size) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
185 |
return 1; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
186 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
187 |
/* Check latest retain file. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
188 |
if (CheckRetainFile(rb_file)) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
189 |
return 1; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
190 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
191 |
/* Check if we have backup. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
192 |
if (CheckRetainFile(rb_file_bckp)) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
193 |
return 1; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
194 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
195 |
/* We don't have any valid retain buffer - nothing to remind. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
196 |
return 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
197 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
198 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
199 |
#ifndef FILE_RETAIN_SAVE_PERIOD_S |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
200 |
#define FILE_RETAIN_SAVE_PERIOD_S 1.0 |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
201 |
#endif |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
202 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
203 |
static double CalcDiffSeconds(IEC_TIME* t1, IEC_TIME *t2) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
204 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
205 |
IEC_TIME dt ={ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
206 |
t1->tv_sec - t2->tv_sec, |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
207 |
t1->tv_nsec - t2->tv_nsec |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
208 |
}; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
209 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
210 |
if ((dt.tv_nsec < -1000000000) || ((dt.tv_sec > 0) && (dt.tv_nsec < 0))){ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
211 |
dt.tv_sec--; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
212 |
dt.tv_nsec += 1000000000; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
213 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
214 |
if ((dt.tv_nsec > +1000000000) || ((dt.tv_sec < 0) && (dt.tv_nsec > 0))){ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
215 |
dt.tv_sec++; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
216 |
dt.tv_nsec -= 1000000000; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
217 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
218 |
return dt.tv_sec + 1e-9*dt.tv_nsec; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
219 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
220 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
221 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
222 |
int RetainSaveNeeded(void) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
223 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
224 |
int ret = 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
225 |
static IEC_TIME last_save; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
226 |
IEC_TIME now; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
227 |
double diff_s; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
228 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
229 |
/* no retain */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
230 |
if (!retain_info.retain_size) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
231 |
return 0; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
232 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
233 |
/* periodic retain flush to avoid high I/O load */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
234 |
PLC_GetTime(&now); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
235 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
236 |
diff_s = CalcDiffSeconds(&now, &last_save); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
237 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
238 |
if ((diff_s > FILE_RETAIN_SAVE_PERIOD_S) || ForceSaveRetainReq()) { |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
239 |
ret = 1; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
240 |
last_save = now; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
241 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
242 |
return ret; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
243 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
244 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
245 |
void ValidateRetainBuffer(void) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
246 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
247 |
if (!retain_buffer) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
248 |
return; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
249 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
250 |
/* Add retain data CRC to the end of buffer file. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
251 |
fseek(retain_buffer, 0, SEEK_END); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
252 |
fwrite(&retain_crc, sizeof(uint32_t), 1, retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
253 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
254 |
/* Sync file buffer and close file. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
255 |
#ifdef __WIN32 |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
256 |
fflush(retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
257 |
#else |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
258 |
fsync(fileno(retain_buffer)); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
259 |
#endif |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
260 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
261 |
fclose(retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
262 |
retain_buffer = NULL; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
263 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
264 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
265 |
void InValidateRetainBuffer(void) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
266 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
267 |
if (!RetainSaveNeeded()) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
268 |
return; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
269 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
270 |
/* Rename old retain file into *.bak if it exists. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
271 |
rename(rb_file, rb_file_bckp); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
272 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
273 |
/* Set file CRC initial value. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
274 |
retain_crc = retain_info.header_crc; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
275 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
276 |
/* Create new retain file. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
277 |
retain_buffer = fopen(rb_file, "wb+"); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
278 |
if (!retain_buffer) { |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
279 |
fprintf(stderr, "Failed to create retain file : %s\n", rb_file); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
280 |
return; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
281 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
282 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
283 |
/* Write header to the new file. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
284 |
fwrite(&retain_info.retain_size, |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
285 |
sizeof(retain_info.retain_size), 1, retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
286 |
fwrite(&retain_info.hash_size, |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
287 |
sizeof(retain_info.hash_size), 1, retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
288 |
fwrite(retain_info.hash , |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
289 |
sizeof(char), retain_info.hash_size, retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
290 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
291 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
292 |
void Retain(unsigned int offset, unsigned int count, void *p) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
293 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
294 |
if (!retain_buffer) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
295 |
return; |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
296 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
297 |
/* Generate CRC 32 for each data block. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
298 |
retain_crc = GenerateCRC32Sum(p, count, retain_crc); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
299 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
300 |
/* Save current var in file. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
301 |
fseek(retain_buffer, retain_info.header_offset+offset, SEEK_SET); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
302 |
fwrite(p, count, 1, retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
303 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
304 |
|
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
305 |
void Remind(unsigned int offset, unsigned int count, void *p) |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
306 |
{ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
307 |
/* Remind variable from file. */ |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
308 |
fseek(retain_buffer, retain_info.header_offset+offset, SEEK_SET); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
309 |
fread((void *)p, count, 1, retain_buffer); |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
310 |
} |
976841968d74
Add retain basic implementation on GNU/Linux
Andrey Skvortsov <andrej.skvortzov@gmail.com>
parents:
diff
changeset
|
311 |
#endif // !HAVE_RETAIN |