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 296799
Collapse All | Expand All

(-)src-native-new/include/tptp/TPTPUtils.h (-1 / +38 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2009 Intel Corporation and others.
2
 * Copyright (c) 2005, 2010 Intel Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 319-324 Link Here
319
319
320
int vrfusrpwd(tptp_string *userid, tptp_string *password);
320
int vrfusrpwd(tptp_string *userid, tptp_string *password);
321
321
322
323
// Recursive Directory Deletion Implementation ===============
324
325
#ifdef _WIN32
326
327
	typedef struct {
328
		HANDLE hFind;
329
		WIN32_FIND_DATA findData;
330
	} fileDirectoryPos;
331
332
#else
333
334
	#include <sys/types.h>
335
	#include <dirent.h>
336
	
337
	typedef struct {
338
		DIR * dirPointer;
339
		char * currEntry;
340
	} fileDirectoryPos;
341
342
#endif
343
344
/** Returns 1 if is a directory, 0 if not, -1 if file does not exist or there is some other error*/
345
int isDirectory(char * path);
346
347
fileDirectoryPos * fileOpenDir(char *fileName);
348
349
char * getPathFromDirPos(fileDirectoryPos * dirPos);
350
351
int advanceToNextDirPos(fileDirectoryPos * dirPos);
352
353
int rmdirDirectory(char * path);
354
355
void closeDirPos(fileDirectoryPos *dirPos);
356
357
int fileExists(char * path);
358
322
#ifdef __cplusplus
359
#ifdef __cplusplus
323
} /* Ends extern C */
360
} /* Ends extern C */
324
#endif
361
#endif
(-)src-native-new/src/shared/TPTPUtil/TPTPUtil.cpp (-1 / +196 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2005, 2010 Intel Corporation.
2
 * Copyright (c) 2005, 2010 Intel Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 39-44 Link Here
39
#ifdef _WIN32
39
#ifdef _WIN32
40
	#include <sys/types.h>
40
	#include <sys/types.h>
41
	#include <sys/stat.h>
41
	#include <sys/stat.h>
42
	#include <errno.h>	
42
	#define TPTP_STAT _stat
43
	#define TPTP_STAT _stat
43
	#define mode_t unsigned short
44
	#define mode_t unsigned short
44
	#define S_ISDIR(mode) (((mode)&_S_IFMT) == _S_IFDIR)
45
	#define S_ISDIR(mode) (((mode)&_S_IFMT) == _S_IFDIR)
Lines 2276-2278 Link Here
2276
} 
2277
} 
2277
2278
2278
2279
2280
/** Returns 1 if file (or directory) exists, 0 if not, -1 if there is some other error*/
2281
int fileExists(char * path) {
2282
	#ifdef _WIN32
2283
2284
		struct _stat statbuf;
2285
		int result = 0;
2286
2287
		result = _stat(path, &statbuf);
2288
2289
		if(result != 0) {
2290
			if(errno == ENOENT) {
2291
				return 0;
2292
			} else {
2293
				return -1;
2294
			}
2295
2296
		}
2297
2298
		return 1;
2299
2300
	#else
2301
		struct stat statbuf;
2302
		int result = 0;
2303
2304
		result = stat(path, &statbuf);
2305
2306
		if(result != 0) {
2307
			if(errno == ENOENT) {
2308
				return 0;
2309
			} else {
2310
				return -1;
2311
			}
2312
2313
		}
2314
2315
		return 1;
2316
	#endif
2317
}
2318
2319
2320
2321
/** Returns 1 if is a directory, 0 if not, -1 if file does not exist or there is some other error*/
2322
int isDirectory(char * path) {
2323
	#ifdef _WIN32
2324
		if(path == 0) return -1;
2325
2326
		DWORD attrs;
2327
		attrs = GetFileAttributes(path);
2328
2329
		if (attrs == INVALID_FILE_ATTRIBUTES) return -1;
2330
2331
		return (attrs & FILE_ATTRIBUTE_DIRECTORY) > 0;
2332
2333
	#else
2334
2335
		struct stat statbuf;
2336
		int result = 0;
2337
2338
		result = stat(path, &statbuf);
2339
2340
		if(result != 0) {
2341
			return -1;
2342
		}
2343
2344
		return S_ISDIR(statbuf.st_mode);
2345
2346
	#endif
2347
}
2348
2349
2350
/** Returns 0 if successful, -1 otherwise*/
2351
int rmdirDirectory(char * path) {
2352
	int r = 0;
2353
2354
	#ifdef _WIN32
2355
2356
		r = RemoveDirectory(path);
2357
2358
		// If the operation reports to have succeeded, or the directory no longer exists, then return
2359
		if(r != 0 || fileExists(path) == 0) {
2360
			return 0;
2361
		} else {
2362
			return -1;
2363
		}
2364
2365
	#else
2366
2367
		// Attempt to remove directory first -- this will succeed on either empty directories, or sym links
2368
		// Do not remove this -- this is to prevent recursion into the contents of symlinked dirs which shouldn't be deleted
2369
		r = remove(path);
2370
		if(r == 0 || fileExists(path) == 0) {
2371
			return 0;
2372
		} else {
2373
			return -1;
2374
		}
2375
2376
	#endif
2377
2378
}
2379
2380
fileDirectoryPos * fileOpenDir(char *fileName) {
2381
	fileDirectoryPos * result;
2382
2383
	#ifdef _WIN32
2384
		char *updatedPath;
2385
2386
		// the FindFirstFile API requires a wildcard at the end of the path, so we append one
2387
		// length is = strlen(fileName) + 2 (\*) + 1 (\0) + 1 (buffer)
2388
		updatedPath = (char *)malloc(strlen(fileName) + 2 + 1 + 1 );
2389
		sprintf(updatedPath, "%s\\*", fileName);
2390
2391
		result = (fileDirectoryPos *)malloc(sizeof(fileDirectoryPos));
2392
2393
		result->hFind = FindFirstFile(updatedPath, &(result->findData));
2394
2395
		free(updatedPath);
2396
2397
		if (result->hFind == INVALID_HANDLE_VALUE)  {
2398
			free(result);
2399
			return 0;
2400
		}  else {
2401
			return result;
2402
		}
2403
2404
	#else
2405
2406
		DIR *dir;
2407
		struct dirent *ep;
2408
		dir = opendir (fileName);
2409
		if(dir == 0) return 0;
2410
2411
		ep = readdir (dir);
2412
		if(ep == 0) return 0;
2413
2414
		result = (fileDirectoryPos *) malloc(sizeof(fileDirectoryPos));
2415
2416
		result->dirPointer = dir;
2417
		result->currEntry = ep->d_name;
2418
2419
		return result;
2420
2421
	#endif
2422
2423
}
2424
2425
char * getPathFromDirPos(fileDirectoryPos * dirPos) {
2426
	if(dirPos == 0) return 0;
2427
2428
	#ifdef _WIN32
2429
		return dirPos->findData.cFileName;
2430
	#else
2431
		return dirPos->currEntry;
2432
	#endif
2433
2434
}
2435
2436
int advanceToNextDirPos(fileDirectoryPos * dirPos) {
2437
2438
	#ifdef _WIN32
2439
2440
		int result;
2441
2442
		result = FindNextFile(dirPos->hFind, &(dirPos->findData));
2443
		if(result == 0) {
2444
			return -1;
2445
		}
2446
2447
		return 0;
2448
2449
	#else
2450
2451
		struct dirent *ep;
2452
2453
		ep = readdir (dirPos->dirPointer);
2454
		if(ep == 0) return -1;
2455
2456
		dirPos->currEntry = ep->d_name;
2457
2458
		return 0;
2459
2460
	#endif
2461
2462
}
2463
2464
void closeDirPos(fileDirectoryPos *dirPos) {
2465
2466
	#ifdef _WIN32
2467
		FindClose(dirPos->hFind);
2468
	#else
2469
		closedir (dirPos->dirPointer);
2470
	#endif
2471
2472
	free(dirPos);
2473
}
(-)src-native-new/src/shared/TPTPUtil/TPTUtil.def (-1 / +7 lines)
Lines 103-106 Link Here
103
	validateDirectory
