GNU Octave  3.8.0
A high-level interpreted language, primarily intended for numerical computations, mostly compatible with Matlab
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
file-ops.cc
Go to the documentation of this file.
1 /*
2 
3 Copyright (C) 1996-2013 John W. Eaton
4 
5 This file is part of Octave.
6 
7 Octave is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 3 of the License, or (at your
10 option) any later version.
11 
12 Octave is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15 for more details.
16 
17 You should have received a copy of the GNU General Public License
18 along with Octave; see the file COPYING. If not, see
19 <http://www.gnu.org/licenses/>.
20 
21 */
22 
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26 
27 #include <cerrno>
28 #include <cstdio>
29 #include <cstdlib>
30 #include <cstring>
31 
32 #include <iostream>
33 #include <vector>
34 
35 #include <sys/stat.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 
39 #include "pathmax.h"
40 #include "canonicalize.h"
41 
42 #include "dir-ops.h"
43 #include "file-ops.h"
44 #include "file-stat.h"
45 #include "oct-env.h"
46 #include "oct-locbuf.h"
47 #include "oct-passwd.h"
48 #include "pathlen.h"
49 #include "quit.h"
50 #include "singleton-cleanup.h"
51 #include "str-vec.h"
52 
54 
55 bool
57 {
58  bool retval = true;
59 
60  if (! instance)
61  {
62 #if (defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM) && ! defined (OCTAVE_HAVE_POSIX_FILESYSTEM))
63  char system_dir_sep_char = '\\';
64  std::string system_dir_sep_str = "\\";
65 #else
66  char system_dir_sep_char = '/';
67  std::string system_dir_sep_str = "/";
68 #endif
69 #if defined (OCTAVE_HAVE_WINDOWS_FILESYSTEM)
70  std::string system_dir_sep_chars = "/\\";
71 #else
72  std::string system_dir_sep_chars = system_dir_sep_str;
73 #endif
74 
75  instance = new file_ops (system_dir_sep_char, system_dir_sep_str,
76  system_dir_sep_chars);
77 
78  if (instance)
80  }
81 
82  if (! instance)
83  {
84  (*current_liboctave_error_handler)
85  ("unable to create file_ops object!");
86 
87  retval = false;
88  }
89 
90  return retval;
91 }
92 
93 // The following tilde-expansion code was stolen and adapted from
94 // readline.
95 
96 // The default value of tilde_additional_prefixes. This is set to
97 // whitespace preceding a tilde so that simple programs which do not
98 // perform any word separation get desired behaviour.
99 static const char *default_prefixes[] = { " ~", "\t~", ":~", 0 };
100 
101 // The default value of tilde_additional_suffixes. This is set to
102 // whitespace or newline so that simple programs which do not perform
103 // any word separation get desired behaviour.
104 static const char *default_suffixes[] = { " ", "\n", ":", 0 };
105 
106 // If non-null, this contains the address of a function that the
107 // application wants called before trying the standard tilde
108 // expansions. The function is called with the text sans tilde, and
109 // returns a malloc()'ed string which is the expansion, or a NULL
110 // pointer if the expansion fails.
112 
113 // If non-null, this contains the address of a function to call if the
114 // standard meaning for expanding a tilde fails. The function is
115 // called with the text (sans tilde, as in "foo"), and returns a
116 // malloc()'ed string which is the expansion, or a NULL pointer if
117 // there is no expansion.
119 
120 // When non-null, this is a NULL terminated array of strings which are
121 // duplicates for a tilde prefix. Bash uses this to expand '=~' and
122 // ':~'.
124 
125 // When non-null, this is a NULL terminated array of strings which
126 // match the end of a username, instead of just "/". Bash sets this
127 // to ':' and '=~'.
129 
130 // Find the start of a tilde expansion in S, and return the index
131 // of the tilde which starts the expansion. Place the length of the
132 // text which identified this tilde starter in LEN, excluding the
133 // tilde itself.
134 
135 static size_t
136 tilde_find_prefix (const std::string& s, size_t& len)
137 {
138  len = 0;
139 
140  size_t s_len = s.length ();
141 
142  if (s_len == 0 || s[0] == '~')
143  return 0;
144 
146 
147  if (! prefixes.empty ())
148  {
149  for (size_t i = 0; i < s_len; i++)
150  {
151  for (int j = 0; j < prefixes.length (); j++)
152  {
153  size_t pfx_len = prefixes[j].length ();
154 
155  if (prefixes[j].compare (s.substr (i, pfx_len)) == 0)
156  {
157  len = pfx_len - 1;
158  return i + len;
159  }
160  }
161  }
162  }
163 
164  return s_len;
165 }
166 
167 // Find the end of a tilde expansion in S, and return the index
168 // of the character which ends the tilde definition.
169 
170 static size_t
171 tilde_find_suffix (const std::string& s)
172 {
173  size_t s_len = s.length ();
174 
176 
177  size_t i = 0;
178 
179  for ( ; i < s_len; i++)
180  {
181  if (file_ops::is_dir_sep (s[i]))
182  break;
183 
184  if (! suffixes.empty ())
185  {
186  for (int j = 0; j < suffixes.length (); j++)
187  {
188  size_t sfx_len = suffixes[j].length ();
189 
190  if (suffixes[j].compare (s.substr (i, sfx_len)) == 0)
191  return i;
192  }
193  }
194  }
195 
196  return i;
197 }
198 
199 // Take FNAME and return the tilde prefix we want expanded.
200 
201 static std::string
202 isolate_tilde_prefix (const std::string& fname)
203 {
204  size_t f_len = fname.length ();
205 
206  size_t len = 1;
207 
208  while (len < f_len && ! file_ops::is_dir_sep (fname[len]))
209  len++;
210 
211  return fname.substr (1, len);
212 }
213 
214 // Do the work of tilde expansion on FILENAME. FILENAME starts with a
215 // tilde.
216 
217 static std::string
218 tilde_expand_word (const std::string& filename)
219 {
220  size_t f_len = filename.length ();
221 
222  if (f_len == 0 || filename[0] != '~')
223  return filename;
224 
225  // A leading '~/' or a bare '~' is *always* translated to the value
226  // of $HOME or the home directory of the current user, regardless of
227  // any preexpansion hook.
228 
229  if (f_len == 1 || file_ops::is_dir_sep (filename[1]))
230  return octave_env::get_home_directory () + filename.substr (1);
231 
232  std::string username = isolate_tilde_prefix (filename);
233 
234  size_t user_len = username.length ();
235 
236  std::string dirname;
237 
239  {
240  std::string expansion
242 
243  if (! expansion.empty ())
244  return expansion + filename.substr (user_len+1);
245  }
246 
247  // No preexpansion hook, or the preexpansion hook failed. Look in the
248  // password database.
249 
250  octave_passwd pw = octave_passwd::getpwnam (username);
251 
252  if (! pw)
253  {
254  // If the calling program has a special syntax for expanding tildes,
255  // and we couldn't find a standard expansion, then let them try.
256 
258  {
259  std::string expansion
261 
262  if (! expansion.empty ())
263  dirname = expansion + filename.substr (user_len+1);
264  }
265 
266  // If we don't have a failure hook, or if the failure hook did not
267  // expand the tilde, return a copy of what we were passed.
268 
269  if (dirname.length () == 0)
270  dirname = filename;
271  }
272  else
273  dirname = pw.dir () + filename.substr (user_len+1);
274 
275  return dirname;
276 }
277 
278 // If NAME has a leading ~ or ~user, Unix-style, expand it to the
279 // user's home directory. If no ~, or no <pwd.h>, just return NAME.
280 
281 std::string
282 file_ops::tilde_expand (const std::string& name)
283 {
284  if (name.find ('~') == std::string::npos)
285  return name;
286  else
287  {
288  std::string result;
289 
290  size_t name_len = name.length ();
291 
292  // Scan through S expanding tildes as we come to them.
293 
294  size_t pos = 0;
295 
296  while (1)
297  {
298  if (pos > name_len)
299  break;
300 
301  size_t len;
302 
303  // Make START point to the tilde which starts the expansion.
304 
305  size_t start = tilde_find_prefix (name.substr (pos), len);
306 
307  result.append (name.substr (pos, start));
308 
309  // Advance STRING to the starting tilde.
310 
311  pos += start;
312 
313  // Make FINI be the index of one after the last character of the
314  // username.
315 
316  size_t fini = tilde_find_suffix (name.substr (pos));
317 
318  // If both START and FINI are zero, we are all done.
319 
320  if (! (start || fini))
321  break;
322 
323  // Expand the entire tilde word, and copy it into RESULT.
324 
325  std::string tilde_word = name.substr (pos, fini);
326 
327  pos += fini;
328 
329  std::string expansion = tilde_expand_word (tilde_word);
330 
331  result.append (expansion);
332  }
333 
334  return result;
335  }
336 }
337 
338 // A vector version of the above.
339 
342 {
343  string_vector retval;
344 
345  int n = names.length ();
346 
347  retval.resize (n);
348 
349  for (int i = 0; i < n; i++)
350  retval[i] = tilde_expand (names[i]);
351 
352  return retval;
353 }
354 
355 std::string
356 file_ops::concat (const std::string& dir, const std::string& file)
357 {
358  return dir.empty ()
359  ? file
360  : (is_dir_sep (dir[dir.length ()-1])
361  ? dir + file
362  : dir + dir_sep_char () + file);
363 }
364 
365 
366 int
367 octave_mkdir (const std::string& nm, mode_t md)
368 {
369  std::string msg;
370  return octave_mkdir (nm, md, msg);
371 }
372 
373 int
374 octave_mkdir (const std::string& name, mode_t mode, std::string& msg)
375 {
376  msg = std::string ();
377 
378  int status = -1;
379 
380  status = gnulib::mkdir (name.c_str (), mode);
381 
382  if (status < 0)
383  msg = gnulib::strerror (errno);
384 
385  return status;
386 }
387 
388 int
389 octave_mkfifo (const std::string& nm, mode_t md)
390 {
391  std::string msg;
392  return octave_mkfifo (nm, md, msg);
393 }
394 
395 int
396 octave_mkfifo (const std::string& name, mode_t mode, std::string& msg)
397 {
398  msg = std::string ();
399 
400  int status = -1;
401 
402  // With gnulib we will always have mkfifo, but some systems like MinGW
403  // don't have working mkfifo functions. On those systems, mkfifo will
404  // always return -1 and set errno.
405 
406  status = gnulib::mkfifo (name.c_str (), mode);
407 
408  if (status < 0)
409  msg = gnulib::strerror (errno);
410 
411  return status;
412 }
413 
414 int
415 octave_link (const std::string& old_name, const std::string& new_name)
416 {
417  std::string msg;
418  return octave_link (old_name, new_name, msg);
419 }
420 
421 int
422 octave_link (const std::string& old_name,
423  const std::string& new_name, std::string& msg)
424 {
425  msg = std::string ();
426 
427  int status = -1;
428 
429  status = gnulib::link (old_name.c_str (), new_name.c_str ());
430 
431  if (status < 0)
432  msg = gnulib::strerror (errno);
433 
434  return status;
435 }
436 
437 int
438 octave_symlink (const std::string& old_name, const std::string& new_name)
439 {
440  std::string msg;
441  return octave_symlink (old_name, new_name, msg);
442 }
443 
444 int
445 octave_symlink (const std::string& old_name,
446  const std::string& new_name, std::string& msg)
447 {
448  msg = std::string ();
449 
450  int status = -1;
451 
452  status = gnulib::symlink (old_name.c_str (), new_name.c_str ());
453 
454  if (status < 0)
455  msg = gnulib::strerror (errno);
456 
457  return status;
458 }
459 
460 int
461 octave_readlink (const std::string& path, std::string& result)
462 {
463  std::string msg;
464  return octave_readlink (path, result, msg);
465 }
466 
467 int
468 octave_readlink (const std::string& path, std::string& result,
469  std::string& msg)
470 {
471  int status = -1;
472 
473  msg = std::string ();
474 
475  char buf[MAXPATHLEN+1];
476 
477  status = gnulib::readlink (path.c_str (), buf, MAXPATHLEN);
478 
479  if (status < 0)
480  msg = gnulib::strerror (errno);
481  else
482  {
483  buf[status] = '\0';
484  result = std::string (buf);
485  status = 0;
486  }
487 
488  return status;
489 }
490 
491 int
492 octave_rename (const std::string& from, const std::string& to)
493 {
494  std::string msg;
495  return octave_rename (from, to, msg);
496 }
497 
498 int
499 octave_rename (const std::string& from, const std::string& to,
500  std::string& msg)
501 {
502  int status = -1;
503 
504  msg = std::string ();
505 
506  status = gnulib::rename (from.c_str (), to.c_str ());
507 
508  if (status < 0)
509  msg = gnulib::strerror (errno);
510 
511  return status;
512 }
513 
514 int
515 octave_rmdir (const std::string& name)
516 {
517  std::string msg;
518  return octave_rmdir (name, msg);
519 }
520 
521 int
522 octave_rmdir (const std::string& name, std::string& msg)
523 {
524  msg = std::string ();
525 
526  int status = -1;
527 
528  status = gnulib::rmdir (name.c_str ());
529 
530  if (status < 0)
531  msg = gnulib::strerror (errno);
532 
533  return status;
534 }
535 
536 // And a version that works recursively.
537 
538 int
539 octave_recursive_rmdir (const std::string& name)
540 {
541  std::string msg;
542  return octave_recursive_rmdir (name, msg);
543 }
544 
545 int
546 octave_recursive_rmdir (const std::string& name, std::string& msg)
547 {
548  msg = std::string ();
549 
550  int status = 0;
551 
552  dir_entry dir (name);
553 
554  if (dir)
555  {
556  string_vector dirlist = dir.read ();
557 
558  for (octave_idx_type i = 0; i < dirlist.length (); i++)
559  {
560  octave_quit ();
561 
562  std::string nm = dirlist[i];
563 
564  // Skip current directory and parent.
565  if (nm == "." || nm == "..")
566  continue;
567 
568  std::string fullnm = name + file_ops::dir_sep_str () + nm;
569 
570  // Get info about the file. Don't follow links.
571  file_stat fs (fullnm, false);
572 
573  if (fs)
574  {
575  if (fs.is_dir ())
576  {
577  status = octave_recursive_rmdir (fullnm, msg);
578 
579  if (status < 0)
580  break;
581  }
582  else
583  {
584  status = octave_unlink (fullnm, msg);
585 
586  if (status < 0)
587  break;
588  }
589  }
590  else
591  {
592  msg = fs.error ();
593  break;
594  }
595  }
596 
597  if (status >= 0)
598  {
599  dir.close ();
600  status = octave_rmdir (name, msg);
601  }
602  }
603  else
604  {
605  status = -1;
606 
607  msg = dir.error ();
608  }
609 
610  return status;
611 }
612 
613 int
615 {
616 #if defined (HAVE_UMASK)
617  return umask (mode);
618 #else
619  return 0;
620 #endif
621 }
622 
623 int
624 octave_unlink (const std::string& name)
625 {
626  std::string msg;
627  return octave_unlink (name, msg);
628 }
629 
630 int
631 octave_unlink (const std::string& name, std::string& msg)
632 {
633  msg = std::string ();
634 
635  int status = -1;
636 
637  status = gnulib::unlink (name.c_str ());
638 
639  if (status < 0)
640  msg = gnulib::strerror (errno);
641 
642  return status;
643 }
644 
645 std::string
646 octave_tempnam (const std::string& dir, const std::string& pfx)
647 {
648  std::string msg;
649  return octave_tempnam (dir, pfx, msg);
650 }
651 
652 std::string
653 octave_tempnam (const std::string& dir, const std::string& pfx,
654  std::string& msg)
655 {
656  msg = std::string ();
657 
658  std::string retval;
659 
660  const char *pdir = dir.empty () ? 0 : dir.c_str ();
661 
662  const char *ppfx = pfx.empty () ? 0 : pfx.c_str ();
663 
664  char *tmp = tempnam (pdir, ppfx);
665 
666  if (tmp)
667  {
668  retval = tmp;
669 
670  free (tmp);
671  }
672  else
673  msg = gnulib::strerror (errno);
674 
675  return retval;
676 }
677 
678 std::string
679 octave_canonicalize_file_name (const std::string& name)
680 {
681  std::string msg;
682  return octave_canonicalize_file_name (name, msg);
683 }
684 
685 std::string
686 octave_canonicalize_file_name (const std::string& name, std::string& msg)
687 {
688  msg = std::string ();
689 
690  std::string retval;
691 
692 #if defined (HAVE_CANONICALIZE_FILE_NAME)
693 
694  char *tmp = gnulib::canonicalize_file_name (name.c_str ());
695 
696  if (tmp)
697  {
698  retval = tmp;
699  free (tmp);
700  }
701 
702 #elif defined (HAVE_RESOLVEPATH)
703 
704 #if !defined (errno)
705 extern int errno;
706 #endif
707 
708 #if !defined (__set_errno)
709 # define __set_errno(Val) errno = (Val)
710 #endif
711 
712  if (name.empty ())
713  {
714  __set_errno (ENOENT);
715  return retval;
716  }
717 
718  // All known hosts with resolvepath (e.g. Solaris 7) don't turn
719  // relative names into absolute ones, so prepend the working
720  // directory if the path is not absolute.
721 
722  std::string absolute_name = octave_env::make_absolute (name);
723 
724  size_t resolved_size = absolute_name.length ();
725 
726  while (true)
727  {
728  resolved_size = 2 * resolved_size + 1;
729 
730  OCTAVE_LOCAL_BUFFER (char, resolved, resolved_size);
731 
732  int resolved_len
733  = resolvepath (absolute_name.c_str (), resolved, resolved_size);
734 
735  if (resolved_len < 0)
736  break;
737 
738  if (resolved_len < resolved_size)
739  {
740  retval = resolved;
741  break;
742  }
743  }
744 
745 #elif defined (__WIN32__)
746 
747  int n = 1024;
748 
749  std::string win_path (n, '\0');
750 
751  while (true)
752  {
753  int status = GetFullPathName (name.c_str (), n, &win_path[0], 0);
754 
755  if (status == 0)
756  break;
757  else if (status < n)
758  {
759  win_path.resize (status);
760  retval = win_path;
761  break;
762  }
763  else
764  {
765  n *= 2;
766  win_path.resize (n);
767  }
768  }
769 
770 #elif defined (HAVE_REALPATH)
771 
772 #if !defined (__set_errno)
773 # define __set_errno(Val) errno = (Val)
774 #endif
775 
776  if (name.empty ())
777  {
778  __set_errno (ENOENT);
779  return retval;
780  }
781 
782  OCTAVE_LOCAL_BUFFER (char, buf, PATH_MAX);
783 
784  if (::realpath (name.c_str (), buf))
785  retval = buf;
786 
787 #else
788 
789  // FIXME: provide replacement here...
790  retval = name;
791 
792 #endif
793 
794  if (retval.empty ())
795  msg = gnulib::strerror (errno);
796 
797  return retval;
798 }
799