Linux2010. 4. 20. 22:43
예전글인 이녀석
2008/12/22 - [Linux] - 심볼릭 링크에 대한 미스테리
왜 심볼릭 링크의 크기는 파일크기와 문자열 길이가 동일할까? 라는 의문이 들었는데,
not null-terminated 라고 함은, inode 쪽에서 filename 변수를 가지고,
실제로 filename 변수에는 \0(=NULL)이 없기 때문이 아닐까 싶다.

i-node에는 파일의 속성이 저장되고,
디렉토리 구조체에 파일의 i-node와 파일의 이름이 저장된다.
# vi /usr/include/bits/dirent.h
struct dirent
  {
#ifndef __USE_FILE_OFFSET64
    __ino_t d_ino;
    __off_t d_off;
#else
    __ino64_t d_ino;
    __off64_t d_off;
#endif
    unsigned short int d_reclen;
    unsigned char d_type;
    char d_name[256];           /* We must not include limits.h! */
  };

# vi /usr/include/bits/stat.h
struct stat
  {
    __dev_t st_dev;                     /* Device.  */
    unsigned short int __pad1;
#ifndef __USE_FILE_OFFSET64
    __ino_t st_ino;                     /* File serial number.  */
#else
    __ino_t __st_ino;                   /* 32bit file serial number.    */
#endif
    __mode_t st_mode;                   /* File mode.  */
    __nlink_t st_nlink;                 /* Link count.  */
    __uid_t st_uid;                     /* User ID of the file's owner. */
    __gid_t st_gid;                     /* Group ID of the file's group.*/
    __dev_t st_rdev;                    /* Device number, if device.  */
    unsigned short int __pad2;
#ifndef __USE_FILE_OFFSET64
    __off_t st_size;                    /* Size of file, in bytes.  */
#else
    __off64_t st_size;                  /* Size of file, in bytes.  */
#endif
    __blksize_t st_blksize;             /* Optimal block size for I/O.  */

#ifndef __USE_FILE_OFFSET64
    __blkcnt_t st_blocks;               /* Number 512-byte blocks allocated. */
#else
    __blkcnt64_t st_blocks;             /* Number 512-byte blocks allocated. */
#endif
#ifdef __USE_MISC
    /* Nanosecond resolution timestamps are stored in a format
       equivalent to 'struct timespec'.  This is the type used
       whenever possible but the Unix namespace rules do not allow the
       identifier 'timespec' to appear in the  header.
       Therefore we have to handle the use of this header in strictly
       standard-compliant sources special.  */
    struct timespec st_atim;            /* Time of last access.  */
    struct timespec st_mtim;            /* Time of last modification.  */
    struct timespec st_ctim;            /* Time of last status change.  */
# define st_atime st_atim.tv_sec        /* Backward compatibility.  */
# define st_mtime st_mtim.tv_sec
# define st_ctime st_ctim.tv_sec
#else
    __time_t st_atime;                  /* Time of last access.  */
    unsigned long int st_atimensec;     /* Nscecs of last access.  */
    __time_t st_mtime;                  /* Time of last modification.  */
    unsigned long int st_mtimensec;     /* Nsecs of last modification.  */
    __time_t st_ctime;                  /* Time of last status change.  */
    unsigned long int st_ctimensec;     /* Nsecs of last status change.  */
#endif
#ifndef __USE_FILE_OFFSET64
    unsigned long int __unused4;
    unsigned long int __unused5;
#else
    __ino64_t st_ino;                   /* File serial number.  */
#endif
  };

다르게 생각해 보자면,
directory table에 파일 이름이 저장되고, (심볼릭 링크)
속성으로 심볼릭 링크가 지정되며,
inode의 filesize는 string이 아닌 data로서 \0(=NULL)이 빠진 순수한 원본 경로만 들어가서
ls 시의 심볼릭 링크 파일의 크기가 예상과 달리 -1 크기로 나오는게 아닐까 싶다.

#include <unistd.h>
ssize_t readlink(const char *restrict path, char *restrict buf, size_t bufsize);

Return Value
Upon successful completion, readlink() shall return the count of bytes placed in the buffer. Otherwise, it shall return a value of -1, leave the buffer unchanged, and set errno to indicate the error.

