trafficserver-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a..@apache.org
Subject [trafficserver] branch master updated: Runroot: add an option to specify copy style
Date Mon, 02 Jul 2018 17:38:39 GMT
This is an automated email from the ASF dual-hosted git repository.

amc pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/trafficserver.git


The following commit(s) were added to refs/heads/master by this push:
     new 58beea4  Runroot: add an option to specify copy style
58beea4 is described below

commit 58beea4fec0b84205d502e08fadd8b3d19eb2ec7
Author: Xavier Chi <chitianhaoxp@gmail.com>
AuthorDate: Fri Jun 29 10:30:19 2018 -0500

    Runroot: add an option to specify copy style
---
 doc/appendices/command-line/traffic_layout.en.rst | 94 +++++++++++++----------
 src/traffic_layout/engine.cc                      | 16 +++-
 src/traffic_layout/engine.h                       |  5 ++
 src/traffic_layout/file_system.cc                 | 24 ++++--
 src/traffic_layout/file_system.h                  |  5 +-
 5 files changed, 92 insertions(+), 52 deletions(-)

diff --git a/doc/appendices/command-line/traffic_layout.en.rst b/doc/appendices/command-line/traffic_layout.en.rst
index e9eca56..c236816 100644
--- a/doc/appendices/command-line/traffic_layout.en.rst
+++ b/doc/appendices/command-line/traffic_layout.en.rst
@@ -20,22 +20,12 @@
 traffic_layout
 **************
 
+========
 Synopsis
 ========
 :program:`traffic_layout` SUBCOMMAND [OPTIONS]
 
-Options
-=============
-.. program:: traffic_layout
-
-.. option:: --run-root=[<path>]
-
-   Use the run root file at :arg:`path`.
-
-.. option:: -V, --version
-
-    Print version information and exit.
-
+===========
 Environment
 ===========
 
@@ -43,8 +33,9 @@ Environment
 
    The path to the run root file. It has the same effect as the command line option :option:`--run-root`.
 
+===========
 Description
-=============
+===========
 Document for the special functionality of ``runroot`` inside :program:`traffic_layout`. This
feature
 is for the setup of traffic server runroot. It will create a runtime sandbox for any program
of
 traffic server to run under.
@@ -60,59 +51,78 @@ How it works
 #. Emit a yaml file that defines layout structure for other programs to use (relative path).
 #. Users are able to remove runroot and verify permission of the runroot.
 
+===========
 Subcommands
-=============
+===========
 
-- Initialize the runroot: ::
+``init``
+   Use the current working directory or the specific path to create runroot. 
+   The path can be relative or set up in :envvar:`TS_RUNROOT`.
 
-      traffic_layout init (--path /path/to/sandbox/)
+``remove``
+   Find the sandbox to remove in following order: 
+      #. specified in --path as absolute or relative.
+      #. ENV variable: :envvar:`TS_RUNROOT`
+      #. current working directory
+      #. installed directory.
+   
+``verify``
+   Verify the permission of the sandbox.
+
+=======
+Options
+=======
+.. program:: traffic_layout
 
-      Use the current working directory or the specific path to create runroot.
-      The path can be relative or set up in :envvar:`TS_RUNROOT`.
+.. option:: --run-root=[<path>]
 
+    Use the run root file at :arg:`path`.
+   
+.. option:: -V, --version
 
-- Remove the runroot: ::
+    Print version information and exit.
 
-      traffic_layout remove (--path /path/to/sandbox/)
+.. option:: -h, --help
 
-      Find the sandbox to remove in following order: 
+    Print usage information and exit.
 
-            1. specified in --path as absolute or relative.
-            2. :envvar:`TS_RUNROOT`
-            3. current working directory
-            4. installed directory.
+.. option:: --force
 
-- Verify the runroot: ::
+    Force init will create sandbox even if the directory is not empty.
+    Force remove will remove a directory even if directory has no yaml file.
 
-      traffic_layout verify (--path /path/to/sandbox/)
+.. option:: --absolute
 
