Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
88 commits
Select commit Hold shift + click to select a range
f22ff46
base
ghorges Apr 27, 2019
00267e0
first
ghorges May 3, 2019
215e774
README.md
ghorges May 3, 2019
e4b0020
some bugs
ghorges May 4, 2019
2caf692
some bugs
ghorges May 4, 2019
a649e42
README.md
ghorges May 4, 2019
3dad36b
some bugs
ghorges May 5, 2019
81ef090
version2
ghorges May 18, 2019
fb5117e
change bugs
ghorges May 19, 2019
03723fc
修复了Epoll
yaomer May 19, 2019
382311f
deleted old version
yaomer May 19, 2019
32b80af
deleted old files
yaomer May 19, 2019
4a2d923
new version
yaomer May 19, 2019
154c361
x
yaomer May 20, 2019
b739268
Create LICENSE
okokme May 26, 2019
c4acc5a
线程使用std::thread
yaomer May 27, 2019
cb96a3e
Merge branch 'master' of https://github.com/xiyou-linuxer/deepin-file…
yaomer May 27, 2019
b05ea29
deleted tmp.c
yaomer May 28, 2019
bad9c28
deleted tmp.cc
yaomer May 28, 2019
1c8b064
Merge branch 'master' of https://github.com/xiyou-linuxer/deepin-file…
yaomer May 28, 2019
ed68e7e
fix bug
ghorges May 28, 2019
266bbdf
fix bug
ghorges May 28, 2019
5739883
fix bug
ghorges May 28, 2019
4f1a9b5
fix bug
ghorges May 28, 2019
2a87c7e
fix bug
ghorges May 29, 2019
4f5df04
README.md
ghorges May 29, 2019
d4f7b09
README.md
ghorges May 29, 2019
7587041
README.md
ghorges May 29, 2019
3e74dbe
add some sh
ghorges May 30, 2019
d4cab0e
rmversion1
ghorges May 30, 2019
63f1d74
rmversion1
ghorges May 30, 2019
ee0d4a6
server
ghorges May 30, 2019
f3fbf88
rm log
ghorges May 30, 2019
bfcf227
update project.md
yaomer May 30, 2019
c408d28
Merge branch 'master' of https://github.com/xiyou-linuxer/deepin-file…
yaomer May 30, 2019
21ca0c4
rm-client
ghorges May 30, 2019
cc80785
Merge branch 'master' of https://github.com/xiyou-linuxer/deepin-file…
ghorges May 30, 2019
15220dd
fix-some-bugs
ghorges May 30, 2019
0769744
fix-bugs
ghorges May 30, 2019
ef6e72d
change build.sh
ghorges May 30, 2019
ce0ad7d
fix-bugs
ghorges May 30, 2019
be77e60
rm log
ghorges May 30, 2019
e5f8325
update
ghorges May 30, 2019
229ba52
Update myhook.c
ghorges May 31, 2019
b8f9ac3
Update threadpool.h
ghorges May 31, 2019
95078df
fix-bugs
ghorges May 31, 2019
feb134a
update Buffer
yaomer Aug 19, 2019
ea6320a
Merge branch 'master' of https://github.com/xiyou-linuxer/deepin-file…
yaomer Aug 19, 2019
a07e24e
add zip
ghorges Aug 20, 2019
2304cc9
add zip
ghorges Aug 20, 2019
c6ce3c4
add zip
ghorges Aug 20, 2019
1aea1b1
Update README.md
ghorges Aug 21, 2019
6432bf1
Update README.md
ghorges Aug 21, 2019
277528a
add zip
ghorges Aug 21, 2019
4b68cbc
Merge branch 'master' of https://github.com/xiyou-linuxer/deepin-file…
ghorges Aug 21, 2019
8061764
move server src by:<xiyoulinuxgroup>
ghorges Aug 22, 2019
4b0ea81
fix make.sh bugs by:<xiyoulinuxgroup>
ghorges Aug 22, 2019
ce4ccdd
fix some bugs by:<xiyoulinuxgroup>
ghorges Aug 22, 2019
bc6fbc0
fix some bugs by:<xiyoulinuxgroup>
ghorges Aug 22, 2019
b9f72a8
fix some bugs by:<xiyoulinuxgroup>
ghorges Aug 22, 2019
d8e5be4
fix some bugs by:<xiyoulinuxgroup>
ghorges Aug 22, 2019
3cdf5a4
fix some bugs by:<xiyoulinuxgroup>
ghorges Aug 22, 2019
54fac31
Update README.md
okokme Aug 22, 2019
89b025c
Update README.md
okokme Aug 22, 2019
09dab5e
Update README.md
okokme Aug 22, 2019
c0609a4
Update README.md
okokme Aug 22, 2019
220dae9
Update README.md
okokme Aug 23, 2019
de75377
add doc by:<xiyoulinuxgroup>
okokme Aug 23, 2019
3403e7f
Update README.md
okokme Aug 23, 2019
8591628
Update README.md
okokme Aug 23, 2019
433f15c
update build.sh
yaomer Aug 23, 2019
8ca64af
fix some bugs by:<xiyoulinuxgroup>
ghorges Aug 23, 2019
d037f22
Update README.md
jialuhu Nov 3, 2019
bf412e9
fix some bugs by:<xiyoulinuxgroup>
ghorges Nov 5, 2019
4ee24c9
fix some note
jialuhu Nov 6, 2019
0c9961c
Update README.md
jialuhu Nov 6, 2019
6800fa0
Update README.md
jialuhu Nov 6, 2019
edb67d7
Update README.md
jialuhu Nov 6, 2019
d8fa877
Update README.md
jialuhu Nov 6, 2019
6f491ac
Update README.md
jialuhu Nov 6, 2019
f8ee1f5
Update README.md
jialuhu Nov 6, 2019
0edaa3b
fix some bugs by:<xiyoulinuxgroup>
ghorges Nov 9, 2019
e34f9dc
add zip
ghorges Nov 9, 2019
ff65386
add zip
ghorges Nov 9, 2019
973e9fb
picture
Feb 13, 2020
ff94b4e
Update README.md
ghorges Feb 13, 2020
8539bff
Update README.md
ghorges Feb 13, 2020
10b2315
Update README.md
jialuhu Feb 14, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2019 xiyoulinuxgroup

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
492 changes: 491 additions & 1 deletion README.md

