octave-maintainers
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

use of system to copy files in pkg.m


From: John W. Eaton
Subject: use of system to copy files in pkg.m
Date: Tue, 10 Oct 2006 13:55:47 -0400

We currently have the following eleven uses of system in pkg.m:

  [status, output] = system(["cd " src " ;./configure --prefix=" desc.dir]);
  [status, output] = system(["export INSTALLDIR=" desc.dir "; make -C " src]);
  [status, output] = system(["cd " src "; cp " filenames " " packdir "inst/"]);
  [status, output] = system(["cp -R " f " " packdir "inst/"]);
  [status, output] = system(["cp -R " packdir "inst/* " desc.dir]);
  [status, output] = system(["cp " packdir "DESCRIPTION " packinfo]);
  [status, output] = system(["cp " packdir "COPYING " packinfo]);
  [status, output] = system(["cp " packdir "INDEX " packinfo]);
  [status, output] = system(["cp " packdir "on_uninstall.m " packinfo]);
  [status, output] = system(["cp -pR " packdir "doc " desc.dir]);
  [status, output] = system(["cp -pR " packdir "bin " desc.dir]);

Of these, nine are for copying files.  I think we should at least
encapsulate this in a separate function.  Matlab has copyfile.  I have
the following version of copyfile for Octave that also uses system
(I've omitted the copyright and help text here):

  function [status, msg, msgid] = copyfile (f1, f2, force)

    status = true;
    msg = "";
    msgid = "";

    if (nargin == 2 || nargin == 3)
      if (nargin == 3 && strcmp (force, "f"))
        cmd = "/bin/cp -rf";
      else
        cmd = "/bin/cp -r";
      endif
      [err, msg] = system (sprintf ("%s %s %s", cmd, f1, f2));
      if (err < 0)
        status = false;
        msgid = "copyfile";
      endif
    else
      print_usage ();
    endif

  endfunction

This doesn't support the "-p" option, but is that necessary?  It seems
that preserving ownership will only work if root is running the pkg
command anyway, so I'm not sure why we need to worry about that.  If
you need to ensure that the files can be read by all, then can we just
chmod the files as needed after copying?

Implementing copyfile this way is not ideal, especially for Windows
systems, but it seemed to complex to do a good job of implementing it
without using an external program.  The code for copying files from the
GNU file utiltilies is not easily extracted, and the core
functionality of the file utilities does not seem to be available as a
C-callable library.

I also have movefile:

  function [status, msg, msgid] = movefile (f1, f2, force)

    status = true;
    msg = "";
    msgid = "";

    if (nargin == 2 || nargin == 3)
      if (nargin == 3 && strcmp (force, "f"))
        cmd = "/bin/mv -f";
      else
        cmd = "/bin/mv";
      endif
      [err, msg] = system (sprintf ("%s %s %s", cmd, f1, f2));
      if (err < 0)
        status = false;
        msgid = "movefile";
      endif
    else
      print_usage ();
    endif

  ###   status = true;
  ###   msg = "";
  ###   msgid = "movefile";
  ### 
  ###   if (nargin == 2)
  ### 
  ###     flist = glob (f1);
  ###     nfiles = numel (flist);
  ###     if (nfiles > 1)
  ###       [f2info, err, msg] = stat (f2);
  ###       if (err < 0)
  ###   status = false;
  ###       else
  ###   if (S_ISDIR (f2info.mode))
  ###     for i = 1:nfiles
  ###       [err, msg] = rename (flist{i}, f2);
  ###       if (err < 0)
  ###         status = false;
  ###         break;
  ###       endif
  ###     endfor
  ###   else
  ###     status = false;
  ###     msg = "when moving multiple files, destination must be a directory";
  ###   endif
  ###       endif
  ###     else
  ###       [err, msg] = rename (f1, f2);
  ###       if (err < 0)
  ###   status = false;
  ###   break;
  ###       endif
  ###     endif
  ### 
  ###   else
  ###     usage ("movefile (f1, f2)");
  ###   endif
  ### 
  ###   if (status)
  ###     msgid = "";
  ###   endif

  endfunction

As you can see, I considered implementing this directly in Octave with
system calls, but then gave up and decided to go with a call to the
external mv program.

Should we include these functions with Octave now, more or less as is,
using system to call external programs?  Or is there something better
we can do?

If Windows provides functions to copy and move files, then I suppose
we could implement these in C++ with separate code for Windows (but
still using the external cp and mv programs to do the real work on
Unixy systems).

Comments?

jwe


reply via email to

[Prev in Thread] Current Thread [Next in Thread]