2014-10-19 3 views
1

기존 파일에 쓰려고하는 동시에 해당 사용 권한을 변경하려고합니다. 예를 들어 :기존 파일에 쓰기 및 사용 권한 변경

Writing file with permissions 0755 
Actual file permissions are 0755 
Writing file with permissions 0775 
Actual file permissions are 0755 

왜 두 번째 기록 후 정지 허가를 0755은 다음과 같습니다

use warnings; 
use strict; 
use File::Slurp 'write_file'; 

my $script="#! /bin/bash 
echo \"Hello\" 
"; 

my $saveName='test.sh'; 
unlink $saveName if -f $saveName; 
writeFile($saveName,$script,0755); 
writeFile($saveName,$script,0775); 


sub writeFile { 
    my ($saveName,$script,$mode) = @_; 

    printf "Writing file with permissions %04o\n", $mode & 07777; 
    write_file($saveName,{perms=>$mode},\$script); 
    my $actualMode = (stat($saveName))[2]; 
    printf "Actual file permissions are %04o\n", $actualMode & 07777; 
} 

이 출력을 제공? the documentation에서

답변

2

(나는 0775 것으로 기대) : 단어가 "새로 생성"

perms 

The perms option sets the permissions of newly-created files. This value is 
modified by your process's umask and defaults to 0666 (same as sysopen). 

참고.

이 동작은 모듈에 의해 결정되지 않고 코어 sysopen에 의해 결정됩니다. File::Slurp의 소스에서 :

   my $perms = $opts->{perms} ; 
       $perms = 0666 unless defined $perms ; 

#printf "WR: BINARY %x MODE %x\n", O_BINARY, $mode ; 

# open the file and handle any error. 

       $write_fh = local(*FH) ; 
#    $write_fh = gensym ; 
       unless (sysopen($write_fh, $file_name, $mode, $perms)) { 

우리는 sysopen가 사용되는 것을 알 수있다. the documentation for sysopen 그것은 말한다 :

FILENAME 에 의해 명명 된 파일이 존재하지 않고, 공모가 (MODE는 O_CREAT 플래그를 포함하고 있기 때문에 일반적으로)는 다음 PERMS의 값이 새로 생성의 권한을 지정을 만드는 경우 파일.

+0

감사합니다. 그러나 이것은 이상한 접근 방식입니다. 즉,'perms' 옵션을'write_file'에 쓰고 싶다면 먼저 그 파일이 이미 존재하는지 확인해야합니다. 이 경우, 명시 적으로 파일을 지우거나 파일을 작성한 후에'chmod'를 사용해야합니다. –

+1

이것은 모듈에 의해 제어되는 것이 아니라'sysopen '에 의해 제어됩니다. 나는 당신을 보여줄 문서를 추가 할 것이다. – TLP