103
	validateDirectory
104
	isAbsolutePath
104
	isAbsolutePath
105
	getTempDir	
105
	getTempDir	
106
	
106
	isDirectory
107
	fileOpenDir
108
	getPathFromDirPos
109
	advanceToNextDirPos
110
	closeDirPos
111
	fileExists
112
	rmdirDirectory
(-)src-native-new/src/transport/TPTPClientCompTL/nativeFileServer.c (-2 / +233 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2008, 2009 Intel Corporation.
2
 * Copyright (c) 2008, 2010 Intel Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 15-20 Link Here
15
#include "nativeFileServer.h"
15
#include "nativeFileServer.h"
16
#include "tptp/TPTPUtils.h"
16
#include "tptp/TPTPUtils.h"
17
17
18
#include <stdio.h>
19
#include <stdlib.h>
20
21
#include <sys/types.h>
22
#include <sys/stat.h>
23
#include <errno.h>
24
25
#ifdef _WIN32
26
	#define WIN32_LEAN_AND_MEAN
27
	#include <windows.h>
28
#else
29
30
	#include <unistd.h>
31
	#include <dirent.h>
32
33
#endif
34
35
int deleteDirectory(char * path);
36
18
int readInt (SOCKET socket, int* value) {
37
int readInt (SOCKET socket, int* value) {
19
	int bytesRead, bytesToRead, i, d, r;
38
	int bytesRead, bytesToRead, i, d, r;
20
	unsigned char* p;
39
	unsigned char* p;
Lines 396-401 Link Here
396
	return 0;
415
	return 0;
397
}
416
}
398
417
418
int processDeleteDirCmd(fs_connection_block_t* con) {
419
	int i, numFiles, num;
420
	char* name;
421
422
	readInt(con->socket, &numFiles);
423
	readInt(con->socket, &num);
424
	if (num <= 0) return -1;
425
	
426
	for (i=0; i<num; i++) {
427
		name = readStringIntoBuffer(con);
428
		if (name == NULL) return -1;
429
		
430
		deleteDirectory(name);
431
432
		remove(name);
433
	}
434
435
	return 0;
436
437
}
438
439
399
int processDeleteFileCmd(fs_connection_block_t* con) {
440
int processDeleteFileCmd(fs_connection_block_t* con) {
400
	int i, numFiles, num;
441
	int i, numFiles, num;
401
	char* name;
442
	char* name;
Lines 499-506 Link Here
499
		return processFileArrayCmd(con, cmd);
540
		return processFileArrayCmd(con, cmd);
500
541
501
	case DELETE_FILE_COMMAND:
542
	case DELETE_FILE_COMMAND:
502
	case DELETE_DIR_COMMAND:
503
		return processDeleteFileCmd(con);
543
		return processDeleteFileCmd(con);
544
		
545
	case DELETE_DIR_COMMAND:
546
		return processDeleteDirCmd(con);
504
547
505
	case QUERY_SERVER_STATUS_COMMAND:
548
	case QUERY_SERVER_STATUS_COMMAND:
506
		return processQueryServerStatusCmd(con);
549
		return processQueryServerStatusCmd(con);
Lines 591-593 Link Here
591
	closeSocket(serverSocket);
634
	closeSocket(serverSocket);
592
	return 0;
635
	return 0;
593
}
636
}
637
638
static int isSymLink(char *path) {
639
	#ifndef _WIN32
640
641
		struct stat statbuf;
642
		int result = 0;
643
644
		result = lstat(path, &statbuf);
645
646
		if(result != 0) {
647
			return -1;
648
		}
649
650
		return S_ISLNK(statbuf.st_mode);
651
	#else
652
		// No sym links in Windows :)
653
		return 0;
654
	#endif
655
}
656
657
#ifdef _WIN32
658
	#define PATH_SEPARATOR_STR "\\"
659
	#define PATH_SEPARATOR_CHAR '\\'
660
#else
661
	#define PATH_SEPARATOR_STR "/"
662
	#define PATH_SEPARATOR_CHAR '/'
663
#endif
664
665
static int stripTrailingSlashIfExists(char * str) {
666
	int r = 0;
667
668
	if(str == 0) return -1;
669
670
	r = strlen(str);
671
672
	if(str[r-1] == PATH_SEPARATOR_CHAR) {
673
		str[r-1] = 0;
674
		return 1;
675
	}
676
677
	return 0;
678
}
679
680
static int deleteFile(char * path ) {
681
	int r = 0;
682
	if(path == 0) { return -1; }
683
684
	r = remove(path);
685
686
	if(r != 0) {
687
		return -1;
688
	}
689
690
	return 0;
691
}
692
693
694
695
static int recurseDeleteFilesInPath(char * path) {
696
	char * localPath;
697
	fileDirectoryPos *dp;
698
699
	char *currName;
700
	char *newPath;
701
	int r = 0;
702
	int skipFile = 0;
703
704
	if(fileExists(path) != 1 || isDirectory(path) != 1) {
705
		return -1;
706
	}
707
708
	localPath = (char *) malloc(strlen(path)+1);
709
	strcpy(localPath, path);
710
	stripTrailingSlashIfExists(localPath);
711
712
	dp = fileOpenDir(localPath);
713
714
	if(dp != 0) {
715
716
		currName = getPathFromDirPos(dp);
717
718
		while (currName != 0) {
719
720
			// Skip the . and .. values
721
			if(!strcmp(currName, ".")) { skipFile = 1; }
722
			if(!strcmp(currName, "..")) { skipFile = 1; }
723
724
			if(!skipFile) {
725
726
				// len(localPath) + 1 (for path separator) + length of file name + 1 (for \0 string terminator) + 1 (for buffer)
727
				newPath = (char *)malloc(  strlen(localPath) + 1 + strlen(currName) + 1 + 1  );
728
729
				sprintf(newPath, "%s%s%s", localPath, PATH_SEPARATOR_STR, currName);
730
731
				if(isDirectory(newPath) == 1) {
732
					deleteDirectory(newPath);
733
				} else {
734
					deleteFile(newPath);
735
				}
736
737
				free(newPath);
738
			}
739
740
			if(advanceToNextDirPos(dp) != 0) {
741
				currName = 0;
742
			} else {
743
				currName = getPathFromDirPos(dp);
744
			}
745
			skipFile = 0;
746
747
		}
748
749
		closeDirPos(dp);
750
751
	}
752
753
	free(localPath);
754
755
	return 0;
756
757
}
758
759
/** Note: This function will be passed to both regular directory paths, and symlinked directory paths */
760
int deleteDirectory(char * path) {
761
	int pathLength = 0;
762
	int r = 0;
763
764
	if(path == 0) {
765
		return -1;
766
	}
767
768
	// If the path is not a directory, or there was an error, return -1
769
	if(isDirectory(path) != 1) {
770
		return -1;
771
	}
772
773
	pathLength = strlen(path);
774
775
	// Safety check to ensure we are not being asked to delete the root of the file system
776
	#ifdef _WIN32
777
778
		// This matches any incidence of (drive-letter):(\), so, c:, d:, c:\, d:\, etc.
779
		if( (pathLength == 2 || pathLength == 3) && ((path[0] >= 'a' && path[0] <= 'z') || (path[0] >= 'A' && path[0] <= 'Z')  ) ) {
780
781
			if(path[1] == ':') {
782
				return  -1;
783
			}
784
785
		}
786
787
	#endif
788
789
	// This matches either \ or / or . or .. or empty string
790
	if(!strcmp(path, "/") || !strcmp(path, "\\") || !strcmp(path, ".") || !strcmp(path, "..") || !strcmp(path, "~") || !strcmp(path, "") ) {
791
		return -1;
792
	}
793
794
795
	// Attempt to remove directory first -- this will succeed on either empty directories, or sym links
796
	// Do not remove this -- this is to prevent recursion into the contents of symlinked dirs which shouldn't be deleted
797
	r = rmdirDirectory(path);
798
	if(r == 0) {
799
		return 0;
800
	}
801
802
	#ifndef _WIN32
803
804
		// The symlink should have been removed by the above; we don't want to recurse it if it hasn't, so just return here.
805
		if(isSymLink(path) == 1) {
806
			return -1;
807
		}
808
809
	#endif
810
811
	// We are assured at this point that path is a directory by the isDirectory check above
812
813
	recurseDeleteFilesInPath(path);
814
815
	r = rmdirDirectory(path);
816
817
	// If the operation reports to have succeeded, or the directory no longer exists, then return
818
	if(r == 0) {
819
		return 0;
820
	}
821
822
	return r;
823
824
}
(-)src-native-new/src/transport/TPTPClientCompTL/nativeFileServer.h (-1 / +6 lines)
Lines 1-5 Link Here
1
/*******************************************************************************
1
/*******************************************************************************
2
 * Copyright (c) 2008 Intel Corporation.
2
 * Copyright (c) 2008, 2010 Intel Corporation and others.
3
 * All rights reserved. This program and the accompanying materials
3
 * All rights reserved. This program and the accompanying materials
4
 * are made available under the terms of the Eclipse Public License v1.0
4
 * are made available under the terms of the Eclipse Public License v1.0
5
 * which accompanies this distribution, and is available at
5
 * which accompanies this distribution, and is available at
Lines 15-20 Link Here
15
#ifndef _NATIVE_FILE_SERVER
15
#ifndef _NATIVE_FILE_SERVER
16
#define _NATIVE_FILE_SERVER
16
#define _NATIVE_FILE_SERVER
17
17
18
#ifdef _WIN32
19
	#define WIN32_LEAN_AND_MEAN
20
	#include <windows.h>
21
#endif
22
18
#include <string.h>
23
#include <string.h>
19
24
20
#ifdef _WIN32
25
#ifdef _WIN32

Return to bug 296799