summaryrefslogtreecommitdiff
path: root/src/fastdfs.c
blob: b5de29bf0daacbf020c0feefd2b722e0e806a198 (plain)
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
#include "fastdfs.h"


static ConnectionInfo *pTrackerServer;

int fastdfs_init(char  *fdfs_conf_filename,common_module_t *common)
{
	if (fdfs_client_init(fdfs_conf_filename) == 0)
	{
		pTrackerServer = tracker_get_connection();
		if (pTrackerServer == NULL)
		{
			fdfs_client_destroy();
			if(errno == 0)
			{
				printf("cannot build connection to tracker\n");
				return 0;
			}
			else
			{
				perror("cannot free pTrackerServer:");
				return -1;
			}
		}
		printf("tracker server is %s\n", pTrackerServer->ip_addr);
		return 0;
	}
	else
	{
		printf("init fail!\n");
		return -1;
	}

}

int upload_file_to_fdfs(char *file_id,char *fdfs_file_id)
{
	char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
	int result;
	int store_path_index;
	ConnectionInfo storageServer;
	
	log_init();
	g_log_context.log_level = LOG_ERR;
	ignore_signal_pipe();

	*group_name = '\0';
	
	if ((result=tracker_query_storage_store(pTrackerServer, \
	                &storageServer, group_name, &store_path_index)) != 0)
	{
		fdfs_client_destroy();
		fprintf(stderr, "tracker_query_storage fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		return -1;
	}

	result = storage_upload_by_filename1(pTrackerServer, \
			&storageServer, store_path_index, \
			file_id, NULL, \
			NULL, 0, group_name, fdfs_file_id);
	if (result == 0)
	{
		printf("%s\n", fdfs_file_id);
	}
	else
	{
		fprintf(stderr, "upload file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		result = -1;
	}

	//tracker_disconnect_server_ex(pTrackerServer, true);
	//fdfs_client_destroy();

	return result;

}
int download_file_from_fdfs(char *fdfs_file_id,char *local_filename)
{
	int result;
	int64_t file_size;
	int64_t file_offset;
	int64_t download_bytes;

	log_init();
	g_log_context.log_level = LOG_ERR;
	ignore_signal_pipe();

	file_offset = 0;
	download_bytes = 0;

/*
	local_filename = strrchr(fdfs_file_id, '/');
	if (local_filename != NULL)
	{
		local_filename++;  //skip /
	}
	else
	{
		local_filename = fdfs_file_id;
	}
*/

	result = storage_do_download_file1_ex(pTrackerServer, \
                NULL, FDFS_DOWNLOAD_TO_FILE, fdfs_file_id, \
                file_offset, download_bytes, \
                &local_filename, NULL, &file_size);
	if (result != 0)
	{
		printf("download file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		result = -1;
		return result;
	}

	//tracker_disconnect_server_ex(pTrackerServer, true);
	//fdfs_client_destroy();

	return 0;
}
int delete_file_on_fdfs(const char *fdfs_file_id)
{
	int result;

	log_init();
	g_log_context.log_level = LOG_ERR;
	ignore_signal_pipe();

	if ((result=storage_delete_file1(pTrackerServer, NULL, fdfs_file_id)) != 0)
	{
		printf("delete file fail, " \
			"error no: %d, error info: %s\n", \
			result, STRERROR(result));
		result = -1;
	}

	//tracker_disconnect_server_ex(pTrackerServer, true);
	//fdfs_client_destroy();

	return result;
}