Skip to content

Tar Wrapped by Cli Tool Fails to Write

Our internal CLI tool will try to download a proto file by git achieve command, and then tar it to /usr/local/include. One user reported in his desktop tar cannot unzip file:

xxx.proto: Can't create 'xxx.proto'
tar: Error exit delayed from previous errors.
ERROR fail to extract xxx.proto into '/usr/local/include/xxx.proto' 
via this script: 'tar -xf xxx.tar -C /usr/local/include'.

Solution

Based on "Can't create" and tar intended to write under /usr, it might be a permission issue. Hence, we need to check the permission first.

❯ ls -l /usr/local/ | grep include
drwxr-xr-x   2 root  wheel   64 Jun 16 10:29 include

❯ ls -l $(which tar)
lrwxr-xr-x  1 root  wheel  6 Jun  9 23:31 /usr/bin/tar@ -> bsdtar

Because only the root has the permission to write files in folder /usr/local/include, the tar will fail to write that folder because the current user doesn't has sufficient permission. The solution provided for this user is to change the ownership from root to himself for file /usr/local/include.

Here is also a lesson for developing cli tools:

Note

Write files under a folder managed by the CLI tool instead of a system folder.

File Permission

Basically, file has three kind of permissions, Read, Write and Execute. Read and Write are reasonable because we need to control the modification and visualization. Because some bytes in a file is either data or runnable executable binary in a von neumann archietecture.

The r, w and x denotes the reading, writing and execution. That's the basic permission of files.

A file has three basic permissions, however, in a real world a file contains three parts settings to if you run ls -l or ll, you definitely will see more contents than a single composition of rwx:

❯ ls -l /usr/local/ | grep include
drwxr-xr-x   2 root  wheel   64 Jun 16 10:29 include
The 10-letter charactor denotes permissions for different groups.

   d       dwx                r-x            r-x
   _       ___                ___            ___
filetype   owning user        owning group   other

If an user is neither an owner nor a member in the owning group, it could only access the file with other permission.

chmod and chown are two commands which relates to permission. The chmod means change mode, which changes the permission for each owner. The chown means change owner

Sudo and Su

The sudo is acronym of substitute user do, elevates the prompt without changing identity. It relies on settings in /etc/sudoers. The su substites user to root.