퍼미션을 보기좋게 구하자!

  • 플로렐라
  • 2005.01.03 11:43:32
  • 4
퍼미션을 보기 좋게 구하는 함수를 만들어 보았습니다.

유닉스 시스템에서

echo `ls -al`;
하면,

대략 아래 예와 같이 출력됩니다.
(서버에서 명령어 막았다면 OTL)

total 16
drwxrwxrwx  4 plorella  member   512 12  9 19:08 .
drwxr-xr-x  7 plorella  member   512  1  2 15:13 ..
d---------  2 plorella  member   512 12  2 20:54 000
----------  1 plorella  member     0 12  2 20:23 000.php
---------x  1 plorella  member     0 12  2 20:44 001.php
--------w-  1 plorella  member     0 12  2 20:44 002.php
--------wx  1 plorella  member     0 12  2 20:44 003.php
-------rwx  1 plorella  member     0 12  2 20:46 007.php
------x---  1 plorella  member     0 12  2 20:47 010.php
-rwxrwxrw-  1 plorella  member     0 12  2 20:48 776.php
drwxrwxrwx  2 plorella  member   512 12  2 20:54 777
-rwxrwxrwx  1 plorella  member     0 12  2 20:47 777.php
-rwxrwxrwx  1 plorella  member   239 12  2 20:35 psl_dir.php
lrwxr-xr-x  1 nobody    member    11 12  9 19:04 psl_dir.symlink -> psl_dir.php
-rw-r--r--  1 plorella  member  1299  1  3 11:29 test.php
-rw-r--r--  1 plorella  member    59 12 10 19:38 test2.php
-rw-r--r--  1 plorella  member  1287 12  9 19:07 void.php

여기서 앞의 글자 10자리가 퍼미션을 뜻합니다.
고2때 유닉스OS쓰는 DUCT소프트를 사용해서 배운건데...

(아시는 분도 많겠지만)
맨 앞자리가 d면 디렉토리, l이면 심볼릭링크 (위의 예제에 하나 만들어 두었습니다; 찾아보세요;)
-면 일반 파일입니다.(하드링크도 파일로 취급하는듯... 이런건 잘 몰라서;;)
그다음 세자리가 소유자의 권한입니다.
r이면 읽기가능, w면 쓰기가능, x면 실행가능. -는 권한없음
다음 세자리는 그룹의 권한.(같은 방식으로;)
다음 세자리가 유저..라고 되있는데, 일반다른사용자의 권한인.

아래의 함수가 위와같이 파일명을 인수로 문자열로 퍼미션을 반환합니다.

        function get_perms($file){

                $perms = fileperms($file);

                $ans .= ($perms & 0x00004000) ? 'd' : '-'; //디렉토리인지 조사
                $ans .= ($perms & 0x00000100) ? 'r' : '-'; //Owner : 읽기
                $ans .= ($perms & 0x00000080) ? 'w' : '-'; //Owner : 쓰기
                $ans .= ($perms & 0x00000040) ? 'x' : '-'; //Owner : 실행
                $ans .= ($perms & 0x00000020) ? 'r' : '-'; //Group : 읽기
                $ans .= ($perms & 0x00000010) ? 'w' : '-'; //Group : 쓰기
                $ans .= ($perms & 0x00000008) ? 'x' : '-'; //Group : 실행
                $ans .= ($perms & 0x00000004) ? 'r' : '-'; //User : 읽기
                $ans .= ($perms & 0x00000002) ? 'w' : '-'; //User : 쓰기
                $ans .= ($perms & 0x00000001) ? '+ 'x' : '-'; //User : 실행

                return $ans;
        }

단 fileperms함수로 퍼미션정보를 얻기때문에,
심볼릭링크는 판별하지 못합니다. (심심하신분은 링크판별함수 추가해서 기능을 추가하시는것도..)

TagList

  • Doesn't exist

AttachmentFile List

  • Doesn't exist
목록으로 돌아가기
Write a comment
Comment List [4]
  • Snow

    이와오 준코님이... 70이었군요.... 흐음.... 벌써 30대중반 =_=;;;;;;;
    인생 무상~

    Comment Jan 06, 2005 Delete

  • 플로렐라

    목소리는 여전하십니다 /ㅅ/;;

    Comment Jan 06, 2005 Delete

  • 앳플군

    $perms = fileperms('/etc/passwd');

    if (($perms & 0xC000) == 0xC000) {
    // Socket
    $info = 's';
    } elseif (($perms & 0xA000) == 0xA000) {
    // Symbolic Link
    $info = 'l';
    } elseif (($perms & 0x8000) == 0x8000) {
    // Regular
    $info = '-';
    } elseif (($perms & 0x6000) == 0x6000) {
    // Block special
    $info = 'b';
    } elseif (($perms & 0x4000) == 0x4000) {
    // Directory
    $info = 'd';
    } elseif (($perms & 0x2000) == 0x2000) {
    // Character special
    $info = 'c';
    } elseif (($perms & 0x1000) == 0x1000) {
    // FIFO pipe
    $info = 'p';
    } else {
    // Unknown
    $info = 'u';
    }

    // Owner
    $info .= (($perms & 0x0100) ? 'r' : '-');
    $info .= (($perms & 0x0080) ? 'w' : '-');
    $info .= (($perms & 0x0040) ?
    (($perms & 0x0800) ? 's' : 'x' ) :
    (($perms & 0x0800) ? 'S' : '-'));

    // Group
    $info .= (($perms & 0x0020) ? 'r' : '-');
    $info .= (($perms & 0x0010) ? 'w' : '-');
    $info .= (($perms & 0x0008) ?
    (($perms & 0x0400) ? 's' : 'x' ) :
    (($perms & 0x0400) ? 'S' : '-'));

    // World
    $info .= (($perms & 0x0004) ? 'r' : '-');
    $info .= (($perms & 0x0002) ? 'w' : '-');
    $info .= (($perms & 0x0001) ?
    (($perms & 0x0200) ? 't' : 'x' ) :
    (($perms & 0x0200) ? 'T' : '-'));

    echo $info;

    Comment Jan 15, 2005 Delete

  • 플로렐라

    앳플군 // 정확하게 잘 동작하는것인지요?
    심볼릭링크 판별못하던데...

    Comment Jan 26, 2005 Delete