|
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 |
} |