linuxファイルプロパティ
3419 ワード
属性の構造について
linuxではファイルもフォルダもファイルとみなされるため、以下のこの属性はファイルとフォルダに共通して属性を取得する関数にstat/fstat/lstat/fstatがある
ファイルの種類
linuxの下のファイルは以下のいくつかに分けられます:1.通常ファイル(regular file)、判定関数はS_ISREG(); 2.ディレクトリファイル(directory file)、判定関数はS_ISDIR(); 3.ブロック特殊ファイル(block special file),S_ISBLK(); 3.文字特殊ファイル(character special file),S_ISCHR(); 5.プロセス通信パイプファイル(FIFO),S_ISFIFO(); 6.ソケット、S_ISSOCK(); 7.シンボルリンク(symbolic link)、S_ISLNK();
アクセス権のテスト
ファイルには読み取り/書き込み/実行の3つの権限があり、ファイルの所有者はファイルに対して読み取り/書き込み/実行の権限があり、同じグループには読み取り/実行の権限があり、異なるグループには読み取りの権限がない可能性があるので、既存のファイルを読み書き操作する際に、アクセス権判断int access(const char*pathname,int mode)を先に行うことができます.modeには3つの選択パラメータがあります:R_OK, W_OK, X_OK、それぞれ読み書き/実行
アクセス権の変更
ファイルのアクセス条件を変更するには、少なくとも1つを満たす必要があります:1.スーパーユーザープロセスが変更されました.2.ファイル所有者のプロセスによるファイル権限の変更の設定は、次の3つの部分に分かれています:1.ファイルの所有者、S_IRUSR,S_IWUSR,S_IXUSR、三合一の表記はS_IRWXU 2.同グループのユーザ、S_IRGRP,S_IWGRP,S_IXGLP、三合一の表記はS_IRWXG 3.他のグループのユーザ、S_IROTH,S_IWOTH,S_IXOTH、三合一の表記はS_IRWXO
以上の権限は、読み取り、書き込み、実行の順で、3つの権限が含まれています.
変更された関数はchmod/fchmod/fchmodatです
ファイル所有者の変更
この動作は、ほとんどのlinuxシステムでは、chown/fchown/fchownat/lchown int chown(const char*pathname,uid_t owner,gid_t group)を実行するためにrootが必要である.
ファイルサイズ
またtruncate(pathname,0)でファイルサイズを0に設定したり、ファイル内容を消去したりすることができます.
その他
ファイルの作成:int creat(const char*path,mode_t mode)modeはアクセス権であり、openでファイル名の変更を作成することもできます:int rename(const char*oldname,const char*newname)削除ファイル:int remove(const char*pathname)
linuxではファイルもフォルダもファイルとみなされるため、以下のこの属性はファイルとフォルダに共通して属性を取得する関数にstat/fstat/lstat/fstatがある
struct stat{
mode_t st_mode; //
ino_t st_ino;
dev_t st_dev;
dev_t st_rdev;
nlink_t st_nlink;
uid_t st_uid; // ID
gid_t st_gid; //
offt_t st_size; //
struct timespec st_atime;
struct timespec st_mtime;
struct timespec st_ctime;
blksize_t st_blksize;
blkcnt_t st_blocks;
}
struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname, &buf) < 0){
printf("lstat error");
exit(1);
}
ファイルの種類
linuxの下のファイルは以下のいくつかに分けられます:1.通常ファイル(regular file)、判定関数はS_ISREG(); 2.ディレクトリファイル(directory file)、判定関数はS_ISDIR(); 3.ブロック特殊ファイル(block special file),S_ISBLK(); 3.文字特殊ファイル(character special file),S_ISCHR(); 5.プロセス通信パイプファイル(FIFO),S_ISFIFO(); 6.ソケット、S_ISSOCK(); 7.シンボルリンク(symbolic link)、S_ISLNK();
struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname, &buf) < 0){
printf("lstat error");
exit(1);
}
if(S_ISREG(buf.st_mode))
printf("this is a regular file");
else if(S_ISDIR(buf.st_mode))
printf("this is a directory");
else if(..)
...
else
printf("unknown file type");
アクセス権のテスト
ファイルには読み取り/書き込み/実行の3つの権限があり、ファイルの所有者はファイルに対して読み取り/書き込み/実行の権限があり、同じグループには読み取り/実行の権限があり、異なるグループには読み取りの権限がない可能性があるので、既存のファイルを読み書き操作する際に、アクセス権判断int access(const char*pathname,int mode)を先に行うことができます.modeには3つの選択パラメータがあります:R_OK, W_OK, X_OK、それぞれ読み書き/実行
char *pathname="./test.txt";
if(access(pathname,R_OK) < 0){
perror("access error");
exit(1);
}else
printf("read access");
int fd;
if((fd=open(pathname,O_RDONLY))<0){
printf("open error");
exit(1);
}else
printf("open for reading");
アクセス権の変更
ファイルのアクセス条件を変更するには、少なくとも1つを満たす必要があります:1.スーパーユーザープロセスが変更されました.2.ファイル所有者のプロセスによるファイル権限の変更の設定は、次の3つの部分に分かれています:1.ファイルの所有者、S_IRUSR,S_IWUSR,S_IXUSR、三合一の表記はS_IRWXU 2.同グループのユーザ、S_IRGRP,S_IWGRP,S_IXGLP、三合一の表記はS_IRWXG 3.他のグループのユーザ、S_IROTH,S_IWOTH,S_IXOTH、三合一の表記はS_IRWXO
以上の権限は、読み取り、書き込み、実行の順で、3つの権限が含まれています.
変更された関数はchmod/fchmod/fchmodatです
struct stat buf;
char *pathname="./test.txt";
if(stat(pathname,&buf)<0){
printf("stat error");
exit(1);
}
// S_IXGRP
if(chmod(pathname,buf.st_mode & ~S_IXGRP)<0)
printf("chmod error");
// ,
if(chmod(pathname,S_IRUSR|S_IWUSR|S_IRGRP)<0)
printf("chmod error");
ファイル所有者の変更
この動作は、ほとんどのlinuxシステムでは、chown/fchown/fchownat/lchown int chown(const char*pathname,uid_t owner,gid_t group)を実行するためにrootが必要である.
// ,ID ID 1000
// root , sudo root
char *pathname="./test.txt";
if(chown(pathname,1000,1000)<0)
printf("chown failed");
ファイルサイズ
struct stat buf;
char *pathname="./test.txt";
if(lstat(pathname,&buf)<0){
printf("lstat error");
exit(1);
}
printf("file size: %lu",buf.st_size);
またtruncate(pathname,0)でファイルサイズを0に設定したり、ファイル内容を消去したりすることができます.
その他
ファイルの作成:int creat(const char*path,mode_t mode)modeはアクセス権であり、openでファイル名の変更を作成することもできます:int rename(const char*oldname,const char*newname)削除ファイル:int remove(const char*pathname)