Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
View | Details | Raw Unified | Return to bug 196435 | Differences between
and this patch

Collapse All | Expand All

(-)src-native-new/src/transport/TPTPClientCompTL/TPTPClientCompTL.dsp (+8 lines)
Lines 118-123 Link Here
118
# End Source File
118
# End Source File
119
# Begin Source File
119
# Begin Source File
120
120
121
SOURCE=.\nativeFileServer.c
122
# End Source File
123
# Begin Source File
124
121
SOURCE=.\RACClientSupport.c
125
SOURCE=.\RACClientSupport.c
122
# End Source File
126
# End Source File
123
# Begin Source File
127
# Begin Source File
Lines 166-171 Link Here
166
# End Source File
170
# End Source File
167
# Begin Source File
171
# Begin Source File
168
172
173
SOURCE=.\nativeFileServer.h
174
# End Source File
175
# Begin Source File
176
169
SOURCE=.\RACClientSupport.h
177
SOURCE=.\RACClientSupport.h
170
# End Source File
178
# End Source File
171
# Begin Source File
179
# Begin Source File
(-)src-native-new/src/transport/TPTPClientCompTL/Connect2AC.c (-1 / +1 lines)
Lines 1012-1018 Link Here
1012
			break;
1012
			break;
1013
		}
1013
		}
1014
		if (i == 0) {
1014
		if (i == 0) {
1015
			if ((strlen(name) + strlen(value) + 100) > variableListSize)
1015
			if ((tptp_int32)(strlen(name) + strlen(value) + 100) > variableListSize)
1016
			{
1016
			{
1017
				variableListSize = (strlen(name) + strlen(value) + 100) * 2;
1017
				variableListSize = (strlen(name) + strlen(value) + 100) * 2;
1018
			}
1018
			}
(-)src-native-new/src/transport/TPTPClientCompTL/ClientCTL.c (-24 / +24 lines)
Lines 17-22 Link Here
17
	#include <sys/socket.h>
17
	#include <sys/socket.h>
18
#endif
18
#endif
19
19
20
#include <string.h>
20
#include "tptp/TPTPTypes.h"
21
#include "tptp/TPTPTypes.h"
21
#include "tptp/TPTPSupportTypes.h"
22
#include "tptp/TPTPSupportTypes.h"
22
#include "tptp/TransportSupport.h"
23
#include "tptp/TransportSupport.h"
Lines 39-44 Link Here
39
extern void *processPollingThread(void *args);
40
extern void *processPollingThread(void *args);
40
#endif
41
#endif
41
42
43
THREAD_USER_FUNC_RET_TYPE startNativeFileServer(LPVOID args);
44
extern int stopNativeFileServer();
45
42
/**
46
/**
43
 *********************************************************
47
 *********************************************************
44
 *
48
 *
Lines 201-211 Link Here
201
			cctlData->isPolling = TRUE;
205
			cctlData->isPolling = TRUE;
202
		}
206
		}
203
207
204
		
205
		/* Assume NO Security for now !!! */
208
		/* Assume NO Security for now !!! */
206
		/******************************/
209
		/******************************/
207
		/* Create JVM */
210
		/* Create JVM */
208
		if (!cctlData->jvm)
211
		if (!cctlData->securityEnabled) {
212
			jvmCreated = FALSE;		// skip jvm
213
		}
214
		else if (!cctlData->jvm)
209
		{
215
		{
210
			TPTP_LOG_WARNING_MSG( stateData, "Cannot create JVM and therefore cannot create file server" );
216
			TPTP_LOG_WARNING_MSG( stateData, "Cannot create JVM and therefore cannot create file server" );
211
			jvmCreated = FALSE;
217
			jvmCreated = FALSE;
Lines 293-299 Link Here
293
				startFileServer(stateData, &_fileServerTID);
299
				startFileServer(stateData, &_fileServerTID);
294
			}
300
			}
295
		}
301
		}
