-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy pathpcs_file.c
215 lines (187 loc) · 4.5 KB
/
pcs_file.c
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
/*
* Copyright (c) 2013 [email protected]
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <dirent.h>
#include "pcs.h"
PCSFileBlock *PCSFileBlock_New() {
//{{{
PCSFileBlock *block = calloc(sizeof(PCSFileBlock), 1);
return block;
}
//}}}
void PCSFileBlock_Free(PCSFileBlock *block) {
//{{{
free(block);
}
//}}}
PCSFile *PCSFile_New() {
//{{{
PCSFile *file = calloc(sizeof(PCSFile), 1);
return file;
}
//}}}
void PCSFile_Free(PCSFile *file) {
//{{{
PCSFileBlock *old = NULL;
PCSFileBlock *block = NULL;
if (file == NULL) return;
if (file->path != NULL) {
free(file->path);
}
if (file->block != NULL) {
block = file->block;
while (block) {
old = block;
block = block->next;
PCSFileBlock_Free(old);
}
}
free(file);
}
//}}}
void PCSFile_SetPath(PCSFile *file, const char *path) {
//{{{
char *new_path = strdup(path);
if (file->path) {
free(file->path);
}
file->path = new_path;
}
//}}}
PCSFileList *PCSFileList_New() {
//{{{
PCSFileList *list = calloc(sizeof(PCSFileList), 1);
return list;
}
//}}}
void PCSFileList_Free(PCSFileList *list) {
//{{{
if (list == NULL) return;
PCSFile *old;
PCSFile *file = list->first;
while (file) {
old = file;
file = file->next;
PCSFile_Free(old);
}
free(list);
}
//}}}
void PCSFileList_Prepend(PCSFileList *list, PCSFile *file) {
//{{{
file->next = list->first;
list->first = file;
}
//}}}
PCSFile *PCSFileList_Find(PCSFileList *list, const char *path) {
//{{{
PCSFile *file = list->first;
while (file != NULL) {
if (strcmp(file->path, path) == 0) {
return file;
}
file = file->next;
}
return NULL;
}
//}}}
PCSFile *PCSFileList_Shift(PCSFileList *list) {
//{{{
PCSFile *file = list->first;
if (file != NULL) {
list->first = file->next;
} else {
list->first = NULL;
}
return file;
}
//}}}
/* 构建本地文件对象 */
PCSFile *BaiduPCS_NewLocalFile(BaiduPCS *api, const char *path) {
//{{{
PCSFile *file = NULL;
FILE *fp = NULL;
BaiduPCS_ResetError(api);
//文件不存在
if (lstat(path, &(api->file_st)) == -1) {
BaiduPCS_ThrowError(api, "%s dons't exsit", path);
goto free;
}
//目录
if (S_ISDIR(api->file_st.st_mode)) {
file = PCSFile_New();
file->path = strdup(path);
file->is_dir = 1;
//链接
} else if (S_ISLNK(api->file_st.st_mode)) {
file = PCSFile_New();
file->path = strdup(path);
file->is_link = 1;
//普通文件
} else if (S_ISREG(api->file_st.st_mode)) {
file = PCSFile_New();
file->path = strdup(path);
file->size = api->file_st.st_size;
fp = fopen(path, "rb");
if (fp == NULL) {
BaiduPCS_ThrowError(api, "%s is not readable", path);
PCSFile_Free(file);
file = NULL;
goto free;
}
fclose(fp);
} else {
BaiduPCS_ThrowError(api, "%s unspported file type", path);
}
free:
return file;
}
//}}}
void PCSFile_Dump(PCSFile *file) {
//{{{
if (file == NULL) return;
PCSFileBlock *block = NULL;
int i = 0;
printf("路径:%s\n", file->path);
printf("MD5:%s\n", file->md5);
printf("大小:%lld\n", (unsigned long long)file->size);
printf("创建时间:%u\n", file->ctime);
printf("创建时间:%u\n", file->mtime);
printf("是否目录:%d\n", (int)file->is_dir);
printf("是否链接:%d\n", (int)file->is_link);
printf("切片大小:%lld\n", (unsigned long long)file->block_size);
if (file->block != NULL) {
block = file->block;
for (; block != NULL; i ++) {
printf("-块:%d\n", i);
printf("-偏移:%lld\n", (unsigned long long)block->offset);
printf("-尺寸:%lld\n", (unsigned long long)block->size);
block = block->next;
}
}
}
//}}}
void PCSFileList_Dump(PCSFileList *list) {
//{{{
PCSFile *file = list->first;
while (file != NULL) {
printf("------------------------------\n");
PCSFile_Dump(file);
file = file->next;
}
}
//}}}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* End:
* vim600: et sw=4 ts=4 fdm=marker
* vim<600: et sw=4 ts=4
*/