Skip to content
This repository has been archived by the owner on Dec 16, 2020. It is now read-only.

Commit

Permalink
disk cache
Browse files Browse the repository at this point in the history
  • Loading branch information
GangZhuo committed Aug 24, 2017
1 parent ec4beb1 commit 56b3ffb
Show file tree
Hide file tree
Showing 9 changed files with 311 additions and 51 deletions.
4 changes: 3 additions & 1 deletion Makefile.am
Expand Up @@ -31,7 +31,8 @@ shell_src = shell.c \
rb_tree/red_black_tree.c \
rb_tree/stack.c \
utils.c \
hashtable.c
hashtable.c \
cache.c

baidupcs_SOURCES = \
$(shell_src) \
Expand Down Expand Up @@ -68,6 +69,7 @@ noinst_HEADERS = pcs/BaiduPCS.h \
arg.h \
dir.h \
hashtable.h \
cache.h \
rb_tree/misc.h \
rb_tree/red_black_tree.h \
rb_tree/stack.h
Expand Down
5 changes: 4 additions & 1 deletion Makefile.old
Expand Up @@ -24,7 +24,8 @@ SHELL_OBJS = bin/shell_arg.o \
bin/rb_tree_stack.o \
bin/red_black_tree.o \
bin/shell_utils.o \
bin/hashtable.o
bin/hashtable.o \
bin/cache.o

#CCFLAGS = -DHAVE_ASPRINTF -DHAVE_ICONV
ifeq ($(LC_OS_NAME), cygwin)
Expand Down Expand Up @@ -75,6 +76,8 @@ bin/rb_tree_stack.o: rb_tree/stack.c rb_tree/stack.h
$(CC) -o $@ -c $(PCS_CCFLAGS) rb_tree/stack.c
bin/red_black_tree.o: rb_tree/red_black_tree.c rb_tree/red_black_tree.h
$(CC) -o $@ -c $(PCS_CCFLAGS) rb_tree/red_black_tree.c
bin/cache.o: cache.c cache.h
$(CC) -o $@ -c $(PCS_CCFLAGS) cache.c

bin/cJSON.o: pcs/cJSON.c pcs/cJSON.h
$(CC) -o $@ -c $(PCS_CCFLAGS) pcs/cJSON.c
Expand Down
22 changes: 18 additions & 4 deletions README.md
Expand Up @@ -24,8 +24,7 @@ C/C++写的一个百度网盘工具,可以在linux终端中使用。
cd ..
sudo apt install ./baidupcs_*.deb


编译 (Debian):
编译 (Debian) (新方法):
===================================
程序依赖于 libcurl。

Expand All @@ -35,12 +34,27 @@ C/C++写的一个百度网盘工具,可以在linux终端中使用。
git clone https://github.com/GangZhuo/BaiduPCS.git
### 3. 编译源代码
cd BaiduPCS
make clean
make
./configure && make
make install #将安装到/usr/local/bin下
### 4. 手动安装到其他目录,例如 /usr/bin 下
cp ./baidupcs /usr/bin/

编译 (Debian):
===================================
程序依赖于 libcurl。

### 1. 安装依赖
apt-get install build-essential libcurl4-openssl-dev libssl-dev
### 2. 获取源代码
git clone https://github.com/GangZhuo/BaiduPCS.git
### 3. 编译源代码
cd BaiduPCS
make clean -f Makefile.old
make -f Makefile.old
make install -f Makefile.old #将安装到/usr/local/bin下
### 4. 手动安装到其他目录,例如 /usr/bin 下
cp ./bin/pcs /usr/bin/

编译 (Windows):
===================================
### 1. 获取源代码
Expand Down
115 changes: 115 additions & 0 deletions cache.c
@@ -0,0 +1,115 @@
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <inttypes.h>
#include <string.h>
#include <assert.h>
#ifdef WIN32
# include <WinSock2.h>
# include <Windows.h>
#else
# include <unistd.h>
#endif

#include "cache.h"
#include "pcs/pcs_mem.h"

#ifdef WIN32
#ifndef __MINGW32__
# define lseek _lseek
# define fileno _fileno
# define fseeko _fseeki64
# define ftello _ftelli64
#endif
#endif

int cache_init(cathe_t *cache)
{
memset(cache, 0, sizeof(cathe_t));
cache->blocks.prev = &cache->blocks;
cache->blocks.next = &cache->blocks;
return 0;
}