Large diffs are not rendered by default.

Binary file added about_us.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added award.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions deepin_os_match2019
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
join in deepin_os_match2019
Binary file added doc/xFileContorl操作手册.pdf
Binary file not shown.
6 changes: 6 additions & 0 deletions etc/file.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#这个是连接的端口
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

json 不够好用,还是 yaml 不对胃口

port:8888
#这个是服务器的地址
addr:132.232.63.90
#这是监测的路径,路径请使用绝对路径
path:/home/ma/deepin-file-control/etc
Empty file added etc/unix
Empty file.
5 changes: 5 additions & 0 deletions load.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#!/bin/bash
PATHS=`pwd`
REA+=$PATHS
REA+="/test/myhook.so"
`echo $REA > /etc/ld.so.preload`
23 changes: 23 additions & 0 deletions make.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#!/bin/bash

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

set -e
以防脚本出错都不知道

PATHS=`pwd`
REA="#define FILE_PATH \""
REA+=$PATHS
REA+="/etc/file.conf"
REA+="\""
`echo $REA >> src/hook/myhook.h`
`echo $REA >> src/cli/fconf.h`

UNIXS="unix:"
UNIXS+=$PATHS
UNIXS+="/etc/unix"
`echo $UNIXS >> etc/file.conf`

g++ -std=c++11 -o ./src/cli/client ./src/cli/*.cpp -lpthread -ldl

cd src/hook
`chmod 777 build.sh`
`./build.sh`
cd ../..

`touch etc/unix`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

使用printf而不是手工拼接REA, UNIXS
bash 支持 block 的,可以这样写

(
cd src/server
chmod 777 build.sh
./build.sh
)

这样就不需要记得返回到上一层多少次了。

27 changes: 27 additions & 0 deletions ser-src/Buffer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include <iostream>
#include <sys/uio.h>
#include "Buffer.h"

// 将fd中的数据读到Buffer中
int Buffer::readFd(int fd)
{
char extrabuf[65536];
struct iovec iov[2];
ssize_t writen = writeable();
ssize_t n;

iov[0].iov_base = begin() + _writeindex;
iov[0].iov_len = writen;
iov[1].iov_base = extrabuf;
iov[1].iov_len = sizeof(extrabuf);

if ((n = readv(fd, iov, 2)) > 0) {
if (n <= writen)
_writeindex += n;
else {
_writeindex += writen;
append(extrabuf, n - writen);
}
}
return n;
}
78 changes: 78 additions & 0 deletions ser-src/Buffer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#ifndef _ANGEL_BUFFER_H
#define _ANGEL_BUFFER_H

#include <vector>
#include <algorithm>

class Buffer {
public:
Buffer() : _buf(INIT_SIZE) { }
~Buffer() { }
static const size_t INIT_SIZE = 1024;
char *begin() { return &*_buf.begin(); }
char *peek() { return begin() + _readindex; }
size_t prependable() const { return _readindex; }
size_t readable() const { return _writeindex - _readindex; }
size_t writeable() const { return _buf.size() - _writeindex; }
void append(const char *data, size_t len)
{
makeSpace(len);
std::copy(data, data + len, _buf.begin() + _writeindex);
_writeindex += len;
}
// 内部腾挪
void makeSpace(size_t len)
{
if (len > writeable()) {
if (len <= writeable() + prependable()) {
size_t readBytes = readable();
std::copy(peek(), peek() + readBytes, begin());
_readindex = 0;
_writeindex = _readindex + readBytes;
} else
_buf.resize(_writeindex + len);
}
}
// 返回C风格字符串
const char *c_str()
{
makeSpace(1);
_buf[_writeindex] = '\0';
return peek();
}
int findStr(char *s, const char *p, size_t plen)
{
const char *pattern = std::search(s, begin() + _writeindex, p, p + plen);
return pattern == begin() + _writeindex ? -1 : pattern - s;
}
int findStr(const char *s, size_t len)
{
return findStr(peek(), s, len);
}
// 返回\r\n在Buffer中第一次出现的位置,没出现返回-1
int findCrlf() { return findStr("\r\n", 2); }
int findLf() { return findStr("\n", 1); }
// 跳过已读的数据
void retrieve(size_t len)
{
if (len < readable())
_readindex += len;
else
_readindex = _writeindex = 0;
}
void retrieveAll() { retrieve(readable()); }
int readFd(int fd);
void swap(Buffer& _buffer)
{
_buf.swap(_buffer._buf);
std::swap(_readindex, _buffer._readindex);
std::swap(_writeindex, _buffer._writeindex);
}
char& operator[](size_t idx) { return _buf[idx]; }
private:
std::vector<char> _buf;
size_t _readindex = 0;
size_t _writeindex = 0;
};

#endif // _ANGEL_BUFFER_H
141 changes: 141 additions & 0 deletions ser-src/Channel.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
#include <iostream>
#include <unistd.h>
#include <sys/poll.h>
#include <errno.h>
#include <functional>
#include <cstring>
#include "EventLoop.h"
#include "Channel.h"
#include "Buffer.h"
#include "Logger.h"

void Channel::changeEvent(void)
{
int evs = events();
_loop->changeEvent(fd(), events());
logDebug("fd(%d)'s events %s is changed to %s", eventStr(evs),
eventStr(events()));
}

void Channel::send(const char *s, size_t len)
{
setStatus(Channel::SENDING);
logDebug("sending to fd(%d)'s message len is %d", fd(), len);
if (!isWriting() && _output.readable() == 0) {
ssize_t n = write(fd(), s, len);
logDebug("write %zu bytes to fd(%d)", n, fd());
if (n > 0) {
if (n < len) {
logDebug("the left %zd bytes is append to fd(%d)'s output buffer", n, fd());
_output.append(s + n, len - n);
enableWrite();
} else {
logDebug("the whole message is sent");
clearStatus(Channel::SENDING);
if (_writeCompleteCb)
_writeCompleteCb();
}
} else {
if (errno != EAGAIN
&& errno != EWOULDBLOCK
&& errno != EINTR) {
logWarn("%s", strerror(errno));
handleClose();
}
}
} else {
logDebug("the whole message %zd bytes is append to fd(%d)'s output buffer",
len, fd());
_output.append(s, len);
}
}

void Channel::handleEvent(void)
{
logDebug("fd(%d)'s revents is %s", fd(), eventStr(_revents));
// 写事件由loop自动负责
if (_revents & POLLOUT) {
handleWrite();
}
// 读事件由用户负责
if (_revents & POLLIN) {
if (_readCb) _readCb();
}
}

void Channel::handleAccept(void)
{
int connfd = socket().accept();
Channel *chl = new Channel(_loop);
chl->socket().setFd(connfd);
chl->setReadCb(std::bind(&Channel::handleRead, chl));
chl->setMessageCb(_messageCb);
_loop->addChannel(chl);
logDebug("fd(%d) is connected", connfd);
// _connectionCb() for Client
}

void Channel::handleRead(void)
{
ssize_t n = _input.readFd(fd());
logDebug("read %zu bytes from fd(%d)", n, fd());
if (n > 0) {
if (_messageCb)
_messageCb(shared_from_this(), _input);
} else if (n == 0) {
handleClose();
} else
handleError();
}

void Channel::handleWrite(void)
{
if (isWriting()) {
ssize_t n = write(fd(), _output.peek(), _output.readable());
logDebug("write %zu bytes to fd(%d)", n, fd());
if (n >= 0) {
_output.retrieve(n);
if (_output.readable() == 0) {
clearStatus(Channel::SENDING);
disableWrite();
if (_writeCompleteCb)
_writeCompleteCb();
}
} else {
if (errno == EPIPE // 对端已关闭连接
|| (errno != EAGAIN
&& errno != EWOULDBLOCK
&& errno != EINTR)) {
logWarn("%s", strerror(errno));
handleClose();
}
}
}
}

void Channel::handleClose(void)
{
if (_status == Channel::SENDING) {
logDebug("the message is sending, close the connection later");
setWriteCompleteCb(std::bind(&EventLoop::delChannel, _loop, this));
} else
_loop->delChannel(this);
}

void Channel::handleError(void)
{
logWarn("fd(%d): %s", strerror(errno));
handleClose();
}

const char *Channel::eventStr(int events)
{
if (events == POLLIN)
return "POLLIN";
else if (events == POLLOUT)
return "POLLOUT";
else if (events == (POLLIN | POLLOUT))
return "POLLIN | POLLOUT";
else
return "NONE";
}
69 changes: 69 additions & 0 deletions ser-src/Channel.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
#ifndef _CHANNEL_H
#define _CHANNEL_H

#include <sys/poll.h>
#include <memory>
#include <functional>
#include "Buffer.h"
#include "Coder.h"
#include "Socket.h"
#include "Noncopyable.h"

class EventLoop;

// 不可拷贝的
class Channel : Noncopyable,
public std::enable_shared_from_this<Channel> {
public:
typedef std::function<void(std::shared_ptr<Channel>, Buffer&)> MessageCallback;
typedef std::function<void()> ReadCallback;
typedef std::function<void()> WriteCompleteCallback;
enum {
SENDING = 001, // 正在发送消息
};
explicit Channel(EventLoop *loop) : _loop(loop) { }
~Channel() { }
int fd() { return _socket.fd(); }
Socket& socket() { return _socket; }
int events() { return _events; }
void setRevents(int revents) { _revents = revents; }
int isReading() { return _events & POLLIN; }
int isWriting() { return _events & POLLOUT; }
void enableRead() { _events |= POLLIN; }
void enableWrite() { _events |= POLLOUT; changeEvent(); }
void disableWrite() { _events &= ~POLLOUT; changeEvent(); }
void changeEvent();
void send(const char *s, size_t len);
Request& req() { return _req; }
void setStatus(int status_) { _status |= status_; }
void clearStatus(int status_) { _status &= ~status_; }
static const char *eventStr(int events);
void setReadCb(const ReadCallback _cb)
{ _readCb = _cb; }
void setMessageCb(const MessageCallback _cb)
{ _messageCb = _cb; }
void setWriteCompleteCb(const WriteCompleteCallback _cb)
{ _writeCompleteCb = _cb; }
void handleEvent();
void handleRead();
void handleWrite();
void handleClose();
void handleError();
void handleAccept();
private:
EventLoop *_loop;
Socket _socket;
int _events = 0;
int _revents = 0;
int _status = 0;
Buffer _input;
Buffer _output;
// better to use boost::any
Request _req;
ReadCallback _readCb = nullptr;
MessageCallback _messageCb = nullptr;
// 通常用于保证在写完消息后再关闭连接
WriteCompleteCallback _writeCompleteCb = nullptr;
};

#endif // _CHANNEL_H
Loading