[Midnightbsd-cvs] src [7089] trunk/lib/libmport: reimplement mport_copy_file to use C code rather than system() cp to improve performance.
laffer1 at midnightbsd.org
laffer1 at midnightbsd.org
Tue Jul 14 21:28:04 EDT 2015
Revision: 7089
http://svnweb.midnightbsd.org/src/?rev=7089
Author: laffer1
Date: 2015-07-14 21:28:03 -0400 (Tue, 14 Jul 2015)
Log Message:
-----------
reimplement mport_copy_file to use C code rather than system() cp to improve performance. Also, modify mport to create a copy of .sample files without the file extension if there isnt already a file. This is often used for config files and it helps the user get a default
Modified Paths:
--------------
trunk/lib/libmport/bundle_read_install_pkg.c
trunk/lib/libmport/util.c
Modified: trunk/lib/libmport/bundle_read_install_pkg.c
===================================================================
--- trunk/lib/libmport/bundle_read_install_pkg.c 2015-07-12 17:42:35 UTC (rev 7088)
+++ trunk/lib/libmport/bundle_read_install_pkg.c 2015-07-15 01:28:03 UTC (rev 7089)
@@ -230,6 +230,22 @@
if (chmod(file, newmode))
goto ERROR;
}
+
+ /* for sample files, if we don't have an existing file
+ make a new one */
+ if (type == ASSET_SAMPLE) {
+ char *nonSample = strdup(file);
+ if (nonSample == NULL)
+ goto ERROR;
+ char *sptr = strcasestr(file, ".sample");
+ if (sptr != NULL) {
+ sptr[0] = '\0'; /* hack off .sample */
+ if (!mport_file_exists(nonSample)) {
+ mport_copy_file(file, nonSample);
+ }
+ }
+ free(nonSample);
+ }
}
(mport->progress_step_cb)(++file_count, file_total, file);
Modified: trunk/lib/libmport/util.c
===================================================================
--- trunk/lib/libmport/util.c 2015-07-12 17:42:35 UTC (rev 7088)
+++ trunk/lib/libmport/util.c 2015-07-15 01:28:03 UTC (rev 7089)
@@ -180,7 +180,8 @@
/* deletes the entire directory tree at name.
* think rm -r filename
*/
-int mport_rmtree(const char *filename)
+int
+mport_rmtree(const char *filename)
{
return mport_xsystem(NULL, "/bin/rm -r %s", filename);
}
@@ -187,12 +188,30 @@
/*
- * Copy fromname to toname
- *
+ * Copy file fromname to toname
*/
-int mport_copy_file(const char *fromname, const char *toname)
+int
+mport_copy_file(const char *fromname, const char *toname)
{
- return mport_xsystem(NULL, "/bin/cp %s %s", fromname, toname);
+ char buf[BUFSIZ];
+ size_t size;
+
+ FILE *fsrc = fopen(fromname, "rb");
+ if (fsrc == NULL)
+ RETURN_ERRORX(MPORT_ERR_FATAL, "Couldn't open source file for copying %s: %s", fromname, strerror(errno));
+
+ FILE *fdest = fopen(toname, "wb");
+ if (fdest == NULL)
+ RETURN_ERRORX(MPORT_ERR_FATAL, "Couldn't open destination file for copying %s: %s", toname, strerror(errno));
+
+ while ((size = fread(buf, 1, BUFSIZ, fsrc)) > 0) {
+ fwrite(buf, 1, size, fdest);
+ }
+
+ fclose(fsrc);
+ fclose(fdest);
+
+ return MPORT_OK;
}
More information about the Midnightbsd-cvs
mailing list