Search code examples
linuxshared-librariesredhatdynamic-linkingdynamic-library

Unable to copy the shared library's soft links with their original size in linux


I have created a shared object of 1.2 M and created 4 soft links for that SO. Size of all the links is 20B and the size of the main so is 1.2M

20         May 23 10:56 libAbc.so -> libAbc.so.2.0.11.0
20         May 23 10:56 libAbc.so.1 -> libAbc.so.2.0.11.0
20         May 23 10:56 libAbc.so.1.0 -> libAbc.so.2.0.11.0
1.2M        May 23 10:56 libAbc.so.2.0.11.0

While i tried to copy all these files to another folder using cp , the size of the links is equal to the main file.

1.2M May 24 08:07 libABC.so
1.2M May 24 08:07 libABC.so.1
1.2M May 24 08:07 libABC.so.1.0
1.2M May 24 08:07 libABC.so.2.0.11.0

I have also used

cp -s libAgent.so* src/ 

which is also failing with an error

cp: `src/libAgent.so': can make relative symbolic links only in current directory

Why is it failing to copy the softlinks with their original size(20B)


Solution

  • Copying a soft link usually copies the file the soft link refers to and does not replicate the soft link.

    If you wish to use cp to copy soft links as new soft links, specify the -a option to cp.

    cp -a libAbc.so* /path/to/another/folder
    

    The info page for cp says:

    `-a'
    `--archive'
         Preserve as much as possible of the structure and attributes of the
         original files in the copy (but do not attempt to preserve internal
         directory structure; i.e., `ls -U' may list the entries in a copied
         directory in a different order).  Equivalent to `-dpR'.
    

    The key is to select the "archive" option of the command used to copy the link. rsync is an effective alternative as Maxim Egorushkin points out, and rsync has the capability of copying extended attributes and ACLs if the command includes the appropriate command-line arguments.

    rsync -aAX libAbc.so* /path/to/another/folder
    

    The rsync man page says:

    -a, --archive               archive mode; equals -rlptgoD (no -H,-A,-X)
        --no-OPTION             turn off an implied OPTION (e.g. --no-D)
    -A, --acls                  preserve ACLs (implies -p)
    -X, --xattrs                preserve extended attributes
    

    In the OP's use case, the -A and -X are not needed.

    A caveat is that if the soft link is a relative link, copying it to a new location may make the link inoperative because it does not point to an absolute path.

    For example:

    $ ls -al /usr/lib/rpmpopt
    lrwxrwxrwx 1 root root 19 2012-05-02 12:40 /usr/lib/rpmpopt -> rpm/rpmpopt-4.4.2.3
    

    cp -a /usr/lib/rpmpopt ${HOME}/tmp and rsync -av /usr/lib/rpmpopt ${HOME}/tmp on my machine both create a broken link rpmpopt -> rpm/rpmpopt-4.4.2.3 because my local copy has no rpm sub-folder. One needs to consider this fact when deciding what to copy.

    This link is broken because ${HOME}/tmp/rpm is not present:

    $ ls -nl ${HOME}/tmp/rpmpopt
    lrwxrwxrwx 1 505 700 19 2016-05-24 10:04 /home/kbulgrien/tmp/rpmpopt -> rpm/rpmpopt-4.4.2.3