296
		else {
302
		else if (cctlData->securityEnabled) {
297
			//for linux we have already warned about not having
303
			//for linux we have already warned about not having
298
			//File transfer.
304
			//File transfer.
299
			TPTP_LOG_ERROR_MSG( stateData, "Cannot create JVM" );
305
			TPTP_LOG_ERROR_MSG( stateData, "Cannot create JVM" );
Lines 552-557 Link Here
552
	struct sockaddr_in  myServerSockAddr ;
558
	struct sockaddr_in  myServerSockAddr ;
553
	tl_state_data_t*    stateData;
559
	tl_state_data_t*    stateData;
554
	cctl_state_data_t*  cctlData;
560
	cctl_state_data_t*  cctlData;
561
	TID                 threadId;
562
	HANDLE              threadHandle;
555
563
556
	if ( !isValidTPTPBlock(tlo, TPTP_BASETL_MAKE_OBJECT_ID(tlo)) )
564
	if ( !isValidTPTPBlock(tlo, TPTP_BASETL_MAKE_OBJECT_ID(tlo)) )
557
		return TPTP_ERROR_INVALID_PARAMETER;
565
		return TPTP_ERROR_INVALID_PARAMETER;
Lines 586-591 Link Here
586
		return -1; // TODO: Use a more specific error
594
		return -1; // TODO: Use a more specific error
587
	}
595
	}
588
596
597
598
	if (!cctlData->securityEnabled) {
599
		rc = tptpStartThread(startNativeFileServer, (LPVOID)stateData, &threadId, &threadHandle);
600
601
		if (rc == 0) {
602
			TPTP_LOG_INFO_MSG1(stateData, "Native file server started listening on port %d", cctlData->filePort);
603
		}
604
		else {
605
			TPTP_LOG_ERROR_MSG(stateData, "Native file server could not start");
606
		}
607
	}			
608
589
	return baseTL_startTransportListener( tlo, serveRequests );
609
	return baseTL_startTransportListener( tlo, serveRequests );
590
}
610
}
591
611
Lines 835-861 Link Here
835
	return 0;
855
	return 0;
836
}
856
}
837
857
838
#ifdef __linux__
839
int closeSocket (int socket) {
840
	struct linger linger;
841
	int rc;
842
843
	linger.l_onoff = 1;
844
	linger.l_linger = 0;
845
846
	rc = setsockopt(socket, SOL_SOCKET, SO_LINGER, (const char*) &linger, 
847
				sizeof(linger));
848
849
	close (socket);
850
	
851
	return rc;
852
}
853
#else
854
int closeSocket (int socket) {
855
	return closeThisSocket(socket);
856
}
857
#endif
858
859
int closeConnections (tptp_object* tlo) {
858
int closeConnections (tptp_object* tlo) {
860
	tl_control_connection_t* con;
859
	tl_control_connection_t* con;
861
	client_connection_block_t* client;
860
	client_connection_block_t* client;
Lines 905-910 Link Here
905
tptp_int32 stopTransportListener(tptp_object* tlo)
904
tptp_int32 stopTransportListener(tptp_object* tlo)
906
{
905
{
907
	closeConnections(tlo);
906
	closeConnections(tlo);
907
	stopNativeFileServer();
908
	
908
	
909
	return baseTL_stopTransportListener( tlo );
909
	return baseTL_stopTransportListener( tlo );
910
}
910
}
(-)src-native-new/src/transport/transportSupport/TransportSupport.def (+2 lines)
Lines 52-55 Link Here
52
	ipcCloseMem                    	  @49
52
	ipcCloseMem                    	  @49
53
	isMemOpen						  @50
53
	isMemOpen						  @50
54
	ipcMemDetach					  @51
54
	ipcMemDetach					  @51
55
	closeSocket					  @52
56
55
	
57
	
(-)src-native-new/src/transport/transportSupport/TransportSupportSocket.c (+19 lines)
Lines 470-474 Link Here
470
	return ( send(sock, buffer, byteCount, 0) ) ;
470
	return ( send(sock, buffer, byteCount, 0) ) ;
471
}
471
}
472
472
473
#ifdef __linux__
474
int closeSocket (int socket) {
475
	struct linger linger;
476
	int rc;
473
477
478
	linger.l_onoff = 1;
479
	linger.l_linger = 0;
480
481
	rc = setsockopt(socket, SOL_SOCKET, SO_LINGER, (const char*) &linger, 
482
				sizeof(linger));
483
484
	close (socket);
485
	
486
	return rc;
487
}
488
#else
489
int closeSocket (int socket) {
490
	return closeThisSocket(socket);
491
}
492
#endif
474
493
(-)src-native-new/src/shared/TPTPUtil/TPTUtil.def (-1 / +4 lines)
Lines 99-102 Link Here
99
	terminateXMLPlatformUtils
