26 #if defined (HAVE_CONFIG_H)
37 #if defined (OCTAVE_USE_WINDOWS_API)
68 static const char *default_prefixes[] = {
" ~",
"\t~",
":~",
nullptr };
73 static const char *default_suffixes[] = {
" ",
"\n",
":",
nullptr };
76 tilde_find_prefix (
const std::string& s, std::size_t&
len)
80 std::size_t s_len = s.length ();
82 if (s_len == 0 || s[0] ==
'~')
87 if (! prefixes.
empty ())
89 for (std::size_t i = 0; i < s_len; i++)
91 for (
int j = 0; j < prefixes.
numel (); j++)
93 std::size_t pfx_len = prefixes[j].length ();
95 if (prefixes[j] == s.substr (i, pfx_len))
111 tilde_find_suffix (
const std::string& s)
113 std::size_t s_len = s.length ();
119 for ( ; i < s_len; i++)
124 if (! suffixes.
empty ())
126 for (
int j = 0; j < suffixes.
numel (); j++)
128 std::size_t sfx_len = suffixes[j].length ();
130 if (suffixes[j] == s.substr (i, sfx_len))
142 isolate_tilde_prefix (
const std::string& fname)
144 std::size_t f_len = fname.length ();
151 return fname.substr (1,
len);
158 tilde_expand_word (
const std::string& filename)
160 std::size_t f_len = filename.length ();
162 if (f_len == 0 || filename[0] !=
'~')
163 return std::string (filename);
170 return sys::env::get_home_directory () + filename.substr (1);
172 std::string username = isolate_tilde_prefix (filename);
174 std::size_t user_len = username.length ();
180 std::string expansion
183 if (! expansion.empty ())
184 return expansion + filename.substr (user_len+1);
190 sys::password pw = sys::password::getpwnam (username);
199 std::string expansion
202 if (! expansion.empty ())
203 dirname = expansion + filename.substr (user_len+1);
213 dirname = pw.dir () + filename.substr (user_len+1);
224 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
234 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
244 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
254 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
272 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
275 octave_unused_parameter (c);
285 return tmp.find (c) != std::string::npos;
291 if (name.find (
'~') == std::string::npos)
292 return std::string (name);
297 std::size_t name_len = name.length ();
312 std::size_t start = tilde_find_prefix (name.substr (pos),
len);
314 result.append (name.substr (pos, start));
323 std::size_t fini = tilde_find_suffix (name.substr (pos));
327 if (! (start || fini))
332 std::string tilde_word = name.substr (pos, fini);
336 std::string expansion = tilde_expand_word (tilde_word);
338 result.append (expansion);
352 for (
int i = 0; i <
n; i++)
359 concat (
const std::string& dir,
const std::string& file)
373 return (ipos != std::string::npos) ? path.substr (0, ipos) :
"";
381 if (ipos != std::string::npos)
386 return path.substr (ipos);
398 std::size_t
n = path.length ();
399 for (std::size_t i = 0; i <
n; i++)
411 OCTAVE_END_NAMESPACE(file_ops)
413 int
mkdir (const std::
string& nm, mode_t md)
416 return mkdir (nm, md, msg);
420 mkdir (
const std::string& name, mode_t mode, std::string& msg)
427 msg = std::strerror (errno);
445 #if defined (OCTAVE_USE_WINDOWS_API)
447 std::size_t skip_root = 0;
448 if (name.size () > 1)
470 while (delim != std::string::npos)
472 std::string base = name.substr (0, delim);
473 sys::file_stat fs (base);
476 status =
mkdir (base, mode, msg);
484 return mkdir (name, mode, msg);
488 mkfifo (
const std::string& nm, mode_t md)
491 return mkfifo (nm, md, msg);
495 mkfifo (
const std::string& name, mode_t mode, std::string& msg)
502 msg = std::strerror (errno);
508 link (
const std::string& old_name,
const std::string& new_name)
511 return link (old_name, new_name, msg);
515 link (
const std::string& old_name,
const std::string& new_name,
525 msg = std::strerror (errno);
531 symlink (
const std::string& old_name,
const std::string& new_name)
534 return symlink (old_name, new_name, msg);
538 symlink (
const std::string& old_name,
const std::string& new_name,
548 msg = std::strerror (errno);
554 readlink (
const std::string& path, std::string& result)
557 return readlink (path, result, msg);
561 readlink (
const std::string& path, std::string& result, std::string& msg)
570 msg = std::strerror (errno);
582 rename (
const std::string& from,
const std::string& to)
585 return rename (from, to, msg);
589 rename (
const std::string& from,
const std::string& to,
611 msg =
"Target already exists.";
616 #if defined (OCTAVE_USE_WINDOWS_API)
619 status = _wrename (wfrom.c_str (), wto.c_str ());
625 msg = std::strerror (errno);
634 return rmdir (name, msg);
638 rmdir (
const std::string& name, std::string& msg)
647 msg = std::strerror (errno);
676 std::string nm = dirlist[i];
679 if (nm ==
"." || nm ==
"..")
698 status =
unlink (fullnm, msg);
712 status =
rmdir (name, msg);
730 return unlink (name, msg);
734 unlink (
const std::string& name, std::string& msg)
743 msg = std::strerror (errno);
749 tempnam (
const std::string& dir,
const std::string& pfx)
752 return tempnam (dir, pfx, msg);
756 tempnam (
const std::string& dir,
const std::string& pfx,
764 std::string templatename;
767 else if (!
file_stat (dir,
false).is_dir ())
777 templatename +=
"file";
782 templatename +=
"XXXXXX";
787 strcpy (tname, templatename.c_str ());
790 msg = std::strerror (errno);
814 #if defined (OCTAVE_USE_WINDOWS_API)
817 HANDLE h_file = CreateFileW (wname.c_str (), GENERIC_READ,
818 FILE_SHARE_READ,
nullptr, OPEN_EXISTING,
819 FILE_FLAG_BACKUP_SEMANTICS,
nullptr);
825 if (h_file == INVALID_HANDLE_VALUE)
826 h_file = CreateFileW (wname.c_str (), GENERIC_READ,
827 FILE_SHARE_READ,
nullptr, OPEN_EXISTING,
828 FILE_FLAG_BACKUP_SEMANTICS
829 | FILE_FLAG_OPEN_REPARSE_POINT,
nullptr);
831 if (h_file == INVALID_HANDLE_VALUE)
833 msg =
"Unable to open file \"" + name +
"\"";
839 const std::size_t buf_size = 32767;
840 wchar_t buffer[buf_size] = L
"";
843 DWORD
len = GetFinalPathNameByHandleW (h_file, buffer, buf_size,
844 FILE_NAME_NORMALIZED);
847 msg =
"Error querying normalized name for \"" + name +
"\"";
856 if (retval.compare (0, 8, R
"(\\?\UNC\)") == 0)
858 retval = retval.erase (2, 6);
862 if (name.size () < 3 || name[1] !=
':')
866 std::wstring orig_map = wname.substr (0, 3);
868 HANDLE h_map = CreateFileW (orig_map.c_str (), GENERIC_READ,
869 FILE_SHARE_READ,
nullptr, OPEN_EXISTING,
870 FILE_FLAG_BACKUP_SEMANTICS
871 | FILE_FLAG_OPEN_REPARSE_POINT,
874 if (h_map == INVALID_HANDLE_VALUE)
879 len = GetFinalPathNameByHandleW (h_map, buffer, buf_size,
880 FILE_NAME_NORMALIZED);
882 std::string orig_root
885 if (orig_root.compare (0, 8, R
"(\\?\UNC\)"))
889 orig_root = orig_root.erase (2, 6);
892 while (file_seps.find (orig_root.back ()) != std::string::npos)
893 orig_root.pop_back ();
895 if (retval.compare (0, orig_root.size (), orig_root))
900 size_t sep_pos = orig_root.size ();
901 if (sep_pos != retval.size ())
902 retval = retval.substr (sep_pos-2);
905 retval[0] = std::toupper (name[0]);
908 else if (retval.compare (0, 4, R
"(\\?\)") == 0)
909 retval = retval.erase (0, 4);
920 msg = std::strerror (errno);
926 OCTAVE_END_NAMESPACE(sys)
927 OCTAVE_END_NAMESPACE(
octave)
char * octave_areadlink_wrapper(const char *name)
char * octave_canonicalize_file_name_wrapper(const char *name)
std::string error() const
static std::string get_temp_directory()
octave_idx_type numel() const
OCTAVE_BEGIN_NAMESPACE(octave) static octave_value daspk_fcn
std::string dir_sep_str()
std::string native_separator_path(const std::string &path)
string_vector tilde_additional_suffixes
std::string dir_sep_chars()
std::string dirname(const std::string &path)
tilde_expansion_hook tilde_expansion_failure_hook
std::string(* tilde_expansion_hook)(const std::string &)
tilde_expansion_hook tilde_expansion_preexpansion_hook
string_vector tilde_additional_prefixes
std::string tail(const std::string &path)
int octave_gen_tempname_wrapper(char *tmpl)
std::string u8_from_wstring(const std::wstring &wchar_string)
bool file_exists(const std::string &filename, bool is_dir)
std::wstring u8_to_wstring(const std::string &utf8_string)
bool get_dirlist(const std::string &dirname, string_vector &dirlist, std::string &msg)
bool same_file(const std::string &file1, const std::string &file2)
std::string concat(const std::string &dir, const std::string &file)
string_vector tilde_expand(const string_vector &names)
int symlink(const std::string &old_name, const std::string &new_name, std::string &msg)
std::string canonicalize_file_name(const std::string &name, std::string &msg)
int readlink(const std::string &path, std::string &result, std::string &msg)
int recursive_mkdir(const std::string &name, mode_t mode, std::string &msg)
int rename(const std::string &from, const std::string &to, std::string &msg)
int rmdir(const std::string &name, std::string &msg)
std::string tempnam(const std::string &dir, const std::string &pfx, std::string &msg)
int link(const std::string &old_name, const std::string &new_name, std::string &msg)
int mkdir(const std::string &name, mode_t mode, std::string &msg)
int mkfifo(const std::string &name, mode_t mode, std::string &msg)
int recursive_rmdir(const std::string &name, std::string &msg)
int unlink(const std::string &name, std::string &msg)
#define OCTAVE_LOCAL_BUFFER(T, buf, size)
int octave_mkdir_wrapper(const char *name, mode_t mode)
int octave_mkfifo_wrapper(const char *name, mode_t mode)
int octave_umask_wrapper(mode_t mode)
int octave_rmdir_wrapper(const char *nm)
int octave_unlink_wrapper(const char *nm)
int octave_symlink_wrapper(const char *nm1, const char *nm2)
int octave_link_wrapper(const char *nm1, const char *nm2)