歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Unix知識 >> Unix基礎知識 >> unix系統編程小結:文件和目錄

unix系統編程小結:文件和目錄

日期:2017/2/25 10:12:14   编辑:Unix基礎知識

一.對linux的安全機制的一點感悟

各種權限,read,write,execute,set-user-ID,set-group-ID,sticky bit,對目錄的權限,對文件的權限,用於保證系統安全的各種組合技,各種經典。比如,如果我們想unlink一個文件,就必須擁有該文件所在目錄的write與execute的權限。

二.兩個小例子

1.當文件有hole時,cp命令會同時拷貝這些hole為'\0'。這裡是一個實現了拷貝時跳過文件hole的程序。ps:我用的buffer是一個字節的,效率很低,但如果用大的buffer就會使得hole被移除,使得原先分開的字符被連上。我沒想好如何解決這個問題。如果您知道,請您告訴小弟我,非常感謝!

[cpp] view plaincopy

  1. #include <apue.h>
  2. #include <my_error.h>
  3. #include <fcntl.h>
  4. int main()
  5. {
  6. char buf[1];
  7. int fd,fd_to;
  8. int n;
  9. if( (fd=open("temp_in_hole",O_RDWR) )<0)
  10. err_sys("error open");
  11. if( (fd_to=open("temp_OUT_hole",O_WRONLY))<0 )
  12. err_sys("error open for ");
  13. while( (n=read(fd,buf,1))!=0 )
  14. {
  15. if(buf[0]!='\0')
  16. if(write(fd_to,buf,1)!=n)
  17. err_sys("error write");
  18. }
  19. close(fd);
  20. close(fd_to);
  21. return 0;
  22. }


2.遍歷目錄。這裡只貼出主要代碼: ps:有一個技巧就是每遇到一個目錄時,就用chdir將該目錄設置為當前的工作目錄,可以提高程序運行的效率。

[cpp] view plaincopy

  1. static int dopath(Myfunc * func)
  2. {
  3. struct stat statbuf;
  4. struct dirent *dirp;
  5. DIR *dp;
  6. int ret;
  7. char *ptr;
  8. if(lstat(fullpath,&statbuf)<0)
  9. return (func(fullpath,&statbuf,FTW_NS));
  10. if(S_ISDIR(statbuf.st_mode)==0)
  11. return (func(fullpath,&statbuf,FTW_F));
  12. if( (ret=func(fullpath,&statbuf,FTW_D))!=0 )
  13. return ret;
  14. ptr=fullpath+strlen(fullpath);
  15. *ptr++='/';
  16. *ptr=0;
  17. if(chdir(fullpath)<0)
  18. err_ret("chdir for %s failed!",fullpath);
  19. if((dp=opendir("./"))==NULL)
  20. return (func(fullpath,&statbuf,FTW_DNR));
  21. while((dirp=readdir(dp))!=NULL)
  22. {
  23. if(strcmp(dirp->d_name,".")==0 || strcmp(dirp->d_name,"..")==0)
  24. continue;
  25. strcpy(ptr,dirp->d_name);
  26. if(ret=dopath(func)!=0)
  27. return ret;
  28. }
  29. ptr[-1]=0;
  30. if(closedir(dp)<0)
  31. err_ret("can't close directory %s",fullpath);
  32. if(chdir("../")<0)
  33. err_ret("chdir for ../ failed!");
  34. return ret;
  35. }


三.一些筆記

相關的system call很多,具體的用法我就不列出來了,只列出一些我在看書過程中的筆記(有些是摘自apue)。

1.there is no distinction to the UNIX kernel whether this data is text or binary.

   2.FIFO is a type of file used for communication between processed.It's sometimes called a named pipe.

3.if we used the stat function,we would never see symbolic links.用lstat才能處理symbolic links.

   4. set-user-ID:這個概念要特別注意。比如說,當一個文件被設置了set-user-ID後,而這個文件的owner是root,然後用一個非root的進程執行這個文件,那麼該文件所執行的進程也將擁有root權限。比如說passwd命令就是一個set-user-ID程序。任何用戶都可以修改密碼,但/etc/passwd只能是root才有write的權利,所以這時set-usr_ID就可以發揮作用,讓所有執行passwd的進程都可以向/etc/passwd中write。

5.whenever we want to open any type of file by name ,we must have execute permission in each directory mentioned in the name,including the current directory,if it is implied.This is why the execute permission bit for a directory is often called the search bit.這個比較好理解,對於一個目錄來說,自然不可能去執行目錄,所以目錄的可執行性就是為了執行該目錄下的文件作基礎。

   6.目錄的read權限僅僅是獲得a list of all the filenames in the directory.目錄的read權限與執行權限是很不相同的。

   7.we cannot create a new file in a directory unless we have write permission and execute permission in the directory.

   8.To delete an existing file,we need write permission and execute permission in the directory containing the file.We do not need read permission or write permission for the file itself.以前沒有注意目錄與文件之間的這樣的權限的聯系。

   9.當用gcc編譯生成一個二進制文件後,用chmod u+s來設置set-user-ID。 當再用gcc更新該二進制文件後,set-user-ID就會消失

   10.read系統調用讀的內容是不會自動加上'\0'的,所以在使用printf輸出該內容的時候要手動加上'\0'

   11.open(filename,O_TRunC).就可以清空文件filename

   12.The i-nodes are fixed-length entries that contain most of the information about a file.Most of the information in the stat structure is obtained from the i-node.

   13.對目錄做硬鏈接有可能使得文件系統出現循環,而這個循環是處理文件系統的很多工具都無法解決的。所以非常多的UNIX實現版本都不允許對目錄做硬鏈接。apue上4.16有出現循環的例子

   14.We've mentioned before that to unlink a file,we must have write permission and execute permission in the directory containing the directory entry,as it is the directory entry that we will be removing.Also,we mentioned in Section 4.10(in apue)that if the sticky bit is set in this directory we must have write permission for the directory and one of the following:Own the file;Own the directory;Have superuser privileges.

   15.A symbolic link is an indirect pointer to a file,unlike the hard links,which pointed directly to the i-node of the file.

Copyright © Linux教程網 All Rights Reserved