[링크 : http://linux.die.net/man/3/readlink]

The readlink function gets the value of the symbolic link filename. The file name that the link points to is copied into buffer. This file name string is not null-terminated; readlink normally returns the number of characters copied. The size argument specifies the maximum number of characters to copy, usually the allocation size of buffer.

[링크 : http://www.gnu.org/s/libc/manual/html_node/Symbolic-Links.html]

APPLICATION USAGE
    Conforming applications should not assume that the returned contents of the symbolic link are null-terminated.

RATIONALE
    Since IEEE Std 1003.1-2001 does not require any association of file times with symbolic links, there is no requirement that file times be updated by readlink(). The type associated with bufsiz is a size_t in order to be consistent with both the ISO C standard and the definition of read(). The behavior specified for readlink() when bufsiz is zero represents historical practice. For this case, the standard developers considered a change whereby readlink() would return the number of non-null bytes contained in the symbolic link with the buffer buf remaining unchanged; however, since the stat structure member st_size value can be used to determine the size of buffer necessary to contain the contents of the symbolic link as returned by readlink(), this proposal was rejected, and the historical practice retained.

[링크 : http://www.opengroup.org/onlinepubs/000095399/functions/readlink.html]

'Linux' 카테고리의 다른 글

ubuntu 9.10 에서 APM + viewvc + cvsgraph 돌리기  (0) 2010.04.28
enscript  (0) 2010.04.28
pwd(getcwd), cd(chdir)  (4) 2010.04.19
wget  (4) 2010.04.10
/dev의 major minor에 대하여  (0) 2010.04.08
Posted by 구차니
Linux2008. 11. 4. 14:43
졸지에 낙시글이 되것 같아. 디렉토리 이름 변경하는 방법 급조 -ㅁ-
리눅스에서는 엄밀하게 디렉토리 이름 바꾸는 방법은 존재하지 않습니다.
리눅스의 모든 것들은 파일로 관리 되기 때문이죠.

그런 이유로 리눅스에서 디렉토리 이름을 변경하려면 mv
다른 이름으로 디렉토리를 옮기면 됩니다.

예) test_1 디렉토리를 test_opps로 바꾸기
$ mv test_1 test_oops

---------------------------------------------
오늘 작업을 하다가 신기한 현상을 발견했다.

리눅스에서 컴파일 하고, 윈도우에서 samba와 cvs로 관리하는 시스템인데,
리눅스 에서 열어 놓은 디렉토리를 윈도우에서 다른 이름으로 변경하고
리눅스에서 열린 디렉토리의 이름으로 cvs checkout을 했다.(그러니까 백업본을 가져왔다)

그리고 나서 소스를 변경하고 다시 컴파일 하는데, 변경사항이 없음?!?!?!
먼가 이상해서 경로를 빠져 나왔다가 다시 들어 가니 그 제서야 제대로 컴파일이 된다.

결론만 말하자면, linux에서 directory는 이름으로 확인하는것이 아니라 inode로 확인하므로,
다른 곳에서 디렉토리를 수정했을 경우, 반드시 cd 명령어로 다시 경로를 이동해서 사용하자!!


삭제 테스트
step 1. 임의의 디렉토리를 만든다. (귀찮으니 tt라고 하자)
           [user@hostname ~] $ mkdir tt

step 2. 임의의 디렉토리로 들어간다.
           [user@hostname ~] $ cd tt

step 3. 임의의 디렉토리의 inode를 확인해본다.
           [user@hostname tt] $ ls -ali
           11044597 drwxrwxr-x 2 user hostname 4096 Nov  4 14:38 .
            3866626 drwxrwxrwx 9 user hostname 4096 Nov  4 14:38 ..

step 4. 임의의 디렉토리를 다른 콘솔이나 윈도우의 다른 창에서 삭제한다.

step 5. 다시 inode를 출력해본다.
           [user@hostname tt] $ ls -ali
           total 0

step 6. 현재 경로를 알아본다.
           [user@hostname tt] $ pwd
           /home/user/tt

'Linux' 카테고리의 다른 글

파일 존재 유무 확인하기(how to check existence of file on C,linux)  (2) 2008.12.16
execl() - excute a file  (0) 2008.12.08
fork : Not Enough Memory  (2) 2008.12.08
mkfs - ext2 & ext3  (4) 2008.11.21
SRM - Secure RM  (0) 2008.11.10
Posted by 구차니