Linux2010.04.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;
    __ino64_t d_ino;
    __off64_t d_off;
    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.  */
    __ino_t __st_ino;                   /* 32bit file serial number.    */
    __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.  */
    __off64_t st_size;                  /* Size of file, in bytes.  */
    __blksize_t st_blksize;             /* Optimal block size for I/O.  */

#ifndef __USE_FILE_OFFSET64
    __blkcnt_t st_blocks;               /* Number 512-byte blocks allocated. */
    __blkcnt64_t st_blocks;             /* Number 512-byte blocks allocated. */
#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
    __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.  */
#ifndef __USE_FILE_OFFSET64
    unsigned long int __unused4;
    unsigned long int __unused5;
    __ino64_t st_ino;                   /* File serial number.  */

다르게 생각해 보자면,
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.

[링크 :]

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.

[링크 :]

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

    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.

[링크 :]


'Linux' 카테고리의 다른 글

ubuntu 9.10 에서 APM + viewvc + cvsgraph 돌리기  (0) 2010.04.28
enscript  (0) 2010.04.28
readlink() 와 심볼릭 링크, 그리고 inode(아이노드)  (0) 2010.04.20
pwd(getcwd), cd(chdir)  (4) 2010.04.19
wget  (4) 2010.04.10
/dev의 major minor에 대하여  (0) 2010.04.08
Posted by 구차니

댓글을 달아 주세요