99
	terminateXMLPlatformUtils
100
	parseHostList
100
	parseHostList
101
	getExecutableName
101
	getExecutableName
102
	readStringFromBuffer
102
	readStringFromBuffer
103
	validateDirectory
104
	isAbsolutePath
105
	getTempDir
(-)src-native-new/src/shared/TPTPUtil/TPTPUtil.cpp (+39 lines)
Lines 845-850 Link Here
845
	return statBuf.st_size;
845
	return statBuf.st_size;
846
}
846
}
847
847
848
int validateDirectory(char* dirName) {
849
	struct STAT statBuf;
850
851
	if (STAT(dirName, &statBuf) != 0) {
852
		return 0;
853
	}
854
	
855
	return (S_ISDIR(statBuf.st_mode)) ? 1 : 0;
856
}
857
858
int isAbsolutePath(const char* path) {
859
	int n;
860
	
861
	if (path == NULL) return 0;
862
	
863
	n = strlen(path);
864
	if (n == 0) return 0;
865
	
866
#ifdef _WIN32
867
	if(n >= 3 && isalpha(*path) && *(path+1)==':' && *(path+2)=='\\') return 1;
868
	return (*path == '\\') ? 1 : 0;
869
#else
870
	return (*path == '/') ? 1 : 0;
871
#endif	
872
}
873
874
char* getTempDir() {
875
	char* dir;
876
	
877
	dir = getenv("TEMP");
878
	if(dir != NULL) return dir;
879
	
880
#ifdef _WIN32
881
	return "C:\\tmp";
882
#else
883
	return "/tmp";
884
#endif
885
}
886
848
int getConfigElementName( const char* cmdStr, char** elemName)
887
int getConfigElementName( const char* cmdStr, char** elemName)
849
{
888
{
850
	int i=0;
889
	int i=0;
(-)src-native-new/include/tptp/TransportSupportSocket.h (-2 / +1 lines)
Lines 196-203 Link Here
196
196
197
int getPeerName(SOCKET sock, unsigned char *pAddr);
197
int getPeerName(SOCKET sock, unsigned char *pAddr);
198
198
199
199
int closeSocket (int socket);
200
201
200
202
#ifdef __cplusplus
201
#ifdef __cplusplus
203
}
202
}
(-)src-native-new/include/tptp/TPTPUtils.h (+3 lines)
Lines 234-239 Link Here
234
int tptp_postSemaphore(Semaphore_t *pSem);
234
int tptp_postSemaphore(Semaphore_t *pSem);
235
235
236
int getFileSize(char *fileName);
236
int getFileSize(char *fileName);
237
int validateDirectory(char* dirName);
238
int isAbsolutePath(const char* path);
239
char* getTempDir();
237
240
238
/* String utilities */
241
/* String utilities */
239
void tptp_strcopy(tptp_string* destination, const tptp_string* source);
242
void tptp_strcopy(tptp_string* destination, const tptp_string* source);
(-)src-native-new/src/transport/TPTPClientCompTL/nativeFileServer.h (+87 lines)
Added Link Here
1
#ifndef _NATIVE_FILE_SERVER
2
#define _NATIVE_FILE_SERVER
3
4
#include <string.h>
5
6
#ifdef _WIN32
7
	#include <direct.h>
8
#else
9
	#include <sys/stat.h>
10
	#include <sys/types.h>
11
#endif
12
13
#include "tptp/TPTPTypes.h"
14
#include "tptp/TPTPSupportTypes.h"
15
#include "tptp/TransportSupport.h"
16
#include "tptp/TPTPBaseTL.h" 
17
#include "tptp/compatibility/RAComm.h" 
18
#include "ClientCTL.h"
19
20
#ifdef _WIN32
21
	#define  pathSeparatorCharacter '\\'
22
	#define  pathSeparator "\\"
23
#else
24
	#define  pathSeparatorCharacter '/'
