Why Doesn't Tar Preserve File Permissions

why doesn't tar preserve file permissions?

Use the p option, both when creating the tarball and when extracting it.

File permissions are not being preserved while after unarchive tar.gz

Under Linux/Unix there's no way for a normal user to transfer the ownership of a file system object to another user. Only root (UID 0) can.
In this case your program needs to be run as root and you need to use the system call chown() and related (see man 2 chown).

For the file permissions and time and date you need to use the system call utime() and related ones (see man 2 utime).

You can do this from Java with the methods available in java.io.File like setExecutable, setReadable, and setWritable.
Please refer to the official documentation for the details.

How to preserve file permissions with Java across different operating systems?

Your file transfer program would need a preserve permissions option. I don't think you're going to find one. You could run your program on the unix box to change the file permissions, or you could run a find command like

find . -name "*.sh" -exec chmod a+x {} \;

Or you could try setting your umask,

umask u+x

Or (if you're using cygwin), you could use tar with

tar cfp file.tar <scripts>

note that p is preserve permissions.

How can I use the Ant tar task and preserve file permissions?

I don't think there is a way to retain existing permissions, per this note from the copy task:

Unix Note: File permissions are not retained when files are copied; they end up with the default UMASK permissions instead. This is caused by the lack of any means to query or set file permissions in the current Java runtimes. If you need a permission-preserving copy function, use <exec executable="cp" ... > instead.

However the tar task can take one or more tarfileset elements. The tarfileset can be defined with a filemode and/or dirmode attribute to specify the unix permissions. If you specify multiple includes matching only those files to get each set of required permissions, the files in that set will be included with those permissions.

Permission with tar command

why there is en empty folder in a config001.tgz file?

There is the "root folder" included in the tar archive. The folder the tar was in. The privileges, owner and group, permissions and creation/modification (or one of them, I am not sure) dates are included. You can create such archive with:

mkdir -p /tmp/a
cd /tmp/a
echo 123 > 1
echo 234 > 2
tar cfvp /tmp/test.tar .

# and inspect with:
tar -tvf ./test.tar
drwxr-xr-x kamil/kamil 0 2019-07-15 12:50 ./
-rw-r--r-- kamil/kamil 4 2019-07-15 12:50 ./2
-rw-r--r-- kamil/kamil 4 2019-07-15 12:50 ./1

By specifing the ., ie. the current directory, the information about the current directory itself will be included in he tar. Ie. the information about the owner and group, permissions and dates.

if I don't append --no-overwrite-dir after command,it will raise error, what permission does "tar" want to change?

tar wants to change the permissions of the directory you are inside. The hpc_dir directory. The hpc_dir is owned by root, so tar can't change/touch it.

why --no-overwrite-dir option can fix the problem?

Because then tar sees the the currect directory hpc_dir exists. Because of that, tar doesn't try to create the directory, nor tries to change the owner and group permissions of the directory, nor tries to restore the creation date of the directory.

You could just go with mkdir somedir; tar xzfv archive.tar -C somedir - that way the somedir will be created by current user, so tar will be able to change it's properties.

Or you could just change the owner of hpc_dir directory, letting your user modify it.

Setting file permissions on contents of tarfile

With GNU tar, use the --mode option when creating the archive, e.g.:

tar cf archive.tar --mode='a+rwX' *

But note that when the archive is extracted, the umask will be applied by default. So unless the user's umask is 000, then the permissions will be updated at that point. However, the umask can be ignored by using the -p (--preserve) option, e.g.:

tar xfp archive.tar

After tar extract, Changing Permissions

If you want to keep the permissions on files then you have to add the -p (or --preserve-permissions or --same-permissions) switch when extracting the tarball. From the tar man pages :

--preserve-permissions
--same-permissions
-p
When `tar' is extracting an archive, it normally subtracts the
users' umask from the permissions specified in the archive and
uses that number as the permissions to create the destination
file. Specifying this option instructs `tar' that it should use
the permissions directly from the archive.

So PHP code should be :

exec("tar -xzfp foo.tar.gz");


Related Topics



Leave a reply



Submit