use filesystem for fs stuff.

This commit is contained in:
Kelvin Sherlock 2016-11-11 12:01:06 -05:00
parent 9eaff93985
commit 9501601d7a
4 changed files with 52 additions and 82 deletions

View File

@ -2,8 +2,11 @@
#include <algorithm> #include <algorithm>
#include <cassert> #include <cassert>
#include <unistd.h>
#include <sys/param.h>
#include <filesystem>
namespace fs = std::experimental::filesystem;
namespace OS { namespace OS {
@ -31,13 +34,12 @@ namespace OS {
int32_t FSSpecManager::IDForCWD() int32_t FSSpecManager::IDForCWD()
{ {
char buffer[MAXPATHLEN];
char *cp;
cp = getcwd(buffer, sizeof(buffer)); std::error_code ec;
if (cp < 0) return 0; fs::path p = fs::current_path(ec);
if (ec) return 0;
std::string path(cp); std::string path(p.generic_u8string());
return FSSpecManager::IDForPath(std::move(path), true); return FSSpecManager::IDForPath(std::move(path), true);
} }

View File

@ -39,6 +39,10 @@
#include <strings.h> #include <strings.h>
#include <filesystem>
namespace fs = std::experimental::filesystem;
#include <cpu/defs.h> #include <cpu/defs.h>
#include <cpu/CpuModule.h> #include <cpu/CpuModule.h>
#include <cpu/fmem.h> #include <cpu/fmem.h>
@ -465,8 +469,6 @@ namespace OS
_ioDirID = 48, _ioDirID = 48,
}; };
struct stat st;
bool htrap = trap & 0x0200; bool htrap = trap & 0x0200;
const char *func = htrap ? "HDelete" : "Delete"; const char *func = htrap ? "HDelete" : "Delete";
@ -494,21 +496,10 @@ namespace OS
Log(" %s(%s)\n", func, sname.c_str()); Log(" %s(%s)\n", func, sname.c_str());
int ok; std::error_code ec;
if (fs::remove(sname, ec)) d0 = 0;
ok = ::lstat(sname.c_str(), &st); else if (ec) d0 = macos_error_from_errno(ec);
if (ok == 0) else d0 = MacOS::fnfErr;
{
if (S_ISDIR(st.st_mode))
ok = ::rmdir(sname.c_str());
else
ok = ::unlink(sname.c_str());
}
if (ok < 0)
d0 = macos_error_from_errno();
else
d0 = 0;
memoryWriteWord(d0, parm + _ioResult); memoryWriteWord(d0, parm + _ioResult);
return d0; return d0;

View File

@ -33,13 +33,14 @@
#include <deque> #include <deque>
#include <string> #include <string>
#include <cstring> #include <cstring>
#include <vector>
#include <sys/stat.h> #include <sys/stat.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h> #include <unistd.h>
#include <fcntl.h> #include <fcntl.h>
#include <dirent.h>
#include <sys/param.h> #include <filesystem>
#include <strings.h> #include <strings.h>
@ -63,6 +64,8 @@ using ToolBox::Log;
using MacOS::macos_error_from_errno; using MacOS::macos_error_from_errno;
namespace fs = std::experimental::filesystem;
namespace OS { namespace OS {
uint16_t PBGetWDInfo(uint32_t parm) uint16_t PBGetWDInfo(uint32_t parm)
@ -328,11 +331,8 @@ namespace OS {
std::string sname = FSSpecManager::PathForID(ioDirID); std::string sname = FSSpecManager::PathForID(ioDirID);
if (sname.empty()) { if (sname.empty()) {
char buffer[MAXPATHLEN]; auto path = fs::current_path();
char *cp; sname = path.empty() ? "." : path.generic_u8string();
cp = getcwd(buffer, sizeof(buffer));
sname = cp ? cp : ".";
} }
d0 = CatInfoByName(sname, parm); d0 = CatInfoByName(sname, parm);
@ -366,46 +366,26 @@ namespace OS {
sname = OS::realpath(sname); sname = OS::realpath(sname);
// if sname == "", error... // if sname == "", error...
DIR *dp;
struct dirent *dir;
dp = opendir(sname.c_str()); fs::directory_iterator dir(sname);
if (!dp) { std::vector<fs::path> names(fs::begin(dir), fs::end(dir));
d0 = macos_error_from_errno();
if (ioFDirIndex >= names.size()) {
d0 = MacOS::fnfErr; // end of dir error?
memoryWriteWord(d0, parm + _ioResult); memoryWriteWord(d0, parm + _ioResult);
return d0; return d0;
} }
while ((dir = readdir(dp))) { // alphabetical order...
if (dir->d_name[0] == '.') { std::sort(names.begin(), names.end());
if (!strcmp(dir->d_name, ".")) continue;
if (!strcmp(dir->d_name, "..")) continue;
}
#ifdef HAVE_DIRENT_D_NAMLEN
if (dir->d_namlen > 255) continue; // too long!
#else
if (strlen(dir->d_name) > 255) continue;
#endif
if (--ioFDirIndex == 0) break;
}
if (!dir) { auto e = names[ioFDirIndex - 1];
closedir(dp);
d0 = MacOS::fnfErr;
memoryWriteWord(d0, parm + _ioResult);
return d0;
}
if (ioNamePtr) { if (ioNamePtr) {
ToolBox::WritePString(ioNamePtr, dir->d_name); ToolBox::WritePString(ioNamePtr, e.filename().generic_u8string());
} }
sname.push_back('/'); d0 = CatInfoByName(e.generic_u8string(), parm);
sname.append(dir->d_name);
closedir(dp);
d0 = CatInfoByName(sname, parm);
} }
memoryWriteWord(d0, parm + _ioResult); memoryWriteWord(d0, parm + _ioResult);

View File

@ -43,6 +43,10 @@
#include <strings.h> #include <strings.h>
#include <filesystem>
namespace fs = std::experimental::filesystem;
#include <cpu/defs.h> #include <cpu/defs.h>
#include <cpu/CpuModule.h> #include <cpu/CpuModule.h>
#include <cpu/fmem.h> #include <cpu/fmem.h>
@ -81,6 +85,10 @@ namespace OS {
// MacOS: -> { -1, 1, "MacOS" } // MacOS: -> { -1, 1, "MacOS" }
// MacOS:dumper -> {-1, 2 "dumper"} // MacOS:dumper -> {-1, 2 "dumper"}
#ifndef PATH_MAX
#define PATH_MAX 4096
#endif
std::string realpath(const std::string &path) std::string realpath(const std::string &path)
{ {
char buffer[PATH_MAX + 1]; char buffer[PATH_MAX + 1];
@ -340,7 +348,6 @@ namespace OS {
*/ */
uint32_t spec; uint32_t spec;
struct stat st;
StackFrame<4>(spec); StackFrame<4>(spec);
@ -348,20 +355,11 @@ namespace OS {
Log(" FSpDelete(%s)\n", sname.c_str()); Log(" FSpDelete(%s)\n", sname.c_str());
std::error_code ec;
if (::lstat(sname.c_str(), &st) < 0) if (fs::remove(sname, ec)) return 0;
return macos_error_from_errno(); if (ec) return macos_error_from_errno(ec);
return MacOS::fnfErr;
int ok = 0;
if (S_ISDIR(st.st_mode))
ok = ::rmdir(sname.c_str());
else
ok = ::unlink(sname.c_str());
if (ok < 0)
return macos_error_from_errno();
return 0;
} }
@ -388,20 +386,19 @@ namespace OS {
Log(" ResolveAliasFile(%s)\n", path.c_str()); Log(" ResolveAliasFile(%s)\n", path.c_str());
struct stat st;
int rv;
rv = ::stat(path.c_str(), &st); std::error_code ec;
if (rv < 0) fs::file_status st = fs::status(path, ec);
{
if (ec) {
if (wasAliased) memoryWriteWord(0, wasAliased); if (wasAliased) memoryWriteWord(0, wasAliased);
if (targetIsFolder) memoryWriteWord(0, targetIsFolder); if (targetIsFolder) memoryWriteWord(0, targetIsFolder);
return macos_error_from_errno(); return macos_error_from_errno(ec);
} }
if (targetIsFolder) if (targetIsFolder)
memoryWriteWord(S_ISDIR(st.st_mode) ? 1 : 0, targetIsFolder); memoryWriteWord(fs::is_directory(st) ? 1 : 0, targetIsFolder);
// don't bother pretending a soft link is an alias. // don't bother pretending a soft link is an alias.
if (wasAliased) memoryWriteWord(0, wasAliased); if (wasAliased) memoryWriteWord(0, wasAliased);