-      Verify the permission of the sandbox.
+    Put directories in the yaml file in the form of absolute path when creating.
+    
+.. option:: --fix
 
-Subcommands options
--------------------
+    Fix the permission issues verify found. ``--fix`` requires root privilege (sudo).
 
-- Force option: ::
+.. option:: --copy-style=[HARD/SOFT/FULL]
 
-      traffic_layout init --force (--path /path/to/sandbox)
-      traffic_layout remove --force (--path /path/to/sandbox)
+    Specify the way of copying executables when creating runroot
 
-      Force init will create sandbox even if the directory is not empty.
-      Force remove will remove a directory even if directory has no yaml file.
+========
+Examples
+========
 
-- Absolute option: ::
+Initialize the runroot. ::
 
-      traffic_layout init --absolute (--path /path/to/sandbox)
+    traffic_layout init (--path /path/to/sandbox/) (--force) (--absolute) (--copy-style=[HARD/SOFT/FULL])
 
-      create the sandbox and put directories in the yaml file in the form of absolute path.
+Remove the runroot. ::
 
-- Fix option: ::
+    traffic_layout remove (--path /path/to/sandbox/) (--force)
 
-      traffic_layout verify --fix (--path /path/to/sandbox)
+Verify the runroot. ::
+    
+    traffic_layout verify (--path /path/to/sandbox/) (--fix)
 
-      Fix the permission issues verify found. ``--fix`` requires root privilege (sudo).
 
+=========================
 Usage for other programs:
-==============================================
+=========================
 
 All programs can find the runroot to use in the following order
 
diff --git a/src/traffic_layout/engine.cc b/src/traffic_layout/engine.cc
index 0e9a142..b6af4f6 100644
--- a/src/traffic_layout/engine.cc
+++ b/src/traffic_layout/engine.cc
@@ -36,6 +36,7 @@
 #include "file_system.h"
 #include "ts/runroot.h"
 
+#include <cctype>
 #include <fstream>
 #include <iostream>
 #include <ftw.h>
@@ -147,6 +148,7 @@ RunrootEngine::runroot_help_message(const bool runflag, const bool cleanflag,
co
                  "--force   Force to create ts_runroot even directory already exists\n"
                  "--absolute    Produce absolute path in the yaml file\n"
                  "--run-root(=/path)  Using specified TS_RUNROOT as sandbox\n"
+                 "--copy-style=[STYLE] Specify style (FULL, HARD, SOFT) when copying executable\n"
               << std::endl;
   }
   if (cleanflag) {
@@ -216,6 +218,18 @@ RunrootEngine::runroot_parse()
       fix_flag = true;
       continue;
     }
