1 /**
2 * Copyright (C) 2008 Happy Fish / YuQing
3 *
4 * FastDFS may be copied only under the terms of the GNU General
5 * Public License V3, which may be found in the FastDFS source kit.
6 * Please visit the FastDFS Home Page http://www.fastken.com/ for more detail.
7 **/
8 
9 #ifndef STORAGE_CLIENT_H
10 #define STORAGE_CLIENT_H
11 
12 #include "tracker_types.h"
13 #include "client_func.h"
14 
15 #define FDFS_DOWNLOAD_TO_BUFF   	1
16 #define FDFS_DOWNLOAD_TO_FILE   	2
17 #define FDFS_DOWNLOAD_TO_CALLBACK   	3
18 
19 #define FDFS_UPLOAD_BY_BUFF   	1
20 #define FDFS_UPLOAD_BY_FILE   	2
21 #define FDFS_UPLOAD_BY_CALLBACK 3
22 
23 #define FDFS_FILE_ID_SEPERATOR		'/'
24 #define FDFS_FILE_ID_SEPERATE_STR	"/"
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 #define storage_upload_by_filename(pTrackerServer, \
31 		pStorageServer, store_path_index, local_filename, \
32 		file_ext_name, meta_list, meta_count, group_name, \
33 		remote_filename) \
34 	storage_upload_by_filename_ex(pTrackerServer, \
35 		pStorageServer, store_path_index, \
36 		STORAGE_PROTO_CMD_UPLOAD_FILE, local_filename, \
37 		file_ext_name, meta_list, meta_count, group_name, \
38 		remote_filename)
39 
40 #define storage_upload_appender_by_filename(pTrackerServer, \
41 		pStorageServer, store_path_index, local_filename, \
42 		file_ext_name, meta_list, meta_count, group_name, \
43 		remote_filename) \
44 	storage_upload_by_filename_ex(pTrackerServer, \
45 		pStorageServer, store_path_index, \
46 		STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, local_filename, \
47 		file_ext_name, meta_list, meta_count, group_name, \
48 		remote_filename)
49 
50 /**
51 * upload file to storage server (by file name)
52 * params:
53 *       pTrackerServer: tracker server
54 *       pStorageServer: storage server
55 *       store_path_index: the index of path on the storage server
56 *       local_filename: local filename to upload
57 *       cmd: the protocol command
58 *       file_ext_name: file ext name, not include dot(.),
59 *                      if be NULL will abstract ext name from the local filename
60 *	meta_list: meta info array
61 *       meta_count: meta item count
62 *	group_name: if not empty, specify the group name.
63 	 	    return the group name to store the file
64 *	remote_filename: return the new created filename
65 * return: 0 success, !=0 fail, return the error code
66 **/
67 int storage_upload_by_filename_ex(ConnectionInfo *pTrackerServer, \
68 		ConnectionInfo *pStorageServer, const int store_path_index, \
69 		const char cmd, const char *local_filename, \
70 		const char *file_ext_name, const FDFSMetaData *meta_list, \
71 		const int meta_count, char *group_name, char *remote_filename);
72 
73 /**
74 * upload file to storage server (by file buff)
75 * params:
76 *       pTrackerServer: tracker server
77 *       pStorageServer: storage server
78 *       store_path_index: the index of path on the storage server
79 *       file_buff: file content/buff
80 *       file_size: file size (bytes)
81 *       file_ext_name: file ext name, not include dot(.), can be NULL
82 *	meta_list: meta info array
83 *       meta_count: meta item count
84 *	group_name: if not empty, specify the group name.
85 		    return the group name to store the file
86 *	remote_filename: return the new created filename
87 * return: 0 success, !=0 fail, return the error code
88 **/
89 #define storage_upload_by_filebuff(pTrackerServer, pStorageServer, \
90 		store_path_index, file_buff, \
91 		file_size, file_ext_name, meta_list, meta_count, \
92 		group_name, remote_filename) \
93 	storage_do_upload_file(pTrackerServer, pStorageServer, \
94 		store_path_index, STORAGE_PROTO_CMD_UPLOAD_FILE, \
95 		FDFS_UPLOAD_BY_BUFF, file_buff, NULL, \
96 		file_size, NULL, NULL, file_ext_name, meta_list, meta_count, \
97 		group_name, remote_filename)
98 
99 #define storage_upload_appender_by_filebuff(pTrackerServer, pStorageServer, \
100 		store_path_index, file_buff, \
101 		file_size, file_ext_name, meta_list, meta_count, \
102 		group_name, remote_filename) \
103 	storage_do_upload_file(pTrackerServer, pStorageServer, \
104 		store_path_index, STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, \
105 		FDFS_UPLOAD_BY_BUFF, file_buff, NULL, \
106 		file_size, NULL, NULL, file_ext_name, meta_list, meta_count, \
107 		group_name, remote_filename)
108 
109 /**
110 * Upload file callback function prototype
111 * params:
112 *	arg: callback extra arguement
113 *       sock: connected storage socket for sending file content
114 * return: 0 success, !=0 fail, should return the error code
115 **/
116 typedef int (*UploadCallback) (void *arg, const int64_t file_size, int sock);
117 
118 /**
119 * upload file to storage server (by callback)
120 * params:
121 *       pTrackerServer: tracker server
122 *       pStorageServer: storage server
123 *       store_path_index: the index of path on the storage server
124 *       callback: callback function to send file content to storage server
125 *       arg: callback extra arguement
126 *       file_size: the file size
127 *       file_ext_name: file ext name, not include dot(.), can be NULL
128 *	meta_list: meta info array
129 *       meta_count: meta item count
130 *	group_name: if not empty, specify the group name.
131 	 	    return the group name to store the file
132 *	remote_filename: return the new created filename
133 * return: 0 success, !=0 fail, return the error code
134 **/
135 #define storage_upload_by_callback(pTrackerServer, pStorageServer, \
136 		store_path_index, callback, arg, file_size, file_ext_name, \
137 		meta_list, meta_count, group_name, remote_filename) \
138 	storage_upload_by_callback_ex(pTrackerServer, pStorageServer, \
139 		store_path_index, STORAGE_PROTO_CMD_UPLOAD_FILE, \
140 		callback, arg, file_size, file_ext_name, meta_list, \
141 		meta_count, group_name, remote_filename)
142 
143 #define storage_upload_appender_by_callback(pTrackerServer, pStorageServer, \
144 		store_path_index, callback, arg, file_size, file_ext_name, \
145 		meta_list, meta_count, group_name, remote_filename) \
146 	storage_upload_by_callback_ex(pTrackerServer, pStorageServer, \
147 		store_path_index, STORAGE_PROTO_CMD_UPLOAD_APPENDER_FILE, \
148 		callback, arg, file_size, file_ext_name, meta_list, \
149 		meta_count, group_name, remote_filename)
150 
151 int storage_upload_by_callback_ex(ConnectionInfo *pTrackerServer, \
152 		ConnectionInfo *pStorageServer, const int store_path_index, \
153 		const char cmd, UploadCallback callback, void *arg, \
154 		const int64_t file_size, const char *file_ext_name, \
155 		const FDFSMetaData *meta_list, const int meta_count, \
156 		char *group_name, char *remote_filename);
157 
158 int storage_do_upload_file(ConnectionInfo *pTrackerServer, \
159 	ConnectionInfo *pStorageServer, const int store_path_index, \
160 	const char cmd, const int upload_type, const char *file_buff, \
161 	void *arg, const int64_t file_size, const char *master_filename, \
162 	const char *prefix_name, const char *file_ext_name, \
163 	const FDFSMetaData *meta_list, const int meta_count, \
164 	char *group_name, char *remote_filename);
165 
166 /**
167 * delete file from storage server
168 * params:
169 *       pTrackerServer: tracker server
170 *       pStorageServer: storage server
171 *	group_name: the group name of storage server
172 *	filename: filename on storage server
173 * return: 0 success, !=0 fail, return the error code
174 **/
175 int storage_delete_file(ConnectionInfo *pTrackerServer, \
176 			ConnectionInfo *pStorageServer, \
177 			const char *group_name, const char *filename);
178 
179 /**
180 * download file from storage server
181 * params:
182 *       pTrackerServer: tracker server
183 *       pStorageServer: storage server
184 *	group_name: the group name of storage server
185 *	remote_filename: filename on storage server
186 *       file_buff: return file content/buff, must be freed
187 *       file_size: return file size (bytes)
188 * return: 0 success, !=0 fail, return the error code
189 **/
190 #define storage_download_file(pTrackerServer, pStorageServer, group_name, \
191 			remote_filename, file_buff, file_size)  \
192 	storage_do_download_file_ex(pTrackerServer, pStorageServer, \
193 			FDFS_DOWNLOAD_TO_BUFF, group_name, remote_filename, \
194 			0, 0, file_buff, NULL, file_size)
195 
196 #define storage_download_file_to_buff(pTrackerServer, pStorageServer, \
197 			group_name, remote_filename, file_buff, file_size)  \
198 	storage_do_download_file_ex(pTrackerServer, pStorageServer, \
199 			FDFS_DOWNLOAD_TO_BUFF, group_name, remote_filename, \
200 			0, 0, file_buff, NULL, file_size)
201 
202 #define storage_do_download_file(pTrackerServer, pStorageServer, \
203 		download_type, group_name, remote_filename, \
204 		file_buff, arg, file_size) \
205 	storage_do_download_file_ex(pTrackerServer, pStorageServer, \
206 		download_type, group_name, remote_filename, \
207 		0, 0, file_buff, arg, file_size);
208 
209 /**
210 * download file from storage server
211 * params:
212 *       pTrackerServer: tracker server
213 *       pStorageServer: storage server
214 *       download_type: FDFS_DOWNLOAD_TO_BUFF or FDFS_DOWNLOAD_TO_FILE
215 *                      or FDFS_DOWNLOAD_TO_CALLBACK
216 *	group_name: the group name of storage server
217 *	remote_filename: filename on storage server
218 *       file_offset: the start offset to download
219 *       download_bytes: download bytes, 0 means from start offset to the file end
220 *       file_buff: return file content/buff, must be freed
221 *       arg: additional argument for callback(valid only when download_tyee
222 *                       is FDFS_DOWNLOAD_TO_CALLBACK), can be NULL
223 *       file_size: return file size (bytes)
224 * return: 0 success, !=0 fail, return the error code
225 **/
226 int storage_do_download_file_ex(ConnectionInfo *pTrackerServer, \
227 		ConnectionInfo *pStorageServer, \
228 		const int download_type, \
229 		const char *group_name, const char *remote_filename, \
230 		const int64_t file_offset, const int64_t download_bytes, \
231 		char **file_buff, void *arg, int64_t *file_size);
232 
233 /**
234 * download file from storage server
235 * params:
236 *       pTrackerServer: tracker server
237 *       pStorageServer: storage server
238 *	group_name: the group name of storage server
239 *	remote_filename: filename on storage server
240 *	local_filename: local filename to write
241 *       file_size: return file size (bytes)
242 * return: 0 success, !=0 fail, return the error code
243 **/
244 int storage_download_file_to_file(ConnectionInfo *pTrackerServer, \
245 		ConnectionInfo *pStorageServer, \
246 		const char *group_name, const char *remote_filename, \
247 		const char *local_filename, int64_t *file_size);
248 
249 /**
250 * Download file callback function prototype
251 * params:
252 *	arg: callback extra arguement
253 *       file_size: file size
254 *       data: temp buff, should not keep persistently
255 *	current_size: current data size
256 * return: 0 success, !=0 fail, should return the error code
257 **/
258 typedef int (*DownloadCallback) (void *arg, const int64_t file_size, \
259 		const char *data, const int current_size);
260 
261 /**
262 * download file from storage server
263 * params:
264 *       pTrackerServer: tracker server
265 *       pStorageServer: storage server
266 *	group_name: the group name of storage server
267 *	remote_filename: filename on storage server
268 *       file_offset: the start offset to download
269 *       download_bytes: download bytes, 0 means from start offset to the file end
270 *	callback: callback function
271 *	arg: callback extra arguement
272 *       file_size: return file size (bytes)
273 * return: 0 success, !=0 fail, return the error code
274 **/
275 int storage_download_file_ex(ConnectionInfo *pTrackerServer, \
276 		ConnectionInfo *pStorageServer, \
277 		const char *group_name, const char *remote_filename, \
278 		const int64_t file_offset, const int64_t download_bytes, \
279 		DownloadCallback callback, void *arg, int64_t *file_size);
280 
281 /**
282 * set metadata items to storage server
283 * params:
284 *       pTrackerServer: tracker server
285 *       pStorageServer: storage server
286 *	group_name: the group name of storage server
287 *	filename: filename on storage server
288 *	meta_list: meta item array
289 *       meta_count: meta item count
290 *       op_flag:
291 *            # STORAGE_SET_METADATA_FLAG_OVERWRITE('O'): overwrite all old
292 *				metadata items
293 *            # STORAGE_SET_METADATA_FLAG_MERGE ('M'): merge, insert when
294 *				the metadata item not exist, otherwise update it
295 * return: 0 success, !=0 fail, return the error code
296 **/
297 int storage_set_metadata(ConnectionInfo *pTrackerServer, \
298 			ConnectionInfo *pStorageServer, \
299 			const char *group_name, const char *filename, \
300 			const FDFSMetaData *meta_list, const int meta_count, \
301 			const char op_flag);
302 
303 /**
304 * get all metadata items from storage server
305 * params:
306 *       pTrackerServer: tracker server
307 *       pStorageServer: storage server
308 *	group_name: the group name of storage server
309 *	filename: filename on storage server
310 *	meta_list: return meta info array, must be freed
311 *       meta_count: return meta item count
312 * return: 0 success, !=0 fail, return the error code
313 **/
314 int storage_get_metadata(ConnectionInfo *pTrackerServer, \
315 			ConnectionInfo *pStorageServer,  \
316 			const char *group_name, const char *filename, \
317 			FDFSMetaData **meta_list, \
318 			int *meta_count);
319 
320 /**
321 * upload slave file to storage server (by file name)
322 * params:
323 *       pTrackerServer: tracker server
324 *       pStorageServer: storage server
325 *       local_filename: local filename to upload
326 *       master_filename: the mater filename to generate the slave file id
327 *       prefix_name: the prefix name to generate the slave file id
328 *       file_ext_name: file ext name, not include dot(.),
329 *                      if be NULL will abstract ext name from the local filename
330 *	meta_list: meta info array
331 *       meta_count: meta item count
332 *	group_name: specify the group name.
333 	 	    return the group name to store the file
334 *	remote_filename: return the new created filename
335 * return: 0 success, !=0 fail, return the error code
336 **/
337 int storage_upload_slave_by_filename(ConnectionInfo *pTrackerServer, \
338 		ConnectionInfo *pStorageServer, const char *local_filename,\
339 		const char *master_filename, const char *prefix_name, \
340 		const char *file_ext_name, \
341 		const FDFSMetaData *meta_list, const int meta_count, \
342 		char *group_name, char *remote_filename);
343 
344 /**
345 * upload slave file to storage server (by file buff)
346 * params:
347 *       pTrackerServer: tracker server
348 *       pStorageServer: storage server
349 *       file_buff: file content/buff
350 *       file_size: file size (bytes)
351 *       master_filename: the mater filename to generate the slave file id
352 *       prefix_name: the prefix name to generate the slave file id
353 *       file_ext_name: file ext name, not include dot(.), can be NULL
354 *	meta_list: meta info array
355 *       meta_count: meta item count
356 *	group_name: specify the group name.
357 		    return the group name to store the file
358 *	remote_filename: return the new created filename
359 * return: 0 success, !=0 fail, return the error code
360 **/
361 int storage_upload_slave_by_filebuff(ConnectionInfo *pTrackerServer, \
362 		ConnectionInfo *pStorageServer, const char *file_buff, \
363 		const int64_t file_size, const char *master_filename, \
364 		const char *prefix_name, const char *file_ext_name, \
365 		const FDFSMetaData *meta_list, const int meta_count, \
366 		char *group_name, char *remote_filename);
367 
368 /**
369 * upload slave file to storage server (by callback)
370 * params:
371 *       pTrackerServer: tracker server
372 *       pStorageServer: storage server
373 *       callback: callback function to send file content to storage server
374 *       arg: callback extra arguement
375 *       file_size: the file size
376 *       master_filename: the mater filename to generate the slave file id
377 *       prefix_name: the prefix name to generate the slave file id
378 *       file_ext_name: file ext name, not include dot(.), can be NULL
379 *	meta_list: meta info array
380 *       meta_count: meta item count
381 *	group_name: specify the group name.
382 	 	    return the group name to store the file
383 *	remote_filename: return the new created filename
384 * return: 0 success, !=0 fail, return the error code
385 **/
386 int storage_upload_slave_by_callback(ConnectionInfo *pTrackerServer, \
387 		ConnectionInfo *pStorageServer, \
388 		UploadCallback callback, void *arg, \
389 		const int64_t file_size, const char *master_filename, \
390 		const char *prefix_name, const char *file_ext_name, \
391 		const FDFSMetaData *meta_list, const int meta_count, \
392 		char *group_name, char *remote_filename);
393 
394 
395 /**
396 * append file to storage server (by local filename)
397 * params:
398 *       pTrackerServer: tracker server
399 *       pStorageServer: storage server
400 *       local_filename: local filename to upload
401 *	group_name: the group name
402 *	appender_filename: the appender filename
403 * return: 0 success, !=0 fail, return the error code
404 **/
405 int storage_append_by_filename(ConnectionInfo *pTrackerServer, \
406 		ConnectionInfo *pStorageServer, const char *local_filename,\
407 		const char *group_name, const char *appender_filename);
408 
409 
410 /**
411 * append file to storage server (by callback)
412 * params:
413 *       pTrackerServer: tracker server
414 *       pStorageServer: storage server
415 *       callback: callback function to send file content to storage server
416 *       arg: callback extra arguement
417 *       file_size: the file size
418 *	group_name: the group name
419 *	appender_filename: the appender filename
420 * return: 0 success, !=0 fail, return the error code
421 **/
422 int storage_append_by_callback(ConnectionInfo *pTrackerServer, \
423 		ConnectionInfo *pStorageServer, \
424 		UploadCallback callback, void *arg, const int64_t file_size, \
425 		const char *group_name, const char *appender_filename);
426 
427 
428 /**
429 * append file to storage server (by file buff)
430 * params:
431 *       pTrackerServer: tracker server
432 *       pStorageServer: storage server
433 *       file_buff: file content/buff
434 *       file_size: file size (bytes)
435 *	group_name: the group name
436 *	appender_filename: the appender filename
437 * return: 0 success, !=0 fail, return the error code
438 **/
439 int storage_append_by_filebuff(ConnectionInfo *pTrackerServer, \
440 		ConnectionInfo *pStorageServer, const char *file_buff, \
441 		const int64_t file_size, const char *group_name, \
442 		const char *appender_filename);
443 
444 
445 /**
446 * modify file to storage server (by local filename)
447 * params:
448 *       pTrackerServer: tracker server
449 *       pStorageServer: storage server
450 *       local_filename: local filename to upload
451 *       file_offset: the start offset to modify appender file
452 *	group_name: the group name
453 *	appender_filename: the appender filename
454 * return: 0 success, !=0 fail, return the error code
455 **/
456 int storage_modify_by_filename(ConnectionInfo *pTrackerServer, \
457 		ConnectionInfo *pStorageServer, const char *local_filename,\
458 		const int64_t file_offset, const char *group_name, \
459 		const char *appender_filename);
460 
461 
462 /**
463 * modify file to storage server (by callback)
464 * params:
465 *       pTrackerServer: tracker server
466 *       pStorageServer: storage server
467 *       callback: callback function to send file content to storage server
468 *       arg: callback extra arguement
469 *       file_offset: the start offset to modify appender file
470 *       file_size: the file size
471 *	group_name: the group name
472 *	appender_filename: the appender filename
473 * return: 0 success, !=0 fail, return the error code
474 **/
475 int storage_modify_by_callback(ConnectionInfo *pTrackerServer, \
476 		ConnectionInfo *pStorageServer, \
477 		UploadCallback callback, void *arg, \
478 		const int64_t file_offset, const int64_t file_size, \
479 		const char *group_name, const char *appender_filename);
480 
481 
482 /**
483 * modify file to storage server (by file buff)
484 * params:
485 *       pTrackerServer: tracker server
486 *       pStorageServer: storage server
487 *       file_buff: file content/buff
488 *       file_offset: the start offset to modify appender file
489 *       file_size: file size (bytes)
490 *	group_name: the group name
491 *	appender_filename: the appender filename
492 * return: 0 success, !=0 fail, return the error code
493 **/
494 int storage_modify_by_filebuff(ConnectionInfo *pTrackerServer, \
495 		ConnectionInfo *pStorageServer, const char *file_buff, \
496 		const int64_t file_offset, const int64_t file_size, \
497 		const char *group_name, const char *appender_filename);
498 
499 
500 /**
501 * truncate file to sepecify size
502 * params:
503 *       pTrackerServer: tracker server
504 *       pStorageServer: storage server
505 *	group_name: the group name
506 *	appender_filename: the appender filename
507 *       truncated_file_size: truncated file size
508 * return: 0 success, !=0 fail, return the error code
509 **/
510 int storage_truncate_file(ConnectionInfo *pTrackerServer, \
511 		ConnectionInfo *pStorageServer,
512 		const char *group_name, const char *appender_filename, \
513 		const int64_t truncated_file_size);
514 
515 
516 #define storage_query_file_info(pTrackerServer, pStorageServer, \
517 		group_name, filename, pFileInfo) \
518 	storage_query_file_info_ex(pTrackerServer, pStorageServer, \
519 		group_name, filename, pFileInfo, false)
520 
521 /**
522 * query file info
523 * params:
524 *       pTrackerServer: tracker server
525 *       pStorageServer: storage server
526 *	group_name: the group name of storage server
527 *	filename: filename on storage server
528 *	pFileInfo: return the file info (file size and create timestamp)
529 *	bSilence: when this file not exist, do not log error on storage server
530 * return: 0 success, !=0 fail, return the error code
531 **/
532 int storage_query_file_info_ex(ConnectionInfo *pTrackerServer, \
533 			ConnectionInfo *pStorageServer,  \
534 			const char *group_name, const char *filename, \
535 			FDFSFileInfo *pFileInfo, const bool bSilence);
536 
537 
538 #define fdfs_get_file_info(group_name, remote_filename, pFileInfo) \
539 	fdfs_get_file_info_ex(group_name, remote_filename, true, pFileInfo)
540 
541 /**
542 * check if file exist
543 * params:
544 *       pTrackerServer: tracker server
545 *       pStorageServer: storage server
546 *	group_name: the group name of storage server
547 *	remote_filename: filename on storage server
548 * return: 0 file exist, !=0 not exist, return the error code
549 **/
550 int storage_file_exist(ConnectionInfo *pTrackerServer, \
551 			ConnectionInfo *pStorageServer,  \
552 			const char *group_name, const char *remote_filename);
553 /**
554 * get file info from the filename return by storage server
555 * params:
556 *	group_name: the group name of storage server
557 *	remote_filename: filename on storage server
558 *       get_from_server: if get slave file info from storage server
559 *       pFileInfo: return the file info
560 * return: 0 success, !=0 fail, return the error code
561 **/
562 int fdfs_get_file_info_ex(const char *group_name, const char *remote_filename, \
563 	const bool get_from_server, FDFSFileInfo *pFileInfo);
564 
565 
566 /**
567 * regenerate normal filename for appender file
568 * Note: the appender file will change to normal file
569 * params:
570 *       pTrackerServer: the tracker server
571 *       pStorageServer: the storage server
572 *	    group_name: the group name
573 *	    appender_filename: the appender filename
574 *       new_group_name: return the new group name
575 *       new_remote_filename: return the new filename
576 * return: 0 success, !=0 fail, return the error code
577 **/
578 int storage_regenerate_appender_filename(ConnectionInfo *pTrackerServer,
579 		ConnectionInfo *pStorageServer, const char *group_name,
580         const char *appender_filename, char *new_group_name,
581         char *new_remote_filename);
582 
583 #ifdef __cplusplus
584 }
585 #endif
586 
587 #endif
588 
589