Skip to content

cp

Abstract

Functions to copy files and directories

Header file

namespace sstd{
    bool copy(const char*        pPath_src, const char*        pPath_dst, const char* opt);
    bool copy(const std::string&  path_src, const char*        pPath_dst, const char* opt);
    bool copy(const char*        pPath_src, const std::string&  path_dst, const char* opt);
    bool copy(const std::string&  path_src, const std::string&  path_dst, const char* opt);
    bool copy(const char*        pPath_src, const char*        pPath_dst);
    bool copy(const std::string&  path_src, const char*        pPath_dst);
    bool copy(const char*        pPath_src, const std::string&  path_dst);
    bool copy(const std::string&  path_src, const std::string&  path_dst);

    bool cp  (const char*        pPath_src, const char*        pPath_dst, const char* opt);
    bool cp  (const std::string&  path_src, const char*        pPath_dst, const char* opt);
    bool cp  (const char*        pPath_src, const std::string&  path_dst, const char* opt);
    bool cp  (const std::string&  path_src, const std::string&  path_dst, const char* opt);
    bool cp  (const char*        pPath_src, const char*        pPath_dst);
    bool cp  (const std::string&  path_src, const char*        pPath_dst);
    bool cp  (const char*        pPath_src, const std::string&  path_dst);
    bool cp  (const std::string&  path_src, const std::string&  path_dst);
}

Description

Function name Description
copy() A function to copy a single file, and give the same permission as the source.
1 つのファイルをコピーする関数.コピー元のファイルと同一のパーミッションを付与します.
cp() A function to copy files and directories, and give the same permission as the source.
複数のファイルやディレクトリをコピーする関数.コピー元のファイルやディレクトリと同一のパーミッションを付与します.

options
- n: (n: no overwrite) do not to overwrite the existing file.
- p: (p: permission) copy file with the same permission and timestamp.
- u: (u: update) update the file only when the dst file is older than src file.

Usage

copy(): copy a file

  • main.cpp
    #include <sstd/sstd.hpp>
    
    int main(){
        sstd::mkdir("./tmp");
        sstd::system("dd if=/dev/urandom of=./tmp/rand.bin bs=1M count=10 > /dev/null 2>&1");
    
        sstd::copy("./tmp/rand.bin", "./tmp/rand_copy.bin");
        sstd::system("ls ./tmp");
        sstd::system("sha256sum ./tmp/rand.bin | cut -d \" \" -f 1");
        sstd::system("sha256sum ./tmp/rand_copy.bin | cut -d \" \" -f 1");
    
        sstd::rm("./tmp");
    }
    
  • Execution result
    rand.bin
    rand_copy.bin
    5ed6b00caf3c141b5dd43eed090bfed3636405e9d0182f91eaa2c00729d5bed9
    5ed6b00caf3c141b5dd43eed090bfed3636405e9d0182f91eaa2c00729d5bed9
    

cp(): copy a directory

  • main.cpp
    #include <sstd/sstd.hpp>
    
    int main(){
        sstd::mkdir("./tmp");
    
        sstd::cp("./sstd", "./tmp");
        sstd::system("ls ./tmp");
    
        sstd::rm("./tmp");
    }
    
  • Execution result
    sstd
    

cp(): copy under the directory

  • main.cpp
    #include <sstd/sstd.hpp>
    
    int main(){
        sstd::mkdir("./tmp");
    
        sstd::cp("./sstd/*", "./tmp");
        sstd::system("ls ./tmp");
    
        sstd::rm("./tmp");
    }
    
  • Execution result
    LICENSE
    Makefile
    compileOption.hpp
    include
    lib
    src
    sstd.hpp
    tmpMake
    

Implementation