void cache_uninit(cathe_t *cache)
{
if (cache) {
block_t *block = cache->blocks.next, *tmp;
while (block != &cache->blocks) {
tmp = block;
block = block->next;
pcs_free(tmp->data);
pcs_free(tmp);
}
}
}

int cache_reset(cathe_t *cache)
{
cache_uninit(cache);
cache->blocks.prev = &cache->blocks;
cache->blocks.next = &cache->blocks;
cache->total_size = 0;
return 0;
}

block_t *cache_newblock(curl_off_t start, char *data, size_t size)
{
block_t *block;
block = pcs_malloc(sizeof(block_t));
if (!block)
return NULL;
memset(block, 0, sizeof(block_t));
block->start = start;
block->size = size;
block->data = pcs_malloc(size);
if (!block->data) {
free(block);
return NULL;
}
memcpy(block->data, data, size);
return block;
}

int cache_addblock(cathe_t *cache, block_t *block)
{
block_t *pos = cache->blocks.next;
cache->total_size += block->size;
/* find position */
while (pos != &cache->blocks) {
if (pos->start > block->start)
break;
pos = pos->next;
}
block->next = pos;
block->prev = pos->prev;
pos->prev->next = block;
pos->prev = block;
return 0;
}

int cache_add(cathe_t *cache, curl_off_t start, char *data, size_t size)
{
block_t *block, *pos = cache->blocks.next;
block = cache_newblock(start, data, size);
if (!block)
return -1;
return cache_addblock(cache, block);
}

int cache_flush(cathe_t *cache)
{
block_t *block = cache->blocks.next;
int r;
while (block != &cache->blocks) {
r = fseeko((cache->fp), block->start, SEEK_SET);
if (r)
return -1;
r = fwrite(block->data, 1, block->size, cache->fp);
if (r != block->size)
return -1;
block = block->next;
}
return 0;
}

40 changes: 40 additions & 0 deletions cache.h
@@ -0,0 +1,40 @@
#ifndef _PCS_SHELL_CAHCE_H
#define _PCS_SHELL_CAHCE_H

#include "pcs/pcs.h"

#define MAX_CACHE_SIZE (1 * 1024) /* 1MiB */

#ifdef __cplusplus
extern "C" {
#endif

typedef struct block {
curl_off_t start;
size_t size;
char *data;
struct block *prev;
struct block *next;
} block_t;

typedef struct cache {
block_t blocks;
FILE *fp;
size_t total_size;
} cathe_t;

int cache_init(cathe_t *cache);

void cache_uninit(cathe_t *cache);

int cache_add(cathe_t *cache, curl_off_t start, char *data, size_t size);

int cache_flush(cathe_t *cache);

int cache_reset(cathe_t *cache);

#ifdef __cplusplus
}
#endif

#endif
2 changes: 2 additions & 0 deletions pcs_shell.vcxproj
Expand Up @@ -102,6 +102,7 @@
</ItemDefinitionGroup>
<ItemGroup>
<ClCompile Include="arg.c" />
<ClCompile Include="cache.c" />
<ClCompile Include="dir.c" />
<ClCompile Include="hashtable.c" />
<ClCompile Include="pcs\pcs_buffer.c" />
Expand All @@ -124,6 +125,7 @@
</ItemGroup>
<ItemGroup>
<ClInclude Include="arg.h" />
<ClInclude Include="cache.h" />
<ClInclude Include="dir.h" />
<ClInclude Include="hashtable.h" />
<ClInclude Include="pcs\pcs_buffer.h" />
Expand Down
6 changes: 6 additions & 0 deletions pcs_shell.vcxproj.filters
Expand Up @@ -87,6 +87,9 @@
<ClCompile Include="pcs\utf8.c">
<Filter>Source Files\pcs</Filter>
</ClCompile>
<ClCompile Include="cache.c">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="pcs\cJSON.h">
Expand Down Expand Up @@ -161,6 +164,9 @@
<ClInclude Include="pcs\utf8.h">
<Filter>Header Files\pcs</Filter>
</ClInclude>
<ClInclude Include="cache.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
<ItemGroup>
<None Include="makefile" />
Expand Down

0 comments on commit 56b3ffb

Please sign in to comment.