25
	#define  pathSeparator "/"
26
#endif			
27
28
typedef struct {
29
	SOCKET      socket;
30
	char*		buffer;
31
	int 		bufferLength;
32
	tl_state_data_t* stateData;
33
} fs_connection_block_t;
34
35
#define DETERMINE_SERVER_REACH_COMMAND 1
36
#define DETERMINE_SERVER_REACH_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.DetermineServerReachCommand"
37
38
#define GET_FILE_COMMAND 2
39
#define GET_FILE_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.GetFileCommand"
40
41
#define PUT_FILE_COMMAND 3
42
#define PUT_FILE_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.PutFileCommand"
43
44
#define DELETE_FILE_COMMAND 4
45
#define DELETE_FILE_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.DeleteFileCommand"
46
47
#define DELETE_DIR_COMMAND 5
48
#define DELETE_DIR_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.DeleteDirectoryCommand"
49
50
#define VALIDATE_DIR_COMMAND 6
51
#define VALIDATE_DIR_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.ValidateDirectoryExistenceCommand"
52
53
#define QUERY_SERVER_STATUS_COMMAND 7
54
#define QUERY_SERVER_STATUS_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.QueryServerStatusCommand"
55
56
#define MDFY_PERMISSION_COMMAND 8
57
#define MDFY_PERMISSION_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.ModifyPermissionCommand"
58
59
#define LIST_CONTENT_COMMAND 9
60
#define LIST_CONTENT_COMMAND_STR "org.eclipse.hyades.internal.execution.core.file.dynamic.ListContentCommandCommand"
61
62
#define OPEN_CONTROL_DIALOG "org.eclipse.hyades.execution.core.loader.ScopedChannelClassLoader$AbstractControlDirective$BeginControlDirective"
63
#define CLOSE_CONTROL_DIALOG "org.eclipse.hyades.execution.core.loader.ScopedChannelClassLoader$AbstractControlDirective$EndControlDirective"
64
65
#define FS_TRUE		127		// java.lang.Boolean.MAX_VALUE
66
#define	FS_FALSE	-128	// java.lang.Boolean.MIN_VALUE
67
68
extern int readInt (SOCKET socket, int* value);
69
extern char* readString(SOCKET socket);
70
71
extern int writeByte(SOCKET socket, int byte);
72
extern int writeBoolean(SOCKET socket, int d);
73
extern int writeInt (SOCKET socket, int v);
74
extern int writeString(SOCKET socket, char* buffer);
75
extern int writeStrings(SOCKET socket, char** array, int len);
76
77
extern char* readStringIntoBuffer(fs_connection_block_t* con);
78
extern int readFile(fs_connection_block_t* con, char* fileName);
79
80
extern int writeFile(fs_connection_block_t* con, char* fileName);
81
82
extern int openControlDialog(fs_connection_block_t* con);
83
extern int closeControlDialog(fs_connection_block_t* con);
84
85
extern THREAD_USER_FUNC_RET_TYPE startNativeFileServer(LPVOID args);
86
87
#endif
(-)src-native-new/src/transport/TPTPClientCompTL/nativeFileServer.c (+538 lines)
Added Link Here
1
#include "nativeFileServer.h"
2
3
int readInt (SOCKET socket, int* value) {
4
	int bytesRead, bytesToRead, i, d, r;
5
	unsigned char* p;
6
	char buf[5];	// readFromSocket() appends terminating 0
7
8
	bytesToRead = 4;
9
	while(bytesToRead > 0) {
10
		if (readFromSocket(socket, buf+4-bytesToRead, bytesToRead, &bytesRead) <= 0) {
11
			return -1;
12
		}
13
		
14
		bytesToRead -= bytesRead;
15
	}
16
	
17
	for (i=0, p=(unsigned char*)buf, r=0; i<4; i++, p++) {
18
		d = *p;
19
		r = (r << 8) + d;
20
	}
21
	
22
	*value = r;
23
24
	return 0; 
25
}
26
27
char* readString(SOCKET socket) {
28
	int bytesRead;
29
	int stringLength, stringReadBytes;
30
	char* str;
31
32
	if (readInt(socket, &stringLength) < 0) {
33
		return NULL;
34
	}
35
	
36
	if (stringLength < 0) stringLength *= -1;	// negative means hyades dynamic command loading
37
	
38
	str = (char*) tptp_malloc(stringLength+1);
39
	if (str == NULL) return NULL;
40
	
41
	stringReadBytes = 0;
42
	
43
	while(stringReadBytes < stringLength) {
44
		if (readFromSocket(socket, str+stringReadBytes, stringLength-stringReadBytes, &bytesRead) <= 0) {
45
			tptp_free(str);
46
			return NULL;
47
		}
48
		
49
		stringReadBytes += bytesRead;
50
	}
51
	
52
	*(str + stringLength) = '\0';
53
	
54
	return str;
55
}
56
57
char* readStringIntoBuffer(fs_connection_block_t* con) {
58
	int bytesRead;
59
	int stringLength, stringReadBytes;
60
61
	if (readInt(con->socket, &stringLength) < 0) return NULL;
62
63
	if (stringLength < 0) stringLength *= -1;	// negative means hyades dynamic command loading
64
65
	if (stringLength > (con->bufferLength-1)) return NULL;		// including terminting 0
66
	
67
	stringReadBytes = 0;
68
	
69
	while(stringReadBytes < stringLength) {
70
		if (readFromSocket(con->socket, con->buffer+stringReadBytes, stringLength-stringReadBytes, &bytesRead) < 0) {
71
			return NULL;
72
		}
73
		
74
		stringReadBytes += bytesRead;
75
	}
76
	
77
	*(con->buffer + stringLength) = '\0';
78
	
79
	return con->buffer;
80
}
81
82
int writeByte(SOCKET socket, int byte) {
83
	char c = (char) (byte & 0xff);
84
	
85
	return writeToSocket(socket, &c, 1);
86
}
87
88
int writeBoolean(SOCKET socket, int d) {
89
	d = (d != 0) ? FS_TRUE : FS_FALSE;
90
	return writeByte(socket, d);
91
}
92
93
int writeInt (SOCKET socket, int v) {
94
	writeByte (socket, (v >> 24));
95
	writeByte (socket, (v >> 16));
96
	writeByte (socket, (v >> 8));
97
	return writeByte(socket, v);
98
}
99
100
int writeString(SOCKET socket, char* buffer) {
101
	int len = strlen(buffer);
102
	writeInt(socket, len);
103
	return writeToSocket(socket, buffer, len); 
104
}
105
106
int writeStrings(SOCKET socket, char** array, int len) {
107
	int i;
108
	
109
	writeInt(socket, len);
110
	for (i=0; i<len; i++) {
111
		writeString(socket, array[i]);
112
	}
113
	
114
	return 0; 
115
}
116
117
int readFile(fs_connection_block_t* con, char* fileName) {
118
	int contentLen;
119
	FILE* fp;
120
	int num;
121
	
122
	fp = fopen(fileName, "wb");
123
	if (fp == NULL) {
124
		TPTP_LOG_DEBUG_MSG1(con->stateData, "NFS: file open error: %s", fileName);
125
		return -1;
126
	}
127
128
	readInt(con->socket, &contentLen);	// high 4 bytes
129
	readInt(con->socket, &contentLen);	
130
131
	while (contentLen > 0) {
132
		int bytesToRead = contentLen;
133
		if (bytesToRead > con->bufferLength) bytesToRead = con->bufferLength;
134
		
135
		if (readFromSocket(con->socket, con->buffer, bytesToRead, &num) < 0) break;
136
		if (num <= 0) break;
137
		
138
		fwrite(con->buffer, num, 1, fp);
139
		contentLen -= num;
140
	}
141
142
	fclose(fp);
143
144
	writeInt(con->socket, 0);	// high 4 bytes
145
	writeInt(con->socket, getFileSize(fileName));	
146
147
	return 0; 
148
}
149
150
int writeFile(fs_connection_block_t* con, char* fileName) {
151
	int contentLen;
152
	int gotLen;
153
	FILE* fp;
154
	int n;
155
	
156
	contentLen = getFileSize(fileName);
157
	if (contentLen < 0) return -1;
158
159
	fp = fopen(fileName, "rb");
160
	if (fp == NULL) return -1;
161
162
	writeInt(con->socket, 0);			// high 4 bytes of contentLength (64-bit)
163
	writeInt(con->socket, contentLen);
164
165
	while ((n = fread((void *) con->buffer, 1, con->bufferLength, fp)) > 0) {
166
		writeToSocket(con->socket, con->buffer, n); 
167
	}		
168
169
	fclose(fp);
170
				
171
	readInt(con->socket, &gotLen);	// high 4 bytes
172
	readInt(con->socket, &gotLen);
173
	
174
	return (contentLen == gotLen) ? 0 : -1; 
175
}
176
177
int openControlDialog(fs_connection_block_t* con) {
178
	return writeString(con->socket, OPEN_CONTROL_DIALOG);
179
}
180
181
int closeControlDialog(fs_connection_block_t* con) {
182
	return writeString(con->socket, CLOSE_CONTROL_DIALOG);
183
}
184
185
int readFSCommand(fs_connection_block_t* con) {
186
	char* str;
187
188
	str = readStringIntoBuffer(con);
189
	if (str == NULL) {
190
		return -1;
191
	}
192
 
193
	if (!strcmp(str, DETERMINE_SERVER_REACH_COMMAND_STR)) return DETERMINE_SERVER_REACH_COMMAND; 
194
	if (!strcmp(str, GET_FILE_COMMAND_STR)) return GET_FILE_COMMAND;
195
	if (!strcmp(str, PUT_FILE_COMMAND_STR)) return PUT_FILE_COMMAND;
196
	if (!strcmp(str, DELETE_FILE_COMMAND_STR)) return DELETE_FILE_COMMAND;
197
	if (!strcmp(str, DELETE_DIR_COMMAND_STR)) return DELETE_DIR_COMMAND;
198
	if (!strcmp(str, VALIDATE_DIR_COMMAND_STR)) return VALIDATE_DIR_COMMAND;
199
	if (!strcmp(str, QUERY_SERVER_STATUS_COMMAND_STR)) return QUERY_SERVER_STATUS_COMMAND;
200
	if (!strcmp(str, MDFY_PERMISSION_COMMAND_STR)) return MDFY_PERMISSION_COMMAND;
201
	if (!strcmp(str, LIST_CONTENT_COMMAND_STR)) return LIST_CONTENT_COMMAND;
202
203
	TPTP_LOG_DEBUG_MSG1(con->stateData, "NFS: unknown message: %s", str);
204
205
	return -1;		// unknown command !!! save log 
206
}
207
208
int processServerReachCmd(fs_connection_block_t* con) {
209
	char* host;
210
	int port;
211
	int d, rc;
212
	SOCKET testSocket;
213
	unsigned long ipAddr;
214
	
215
	host = readStringIntoBuffer(con);
216
	if (host == NULL) return -1;
217
	
218
	readInt (con->socket, &port); 
219
	
220
	d = 0;
221
	rc = convertIPAddressStringToUlong(host, &ipAddr);	// reverse call
222
	if (rc == 0) {
223
		if (!connectToTCPServer(ipAddr, (unsigned short) port, &testSocket)) {
224
			d = 1;
225
			closeSocket(testSocket);
226
		}
227
	}
228
	
229
	writeByte(con->socket, d);	// "server is reachable" responce
230
	
231
	return 0;
232
}
233
234
int getFiles(fs_connection_block_t* con, char** names, int num) {
235
	int i;
236
	char* name;
237
238
	for(i=0; i<num; i++) {
239
		name = readString(con->socket);
240
		if (name == NULL) {
241
			return -1;		
242
		}
243
		
244
		names[i] = name;	
245
	}
246
247
	for (i=0; i<num; i++) {
248
		if (writeFile(con, names[i])) {
249
			return -1;
250
		}
251
	}
252
	
253
	return 0;	
254
}
255
256
void checkFileSeparators(char* path) {
257
	char  ips;
258
	char* p;
259
	
260
#ifdef _WIN32
261
	ips = '/';
262
#else
263
	ips = '\\';
264
#endif			
265
	
266
	p = path;
267
	while (1) {
268
		p = strchr(p, ips);
269
		if (p == NULL) break;
270
		
271
		*p = pathSeparatorCharacter;
272
	}
273
}
274
275
int mkDirs(char* path) {
276
	char *p;
277
	
278
	p = path;
279
	while(1) {
280
		p = strchr(p, pathSeparatorCharacter);
281
		if (p == NULL) break;
282
		
283
		*p = '\0';
284
		
285
#ifdef _WIN32
286
		_mkdir(path);
287
#else
288
		mkdir(path, 0777);
289
#endif
290
		
291
		*p++ = pathSeparatorCharacter;
292
	}
293
	
294
	return 0;
295
}
296
297
char* getAbsolutePath(char* path) {
298
	char *absPath, *tmpDir;
299
	int tmpDirLen;
300
	
301
	if (isAbsolutePath(path)) return NULL;
302
	
303
	checkFileSeparators(path);	
304
	
305
	tmpDir = getTempDir();
306
	tmpDirLen = strlen(tmpDir);
307
	
308
	absPath = (char*) tptp_malloc(strlen(tmpDir) + strlen(path) + 2);	// including trailing null and file separator
309
	strcpy(absPath, tmpDir);
310
	if (*(absPath+tmpDirLen-1) != pathSeparatorCharacter) strcat(absPath, pathSeparator);
311
	strcat(absPath, path);
312
313
	mkDirs(absPath);	
314
	
315
	return absPath;
316
}
317
318
int putFiles(fs_connection_block_t* con, char** names, int num) {
319
	int i;
320
	char *name, *absName;
321
	
322
	for(i=0; i<num; i++) {
323
		name = readStringIntoBuffer(con);
324
		if (name == NULL) {
325
			return -1;		
326
		}
327
		
328
		absName = getAbsolutePath(name);
329
		if (absName != NULL) { 
330
			names[i] = absName;
331
		}
332
		else {
333
			names[i] = (char*) tptp_malloc(strlen(name)+1);
334
			strcpy(names[i], name);
335
		}	
336
	}
337
338
	for (i=0; i<num; i++) {
339
		if (readFile(con, names[i])) {
340
			return -1;
341
		}
342
	}	
343
344
	writeStrings(con->socket, names, num);	// files resolution
345
	
346
	return 0;
347
}
348
349
int processDeleteFileCmd(fs_connection_block_t* con) {
350
	int i, numFiles, num;
351
	char* name;
352
353
	readInt(con->socket, &numFiles);
354
	readInt(con->socket, &num);
355
	if (num <= 0) return -1;
356
	
357
	for (i=0; i<num; i++) {
358
		name = readStringIntoBuffer(con);
359
		if (name == NULL) return -1;
360
		
361
		remove(name);
362
	}
363
364
	return 0;
365
}
366
367
int validateDir(fs_connection_block_t* con, char** names, int num) {
368
	int i;
369
	char* name;
370
	
371
	for(i=0; i<num; i++) {
372
		name = readString(con->socket);
373
		if (name == NULL) {
374
			return -1;		
375
		}
376
		
377
		names[i] = name;	
378
	}
379
380
	writeInt(con->socket, num);			// reponse array size
381
	for (i=0; i<num; i++) {
382
		writeBoolean(con->socket, validateDirectory(names[i]));
383
	}
384
	
385
	return 0;	
386
}
387
388
int processQueryServerStatusCmd(fs_connection_block_t* con) {
389
	char* message;
390
	
391
	message = readStringIntoBuffer(con);
392
	if (message == NULL) return -1;
393
	
394
	writeString(con->socket, message);
395
	
396
	return 0;
397
}
398
399
int processDummyCmd(fs_connection_block_t* con) {
400
	return 0;
401
}
402
403
int processFileArrayCmd(fs_connection_block_t* con, int cmd) {
404
	int i, numFiles, num;
405
	char** names=NULL;
406
	int res=0;
407
	
408
	readInt(con->socket, &numFiles);
409
	readInt(con->socket, &num);
410
	if (num <= 0) return -1;
411
	
412
	names = (char**) malloc(num*sizeof(char*));
413
	memset(names, 0, num*sizeof(char*));
414
415
	switch(cmd) {
416
	case GET_FILE_COMMAND:
417
		res = getFiles(con, names, num);
418
		break;
419
		 
420
	case PUT_FILE_COMMAND:
421
		res = putFiles(con, names, num);
422
		break;
423
424
	case VALIDATE_DIR_COMMAND:
425
		res = validateDir(con, names, num);
426
		break;
427
	}
428
429
	for (i=0; i<num; i++) {
430
		if (names[i] != NULL) tptp_free(names[i]);
431
	}
432
	
433
	tptp_free(names);
434
	
435
	return res;
436
}
437
438
int processFSCommand (fs_connection_block_t* con, int cmd) {
439
	openControlDialog(con);
440
	closeControlDialog(con);
441
442
	switch(cmd) {
443
	case DETERMINE_SERVER_REACH_COMMAND:
444
		return processServerReachCmd(con);
445
446
	case GET_FILE_COMMAND:
447
	case PUT_FILE_COMMAND:
448
	case VALIDATE_DIR_COMMAND:
449
		return processFileArrayCmd(con, cmd);
450
451
	case DELETE_FILE_COMMAND:
452
	case DELETE_DIR_COMMAND:
453
		return processDeleteFileCmd(con);
454
455
	case QUERY_SERVER_STATUS_COMMAND:
456
		return processQueryServerStatusCmd(con);
457
458
	case MDFY_PERMISSION_COMMAND:
459
	case LIST_CONTENT_COMMAND:
460
		return processDummyCmd(con);
461
	}
462
	
463
	return -1;
464
}
465
466
THREAD_USER_FUNC_RET_TYPE processFSRequest(LPVOID args){
467
	SOCKET socket;
468
	fs_connection_block_t* con;
469
	int cmd;
470
471
	con = (fs_connection_block_t*)args;
472
	socket = con->socket;
473
	
474
	while ((cmd = readFSCommand(con)) > 0) {
475
		if (processFSCommand(con, cmd)) break;
476
	}
477
478
	tptp_free(con->buffer);
479
	tptp_free(con);
480
	closeSocket(socket);
481
	
482
	return 0;
483
}
484
485
static SOCKET serverSocket;
486
487
THREAD_USER_FUNC_RET_TYPE startNativeFileServer(LPVOID args){
488
	int rc, port;
489
	struct sockaddr_in  fsSockAddr;
490
	SOCKET clientSock;
491
	fs_connection_block_t* con;
492
	tl_state_data_t*    stateData;
493
	TID                 threadId;
494
	HANDLE              threadHandle;
495
496
	stateData = (tl_state_data_t*) args;
497
	port = ((cctl_state_data_t*)stateData->implData)->filePort;
498
	if (port <= 0) port = RA_FILE_PORT_NUM_SERVER;
499
		
500
	serverSocket = getTheSocket(port, &fsSockAddr);
501
	if (serverSocket < 0) {
502
		TPTP_LOG_ERROR_MSG(stateData, "NFS: unable to create the file server socket.");
503
		return 0;
504
	}
505
506
	rc = bindAndListen(serverSocket, (struct sockaddr*)&fsSockAddr);
507
	if (rc != 0) {
508
		TPTP_LOG_ERROR_MSG(stateData, "NFS: cannot bind to server socket");
509
		return 0;
510
	}
511
512
	while(1) { 
513
		clientSock = acceptSocketConnection(serverSocket); 
514
	    if (clientSock < 0) break;
515
516
		con = (fs_connection_block_t*) tptp_malloc(sizeof(fs_connection_block_t));
517
		if (con == NULL) {
518
			closeSocket(clientSock);
519
			continue;
520
		}
521
			
522
		con->socket = clientSock;        
523
		con->buffer = (char*) tptp_malloc(TPTP_DEFAULT_BUFFER_LENGTH+1); // +1 since readFromSocket() adds terminating 0
524
		con->bufferLength = TPTP_DEFAULT_BUFFER_LENGTH;
525
		con->stateData = stateData;
526
	        
527
	    tptpStartThread(processFSRequest, (LPVOID)con, &threadId, &threadHandle) ;
528
	}
529
	
530
	closeSocket(serverSocket);
531
	
532
	return 0;
533
} 
534
535
int stopNativeFileServer() {
536
	closeSocket(serverSocket);
537
	return 0;
538
}

Return to bug 196435