+    if (argument.substr(0, COPYSTYLE_WORD_LENGTH) == "--copy-style") {
+      std::string style = argument.substr(COPYSTYLE_WORD_LENGTH + 1);
+      transform(style.begin(), style.end(), style.begin(), ::tolower);
+      if (style == "full") {
+        copy_style = FULL;
+      } else if (style == "soft") {
+        copy_style = SOFT;
+      } else {
+        copy_style = HARD;
+      }
+      continue;
+    }
     if (argument == "--path") {
       if (i + 1 >= _argv.size() || _argv[i + 1][0] == '-') {
         // invalid path
@@ -463,7 +477,7 @@ RunrootEngine::copy_runroot(const std::string &original_root, const
std::string
     // don't copy the prefix, mandir, localstatedir and infodir
     if (it.first != LAYOUT_EXEC_PREFIX && it.first != LAYOUT_LOCALSTATEDIR &&
it.first != LAYOUT_MANDIR &&
         it.first != LAYOUT_INFODIR) {
-      if (!copy_directory(old_path, new_path, it.first)) {
+      if (!copy_directory(old_path, new_path, it.first, copy_style)) {
         ink_warning("Unable to copy '%s' - %s", it.first.c_str(), strerror(errno));
         ink_notice("Creating '%s': %s", it.first.c_str(), new_path.c_str());
         // if copy failed for certain directory, we create it in runroot
diff --git a/src/traffic_layout/engine.h b/src/traffic_layout/engine.h
index 82c7581..f83339b 100644
--- a/src/traffic_layout/engine.h
+++ b/src/traffic_layout/engine.h
@@ -23,11 +23,14 @@
 
 #pragma once
 
+#include "file_system.h"
+
 #include <vector>
 #include <string>
 #include <unordered_map>
 
 #define RUNROOT_WORD_LENGTH 10
+#define COPYSTYLE_WORD_LENGTH 12
 
 typedef std::unordered_map<std::string, std::string> RunrootMapType;
 
@@ -71,6 +74,8 @@ struct RunrootEngine {
   // for parsing
   int command_num = 0;
 
+  CopyStyle copy_style = HARD;
+
   // the path for create & remove
   std::string path;
 
diff --git a/src/traffic_layout/file_system.cc b/src/traffic_layout/file_system.cc
index e7bca3a..681038e 100644
--- a/src/traffic_layout/file_system.cc
+++ b/src/traffic_layout/file_system.cc
@@ -43,6 +43,7 @@ static std::string dst_root;
 static std::string src_root;
 static std::string copy_dir; // the current dir we are copying. e.x. sysconfdir, bindir...
 static std::string remove_path;
+CopyStyle copy_style;
 
 // list of all executables of traffic server
 std::set<std::string> const executables = {"traffic_crashlog", "traffic_ctl",     "traffic_layout",
"traffic_logcat",
@@ -255,12 +256,18 @@ ts_copy_function(const char *src_path, const struct stat *sb, int flag)
     }
     // hardlink bin executable
     if (sb->st_mode & S_IEXEC) {
-      if (link(src_path, dst_path.c_str()) != 0) {
-        if (errno != EEXIST) {
+      if (copy_style == SOFT) {
+        if (symlink(src_path, dst_path.c_str()) != 0 && errno != EEXIST) {
+          ink_warning("failed to create symlink - %s", strerror(errno));
+        } else {
+          return 0;
+        }
+      } else if (copy_style == HARD) {
+        if (link(src_path, dst_path.c_str()) != 0 && errno != EEXIST) {
           ink_warning("failed to create hard link - %s", strerror(errno));
+        } else {
+          return 0;
         }
-      } else {
-        return 0;
       }
     }
     // for normal other files
@@ -276,11 +283,12 @@ ts_copy_function(const char *src_path, const struct stat *sb, int flag)
 
 // copy directory recursively using ftw to iterate
 bool
-copy_directory(const std::string &src, const std::string &dst, const std::string
&dir)
+copy_directory(const std::string &src, const std::string &dst, const std::string
&dir, CopyStyle style)
 {
-  src_root = src;
-  dst_root = dst;
-  copy_dir = dir;
+  src_root   = src;
+  dst_root   = dst;
+  copy_dir   = dir;
+  copy_style = style;
   remove_slash(src_root);
   append_slash(dst_root);
 
diff --git a/src/traffic_layout/file_system.h b/src/traffic_layout/file_system.h
index 54a93bb..b6d450e 100644
--- a/src/traffic_layout/file_system.h
+++ b/src/traffic_layout/file_system.h
@@ -29,6 +29,9 @@
 // size can be changed accordingly
 #define OPEN_MAX_FILE 256
 
+// full copy, hardlink, softlink
+enum CopyStyle { FULL, HARD, SOFT };
+
 // append slash & remove slash of path for convinient use
 void append_slash(std::string &path);
 
@@ -47,4 +50,4 @@ bool remove_directory(const std::string &dir);
 // remove everything inside this directory
 bool remove_inside_directory(const std::string &dir);
 
-bool copy_directory(const std::string &src, const std::string &dst, const std::string
&dir = "");
+bool copy_directory(const std::string &src, const std::string &dst, const std::string
&dir = "", CopyStyle style = HARD);


Mime
View raw message