-
Notifications
You must be signed in to change notification settings - Fork 7
/
sysutil.cpp
executable file
·163 lines (144 loc) · 3.79 KB
/
sysutil.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "sysutil.h"
#include <iostream>
#include <string>
#include "codec.h"
#include "protobuf/filesync.pb.h"
using namespace std;
namespace sysutil{
/**
* writen - 发送固定字节数
* @fd: 文件描述符
* @buf: 发送缓冲区
* @count: 要读取的字节数
* 成功返回count,失败返回-1
*/
ssize_t writen(int fd, const void *buf, size_t count)
{
size_t nleft = count;
ssize_t nwritten;
char *bufp = (char*)buf;
while (nleft > 0)
{
if ((nwritten = write(fd, bufp, nleft)) < 0)
{
if (errno == EINTR)
continue;
return -1;
}
else if (nwritten == 0)
continue;
bufp += nwritten;
nleft -= nwritten;
}
return count;
}
//从应用层缓冲区读取文件数据,append到文件后面
void fileRecvfromBuf(const char *filename,const char *buf,int size)
{
int fd = open(filename, O_CREAT | O_WRONLY, 0666);
if (fd == -1)
{
CHEN_LOG(ERROR,"Could not create file %s",filename);
return;
}
lseek(fd,0,SEEK_END);
if(write(fd,buf,size) < 0)
CHEN_LOG(ERROR,"write file %s to %d error",filename,fd);
close(fd);
}
/**
* @brief send_SyncInfo 发送SyncInfo信息,线程安全
* @param conn
* @param id 0是创建文件夹,1是create文件,2是modify,3是删除,4是重命名
* @param filename
* @param newname 新文件名,发送重命名信息时才用
* @param removedSize 删除正在发送的文件时,已发送的大小
*/
void send_SyncInfo(const muduo::net::TcpConnectionPtr &conn, int id,
string filename, string newname,int removedSize)
{
filesync::SyncInfo msg;
msg.set_id(id);
msg.set_filename(filename);
msg.set_size(removedSize);
if(4 == id) //重命名
msg.set_newfilename(newname);
string send = Codec::enCode(msg);
conn->send(send);
}
/**
* @brief getFileMd5 利用qt库获取md5,支持大文件
* @param filePath 文件名
* @return
*/
string getFileMd5(string filePath)
{
QFile localFile(QString::fromStdString(filePath));
if (!localFile.open(QFile::ReadOnly))
{
qDebug() << "file open error.";
return 0;
}
QCryptographicHash ch(QCryptographicHash::Md5);
quint64 totalBytes = 0;
quint64 bytesWritten = 0;
quint64 bytesToWrite = 0;
quint64 loadSize = 1024 * 4;
QByteArray buf;
totalBytes = localFile.size();
bytesToWrite = totalBytes;
while (1)
{
if(bytesToWrite > 0)
{
buf = localFile.read(qMin(bytesToWrite, loadSize));
ch.addData(buf);
bytesWritten += buf.length();
bytesToWrite -= buf.length();
buf.resize(0);
}
else
{
break;
}
if(bytesWritten == totalBytes)
{
break;
}
}
localFile.close();
QString md5 = ch.result().toHex();
return md5.toStdString();
}
//获取字符串的md5
string getStringMd5(char *buffer)
{
QCryptographicHash ch(QCryptographicHash::Md5);
QByteArray buf(buffer);
ch.addData(buf);
QString md5 = ch.result().toHex();
return md5.toStdString();
}
unsigned int adler32(char * data, int len)
/* where data is the location of the data in physical memory and len is the length of the data in bytes */
{
unsigned int a = 1, b = 0;
int index;
/* Process each byte of the data in order */
for (index = 0; index < len; ++index)
{
a = a + data[index];
b = b + a ;
}
return (b * 65536) + a;
}
unsigned int adler32_rolling_checksum(unsigned int csum, int len, char c1, char c2)
{
unsigned int s1, s2;
s1 = (csum & 0xffff);
s2 = (csum >> 16);
s1 = (s1- c1) + c2;
s2 = s2- (len * c1 - s1)-1;
return (s2 * 65536) + s1;
}
}