-
Notifications
You must be signed in to change notification settings - Fork 2k
/
Copy pathdfs_func.c
143 lines (118 loc) · 3.33 KB
/
dfs_func.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include <fcntl.h>
#include "fastdfs/fdfs_global.h"
#include "dfs_func.h"
#include "fastdfs/fdfs_client.h"
int dfs_init(const int proccess_index, const char *conf_filename)
{
return fdfs_client_init(conf_filename);
}
void dfs_destroy()
{
fdfs_client_destroy();
}
static int downloadFileCallback(void *arg, const int64_t file_size, const char *data, \
const int current_size)
{
return 0;
}
int upload_file(const char *file_buff, const int file_size, char *file_id, char *storage_ip)
{
int result;
int store_path_index;
char group_name[FDFS_GROUP_NAME_MAX_LEN + 1];
ConnectionInfo *pTrackerServer;
ConnectionInfo *pStorageServer;
ConnectionInfo storageServer;
pTrackerServer = tracker_get_connection();
if (pTrackerServer == NULL)
{
return errno != 0 ? errno : ECONNREFUSED;
}
*group_name = '\0';
if ((result=tracker_query_storage_store(pTrackerServer, &storageServer,
group_name, &store_path_index)) != 0)
{
tracker_close_connection_ex(pTrackerServer, true);
return result;
}
if ((pStorageServer=tracker_make_connection(&storageServer, &result)) \
== NULL)
{
tracker_close_connection(pTrackerServer);
return result;
}
strcpy(storage_ip, storageServer.ip_addr);
result = storage_upload_by_filebuff1(pTrackerServer, pStorageServer,
store_path_index, file_buff, file_size, NULL, NULL, 0, "", file_id);
tracker_close_connection(pTrackerServer);
tracker_close_connection(pStorageServer);
return result;
}
int download_file(const char *file_id, int *file_size, char *storage_ip)
{
int result;
ConnectionInfo *pTrackerServer;
ConnectionInfo *pStorageServer;
ConnectionInfo storageServer;
int64_t file_bytes;
pTrackerServer = tracker_get_connection();
if (pTrackerServer == NULL)
{
return errno != 0 ? errno : ECONNREFUSED;
}
if ((result=tracker_query_storage_fetch1(pTrackerServer, \
&storageServer, file_id)) != 0)
{
tracker_close_connection_ex(pTrackerServer, true);
return result;
}
if ((pStorageServer=tracker_make_connection(&storageServer, &result)) \
== NULL)
{
tracker_close_connection(pTrackerServer);
return result;
}
strcpy(storage_ip, storageServer.ip_addr);
result = storage_download_file_ex1(pTrackerServer, pStorageServer, \
file_id, 0, 0, downloadFileCallback, NULL, &file_bytes);
*file_size = file_bytes;
tracker_close_connection(pTrackerServer);
tracker_close_connection(pStorageServer);
return result;
}
int delete_file(const char *file_id, char *storage_ip)
{
int result;
ConnectionInfo *pTrackerServer;
ConnectionInfo *pStorageServer;
ConnectionInfo storageServer;
pTrackerServer = tracker_get_connection();
if (pTrackerServer == NULL)
{
return errno != 0 ? errno : ECONNREFUSED;
}
if ((result=tracker_query_storage_update1(pTrackerServer, \
&storageServer, file_id)) != 0)
{
tracker_close_connection_ex(pTrackerServer, true);
return result;
}
if ((pStorageServer=tracker_make_connection(&storageServer, &result)) \
== NULL)
{
tracker_close_connection(pTrackerServer);
return result;
}
strcpy(storage_ip, storageServer.ip_addr);
result = storage_delete_file1(pTrackerServer, pStorageServer, file_id);
tracker_close_connection(pTrackerServer);
tracker_close_connection(pStorageServer);
return result;
}