2010-11-29 13:13:04 -05:00
|
|
|
/*
|
|
|
|
Minetest-c55
|
|
|
|
Copyright (C) 2010 celeron55, Perttu Ahola <celeron55@gmail.com>
|
|
|
|
|
|
|
|
This program is free software; you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation; either version 2 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2010-11-26 18:02:21 -05:00
|
|
|
#include "filesys.h"
|
2011-01-25 17:49:32 -05:00
|
|
|
#include "strfnd.h"
|
2010-11-26 18:02:21 -05:00
|
|
|
#include <iostream>
|
2011-01-25 17:40:33 -05:00
|
|
|
#include <string.h>
|
2012-03-22 18:05:32 -04:00
|
|
|
#include "log.h"
|
2010-11-26 18:02:21 -05:00
|
|
|
|
|
|
|
namespace fs
|
|
|
|
{
|
|
|
|
|
2011-01-07 07:52:27 -05:00
|
|
|
#ifdef _WIN32 // WINDOWS
|
2010-11-26 18:02:21 -05:00
|
|
|
|
|
|
|
#define _WIN32_WINNT 0x0501
|
2011-08-14 14:26:07 -04:00
|
|
|
#include <windows.h>
|
2010-11-26 18:02:21 -05:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <malloc.h>
|
|
|
|
#include <tchar.h>
|
|
|
|
#include <wchar.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
|
|
|
|
#define BUFSIZE MAX_PATH
|
|
|
|
|
|
|
|
std::vector<DirListNode> GetDirListing(std::string pathstring)
|
|
|
|
{
|
|
|
|
std::vector<DirListNode> listing;
|
|
|
|
|
|
|
|
WIN32_FIND_DATA FindFileData;
|
|
|
|
HANDLE hFind = INVALID_HANDLE_VALUE;
|
|
|
|
DWORD dwError;
|
|
|
|
LPTSTR DirSpec;
|
|
|
|
INT retval;
|
|
|
|
|
|
|
|
DirSpec = (LPTSTR) malloc (BUFSIZE);
|
|
|
|
|
|
|
|
if( DirSpec == NULL )
|
|
|
|
{
|
2012-03-22 18:05:32 -04:00
|
|
|
errorstream<<"GetDirListing: Insufficient memory available"<<std::endl;
|
2010-11-26 18:02:21 -05:00
|
|
|
retval = 1;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check that the input is not larger than allowed.
|
|
|
|
if (pathstring.size() > (BUFSIZE - 2))
|
|
|
|
{
|
2012-03-22 18:05:32 -04:00
|
|
|
errorstream<<"GetDirListing: Input directory is too large."<<std::endl;
|
2010-11-26 18:02:21 -05:00
|
|
|
retval = 3;
|
|
|
|
goto Cleanup;
|
|
|
|
}
|
|
|
|
|
|
|
|
//_tprintf (TEXT("Target directory is %s.\n"), pathstring.c_str());
|
|
|
|
|
|
|
|
sprintf(DirSpec, "%s", (pathstring + "\\*").c_str());
|
|
|
|
|
|
|
|
// Find the first file in the directory.
|
|
|
|
hFind = FindFirstFile(DirSpec, &FindFileData);
|
|
|
|
|
|
|
|
if (hFind == INVALID_HANDLE_VALUE)
|
|
|
|
{
|
2012-03-25 16:16:53 -04:00
|
|
|
retval = (-1);
|
|
|
|
goto Cleanup;
|
2010-11-26 18:02:21 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2011-01-25 17:49:32 -05:00
|
|
|
// NOTE:
|
|
|
|
// Be very sure to not include '..' in the results, it will
|
|
|
|
// result in an epic failure when deleting stuff.
|
|
|
|
|
2010-11-26 18:02:21 -05:00
|
|
|
DirListNode node;
|
|
|
|
node.name = FindFileData.cFileName;
|
|
|
|
node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
2011-01-25 17:49:32 -05:00
|
|
|
if(node.name != "." && node.name != "..")
|
|
|
|
listing.push_back(node);
|
2010-11-26 18:02:21 -05:00
|
|
|
|
|
|
|
// List all the other files in the directory.
|
|
|
|
while (FindNextFile(hFind, &FindFileData) != 0)
|
|
|
|
{
|
|
|
|
DirListNode node;
|
|
|
|
node.name = FindFileData.cFileName;
|
|
|
|
node.dir = FindFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY;
|
2011-01-25 17:49:32 -05:00
|
|
|
if(node.name != "." && node.name != "..")
|
|
|
|
listing.push_back(node);
|
2010-11-26 18:02:21 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
dwError = GetLastError();
|
|
|
|
FindClose(hFind);
|
|
|
|
if (dwError != ERROR_NO_MORE_FILES)
|
|
|
|
{
|
2012-03-22 18:05:32 -04:00
|
|
|
errorstream<<"GetDirListing: FindNextFile error. Error is "
|
|
|
|
<<dwError<<std::endl;
|
|
|
|
retval = (-1);
|
|
|
|
goto Cleanup;
|
2010-11-26 18:02:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
retval = 0;
|
|
|
|
|
|
|
|
Cleanup:
|
|
|
|
free(DirSpec);
|
|
|
|
|
|
|
|
if(retval != 0) listing.clear();
|
|
|
|
|
|
|
|
//for(unsigned int i=0; i<listing.size(); i++){
|
2012-03-22 18:05:32 -04:00
|
|
|
// infostream<<listing[i].name<<(listing[i].dir?" (dir)":" (file)")<<std::endl;
|
2010-11-26 18:02:21 -05:00
|
|
|
//}
|
|
|
|
|
|
|
|
return listing;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CreateDir(std::string path)
|
|
|
|
{
|
|
|
|
bool r = CreateDirectory(path.c_str(), NULL);
|
|
|
|
if(r == true)
|
|
|
|
return true;
|
|
|
|
if(GetLastError() == ERROR_ALREADY_EXISTS)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PathExists(std::string path)
|
|
|
|
{
|
|
|
|
return (GetFileAttributes(path.c_str()) != INVALID_FILE_ATTRIBUTES);
|
|
|
|
}
|
|
|
|
|
2012-03-25 17:21:38 -04:00
|
|
|
bool IsDir(std::string path)
|
2011-01-25 17:40:33 -05:00
|
|
|
{
|
2012-03-25 17:21:38 -04:00
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
return (attr != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attr & FILE_ATTRIBUTE_DIRECTORY));
|
|
|
|
}
|
2011-01-25 17:40:33 -05:00
|
|
|
|
2012-03-25 17:21:38 -04:00
|
|
|
bool RecursiveDelete(std::string path)
|
|
|
|
{
|
|
|
|
infostream<<"Recursively deleting \""<<path<<"\""<<std::endl;
|
2011-01-25 18:06:45 -05:00
|
|
|
|
2012-03-25 17:21:38 -04:00
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attr & FILE_ATTRIBUTE_DIRECTORY));
|
|
|
|
if(!is_directory)
|
|
|
|
{
|
|
|
|
infostream<<"RecursiveDelete: Deleting file "<<path<<std::endl;
|
|
|
|
//bool did = DeleteFile(path.c_str());
|
|
|
|
bool did = true;
|
|
|
|
if(!did){
|
|
|
|
errorstream<<"RecursiveDelete: Failed to delete file "
|
|
|
|
<<path<<std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
infostream<<"RecursiveDelete: Deleting content of directory "
|
|
|
|
<<path<<std::endl;
|
|
|
|
std::vector<DirListNode> content = GetDirListing(path);
|
|
|
|
for(int i=0; i<content.size(); i++){
|
|
|
|
const DirListNode &n = content[i];
|
|
|
|
std::string fullpath = path + DIR_DELIM + n.name;
|
|
|
|
bool did = RecursiveDelete(fullpath);
|
|
|
|
if(!did){
|
|
|
|
errorstream<<"RecursiveDelete: Failed to recurse to "
|
|
|
|
<<fullpath<<std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
infostream<<"RecursiveDelete: Deleting directory "<<path<<std::endl;
|
|
|
|
//bool did = RemoveDirectory(path.c_str();
|
|
|
|
bool did = true;
|
|
|
|
if(!did){
|
|
|
|
errorstream<<"Failed to recursively delete directory "
|
|
|
|
<<path<<std::endl;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2011-01-25 18:06:45 -05:00
|
|
|
return true;
|
2011-01-25 17:40:33 -05:00
|
|
|
}
|
|
|
|
|
2012-03-25 17:21:38 -04:00
|
|
|
bool DeleteSingleFileOrEmptyDirectory(std::string path)
|
|
|
|
{
|
|
|
|
DWORD attr = GetFileAttributes(path.c_str());
|
|
|
|
bool is_directory = (attr != INVALID_FILE_ATTRIBUTES &&
|
|
|
|
(attr & FILE_ATTRIBUTE_DIRECTORY));
|
|
|
|
if(!is_directory)
|
|
|
|
{
|
|
|
|
bool did = DeleteFile(path.c_str());
|
|
|
|
return did;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bool did = RemoveDirectory(path.c_str());
|
|
|
|
return did;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-07 07:52:27 -05:00
|
|
|
#else // POSIX
|
2010-11-26 18:02:21 -05:00
|
|
|
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/stat.h>
|
2011-01-25 17:40:33 -05:00
|
|
|
#include <sys/wait.h>
|
2010-11-26 18:02:21 -05:00
|
|
|
|
|
|
|
std::vector<DirListNode> GetDirListing(std::string pathstring)
|
|
|
|
{
|
|
|
|
std::vector<DirListNode> listing;
|
|
|
|
|
|
|
|
DIR *dp;
|
|
|
|
struct dirent *dirp;
|
|
|
|
if((dp = opendir(pathstring.c_str())) == NULL) {
|
2012-03-22 18:05:32 -04:00
|
|
|
//infostream<<"Error("<<errno<<") opening "<<pathstring<<std::endl;
|
2010-11-26 18:02:21 -05:00
|
|
|
return listing;
|
|
|
|
}
|
|
|
|
|
|
|
|
while ((dirp = readdir(dp)) != NULL) {
|
2011-01-25 17:49:32 -05:00
|
|
|
// NOTE:
|
|
|
|
// Be very sure to not include '..' in the results, it will
|
|
|
|
// result in an epic failure when deleting stuff.
|
2010-11-26 18:02:21 -05:00
|
|
|
if(dirp->d_name[0]!='.'){
|
|
|
|
DirListNode node;
|
|
|
|
node.name = dirp->d_name;
|
2011-12-04 05:49:58 -05:00
|
|
|
if(node.name == "." || node.name == "..")
|
|
|
|
continue;
|
|
|
|
|
|
|
|
int isdir = -1; // -1 means unknown
|
|
|
|
|
|
|
|
/*
|
2011-12-04 06:02:00 -05:00
|
|
|
POSIX doesn't define d_type member of struct dirent and
|
|
|
|
certain filesystems on glibc/Linux will only return
|
|
|
|
DT_UNKNOWN for the d_type member.
|
|
|
|
|
|
|
|
Also we don't know whether symlinks are directories or not.
|
2011-12-04 05:49:58 -05:00
|
|
|
*/
|
|
|
|
#ifdef _DIRENT_HAVE_D_TYPE
|
2011-12-04 06:02:00 -05:00
|
|
|
if(dirp->d_type != DT_UNKNOWN && dirp->d_type != DT_LNK)
|
2011-12-04 05:49:58 -05:00
|
|
|
isdir = (dirp->d_type == DT_DIR);
|
|
|
|
#endif /* _DIRENT_HAVE_D_TYPE */
|
|
|
|
|
|
|
|
/*
|
2011-12-04 06:02:00 -05:00
|
|
|
Was d_type DT_UNKNOWN, DT_LNK or nonexistent?
|
2011-12-04 05:49:58 -05:00
|
|
|
If so, try stat().
|
|
|
|
*/
|
|
|
|
if(isdir == -1)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
if (stat((pathstring + "/" + node.name).c_str(), &statbuf))
|
|
|
|
continue;
|
|
|
|
isdir = ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
|
|
|
|
}
|
|
|
|
node.dir = isdir;
|
|
|
|
listing.push_back(node);
|
2010-11-26 18:02:21 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dp);
|
|
|
|
|
|
|
|
return listing;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool CreateDir(std::string path)
|
|
|
|
{
|
|
|
|
int r = mkdir(path.c_str(), S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
|
|
|
|
if(r == 0)
|
|
|
|
{
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// If already exists, return true
|
|
|
|
if(errno == EEXIST)
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PathExists(std::string path)
|
|
|
|
{
|
|
|
|
struct stat st;
|
|
|
|
return (stat(path.c_str(),&st) == 0);
|
|
|
|
}
|
|
|
|
|
2012-03-25 17:21:38 -04:00
|
|
|
bool IsDir(std::string path)
|
|
|
|
{
|
|
|
|
struct stat statbuf;
|
|
|
|
if(stat(path.c_str(), &statbuf))
|
|
|
|
return false; // Actually error; but certainly not a directory
|
|
|
|
return ((statbuf.st_mode & S_IFDIR) == S_IFDIR);
|
|
|
|
}
|
|
|
|
|
2011-01-25 17:40:33 -05:00
|
|
|
bool RecursiveDelete(std::string path)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
Execute the 'rm' command directly, by fork() and execve()
|
|
|
|
*/
|
|
|
|
|
2012-03-22 18:05:32 -04:00
|
|
|
infostream<<"Removing \""<<path<<"\""<<std::endl;
|
2011-01-25 17:40:33 -05:00
|
|
|
|
|
|
|
//return false;
|
|
|
|
|
|
|
|
pid_t child_pid = fork();
|
|
|
|
|
|
|
|
if(child_pid == 0)
|
|
|
|
{
|
|
|
|
// Child
|
|
|
|
char argv_data[3][10000];
|
|
|
|
strcpy(argv_data[0], "/bin/rm");
|
|
|
|
strcpy(argv_data[1], "-rf");
|
|
|
|
strncpy(argv_data[2], path.c_str(), 10000);
|
|
|
|
char *argv[4];
|
|
|
|
argv[0] = argv_data[0];
|
|
|
|
argv[1] = argv_data[1];
|
|
|
|
argv[2] = argv_data[2];
|
|
|
|
argv[3] = NULL;
|
|
|
|
|
2012-03-22 18:05:32 -04:00
|
|
|
verbosestream<<"Executing '"<<argv[0]<<"' '"<<argv[1]<<"' '"
|
2011-01-25 17:40:33 -05:00
|
|
|
<<argv[2]<<"'"<<std::endl;
|
|
|
|
|
|
|
|
execv(argv[0], argv);
|
|
|
|
|
|
|
|
// Execv shouldn't return. Failed.
|
2011-09-06 10:52:03 -04:00
|
|
|
_exit(1);
|
2011-01-25 17:40:33 -05:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
// Parent
|
|
|
|
int child_status;
|
|
|
|
pid_t tpid;
|
|
|
|
do{
|
|
|
|
tpid = wait(&child_status);
|
|
|
|
//if(tpid != child_pid) process_terminated(tpid);
|
|
|
|
}while(tpid != child_pid);
|
|
|
|
return (child_status == 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-25 17:21:38 -04:00
|
|
|
bool DeleteSingleFileOrEmptyDirectory(std::string path)
|
|
|
|
{
|
|
|
|
if(IsDir(path)){
|
|
|
|
bool did = (rmdir(path.c_str()) == 0);
|
|
|
|
if(!did)
|
|
|
|
errorstream<<"rmdir errno: "<<errno<<": "<<strerror(errno)
|
|
|
|
<<std::endl;
|
|
|
|
return did;
|
|
|
|
} else {
|
|
|
|
bool did = (unlink(path.c_str()) == 0);
|
|
|
|
if(!did)
|
|
|
|
errorstream<<"unlink errno: "<<errno<<": "<<strerror(errno)
|
|
|
|
<<std::endl;
|
|
|
|
return did;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-26 18:02:21 -05:00
|
|
|
#endif
|
|
|
|
|
2012-03-25 17:21:38 -04:00
|
|
|
void GetRecursiveSubPaths(std::string path, std::vector<std::string> &dst)
|
|
|
|
{
|
|
|
|
std::vector<DirListNode> content = GetDirListing(path);
|
|
|
|
for(unsigned int i=0; i<content.size(); i++){
|
|
|
|
const DirListNode &n = content[i];
|
|
|
|
std::string fullpath = path + DIR_DELIM + n.name;
|
|
|
|
dst.push_back(fullpath);
|
|
|
|
GetRecursiveSubPaths(fullpath, dst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool DeletePaths(const std::vector<std::string> &paths)
|
|
|
|
{
|
|
|
|
bool success = true;
|
|
|
|
// Go backwards to succesfully delete the output of GetRecursiveSubPaths
|
|
|
|
for(int i=paths.size()-1; i>=0; i--){
|
|
|
|
const std::string &path = paths[i];
|
|
|
|
bool did = DeleteSingleFileOrEmptyDirectory(path);
|
|
|
|
if(!did){
|
|
|
|
errorstream<<"Failed to delete "<<path<<std::endl;
|
|
|
|
success = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
2011-01-25 17:40:33 -05:00
|
|
|
bool RecursiveDeleteContent(std::string path)
|
|
|
|
{
|
2012-03-22 18:05:32 -04:00
|
|
|
infostream<<"Removing content of \""<<path<<"\""<<std::endl;
|
2011-01-25 17:40:33 -05:00
|
|
|
std::vector<DirListNode> list = GetDirListing(path);
|
|
|
|
for(unsigned int i=0; i<list.size(); i++)
|
|
|
|
{
|
2011-01-25 17:49:32 -05:00
|
|
|
if(trim(list[i].name) == "." || trim(list[i].name) == "..")
|
|
|
|
continue;
|
2011-10-16 09:16:47 -04:00
|
|
|
std::string childpath = path + DIR_DELIM + list[i].name;
|
2011-01-25 17:40:33 -05:00
|
|
|
bool r = RecursiveDelete(childpath);
|
|
|
|
if(r == false)
|
|
|
|
{
|
2012-03-22 18:05:32 -04:00
|
|
|
errorstream<<"Removing \""<<childpath<<"\" failed"<<std::endl;
|
2011-01-25 17:40:33 -05:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-05-18 11:48:27 -04:00
|
|
|
bool CreateAllDirs(std::string path)
|
|
|
|
{
|
|
|
|
|
|
|
|
size_t pos;
|
|
|
|
std::vector<std::string> tocreate;
|
|
|
|
std::string basepath = path;
|
|
|
|
while(!PathExists(basepath))
|
|
|
|
{
|
|
|
|
tocreate.push_back(basepath);
|
2011-10-16 09:16:47 -04:00
|
|
|
pos = basepath.rfind(DIR_DELIM_C);
|
2011-05-18 11:48:27 -04:00
|
|
|
if(pos == std::string::npos)
|
2012-03-11 10:06:32 -04:00
|
|
|
break;
|
2011-05-18 11:48:27 -04:00
|
|
|
basepath = basepath.substr(0,pos);
|
|
|
|
}
|
|
|
|
for(int i=tocreate.size()-1;i>=0;i--)
|
2012-03-11 10:06:32 -04:00
|
|
|
if(!CreateDir(tocreate[i]))
|
|
|
|
return false;
|
2011-05-18 11:48:27 -04:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2011-01-07 07:52:27 -05:00
|
|
|
} // namespace fs
|
2010-11-26 18:02:21 -05:00
|
|
|
|