문제

One of the task that my python script does is to extract an entire Linux Root FS tarball and then copy files from it into a destination directory. While doing so, I am getting the error in case of special restricted files (like gshadow) as follow:

File "script.py", line 553, in copy_rootfs
shutil.copy(sourceFileName,targetFileName)
File "/usr/lib/python2.6/shutil.py", line 88, in copy
copyfile(src, dst)
File "/usr/lib/python2.6/shutil.py", line 52, in copyfile
fsrc = open(src, 'rb')
IOError: [Errno 13] Permission denied: '/home/myuser/temp/home/embeduser/NFS/LinuxFS/etc/gshadow'

Permissions for the file gshadow extracted from tarball is as follow:

myuser@host temp]$ ls -l home/embeduser/NFS/LinuxFS/etc/gshadow 
---------- 1 myuser myuser 271 Nov  5 08:54 home/embeduser/NFS/LinuxFS/etc/gshadow 

Is there a way I can achieve this without having to run my python script as root?

도움이 되었습니까?

해결책

You can't do it with shutil, but it's easy enough to come up with some custom solution. The trick is that gshadow has no permissions, but belongs to the local user. So you can change its permissions with os.chmod(), read it, then (if necessary) change the permissions back.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top