<em id="0a85b"><option id="0a85b"></option></em>

<abbr id="0a85b"></abbr>

      <nobr id="0a85b"></nobr>
        <tr id="0a85b"></tr>
        9久久伊人精品综合,亚洲一区精品视频在线,成 人免费va视频,国产一区二区三区黄网,99国产精品永久免费视频,亚洲毛片多多影院,精品久久久无码人妻中文字幕,无码国产欧美一区二区三区不卡
        學習啦>學習電腦>操作系統>Linux教程>

        linux的rm命令源碼

        時間: 佳洲1085 分享

          Linux下的rm命令是個刪除文件命令那么它的源代碼是怎樣的呢?下面由學習啦小編為大家整理了linux下rm命令源碼的相關知識,希望對大家有幫助!

          linux的rm命令源碼分析

          1.原因

          想要在刪除文件前,先覆蓋文件內容,防止他人恢復文件,從而得到文件原內容;并且需要支持rm命令原本的參數選項:

          NAME rm - remove files or directoriesSYNOPSIS rm [OPTION]... FILE...Remove (unlink) the FILE(s). -f, --force ignore nonexistent files, never prompt -i prompt before every removal -I prompt once before removing more than three files, or when removing recursively. Less intru- sive than -i, while still giving protection against most mistakes ......

          想來還是直接修改rm源文件比較方便,因而查看rm命令的源碼文件,在調用刪除系統調用前加入覆蓋文件內容的操作,從而安全刪除文件,并且支持rm命令的參數選項。

          2.獲取linux命令源碼

          可以通過三條命令獲得命令的源代碼網址

          which rm //得到文件的絕對路徑

          rpm -qf 路徑 //獲取該命令所屬的軟件包名

          rpm -qi 包名 //獲取包信息,包含網址URL信息

          如下:

          [ty@ty ~]$ which rm/bin/rm[ty@ty ~]$ rpm -qf /bin/rmcoreutils-8.4-19.el6.x86_64[ty@ty ~]$ rpm -qi coreutilsName : coreutils Relocations: (not relocatable)Version : 8.4 Vendor: Red Hat, Inc.Release : 19.el6 Build Date: Tue 17 Apr 2012 06:14:13 AM PDTInstall Date: Sun 04 Aug 2013 11:48:59 AM PDT Build Host: hs20-bc2-3.build.redhat.comGroup : System Environment/Base Source RPM: coreutils-8.4-19.el6.src.rpmSize : 12847030 License: GPLv3+Signature : RSA/8, Thu 19 Apr 2012 10:59:38 PM PDT, Key ID 199e2f91fd431d51Packager : Red Hat, Inc. <http://bugzilla.redhat.com/bugzilla>URL : http://www.gnu.org/software/coreutils/Summary : A set of basic GNU tools commonly used in shell scriptsDescription :These are the GNU core utilities. This package is the combination ofthe old GNU fileutils, sh-utils, and textutils packages.

          linux的rm命令源碼

          rm.c主函數main

          rm命令函數調用流程:

          man() -> rm() -> rm_fts() -> excise() -> unlinkat()

          int main (int argc, char **argv){ bool preserve_root = true; struct rm_options x; bool prompt_once = false; int c; initialize_main (&argc, &argv); //初始化輸入參數 set_program_name (argv[0]); //設置程序名 setlocale (LC_ALL, ""); bindtextdomain (PACKAGE, LOCALEDIR); textdomain (PACKAGE); /* 程序正常結束前調用的close_stdin函數,關閉標準輸入; * 一個程序最多可以用atexit()注冊32個處理函數,這些處理函數的 * 調用順序與其注冊順序相反。即就是先注冊的最后調用,最后調用的最先調用*/ atexit (close_stdin); rm_option_init (&x); //初始化rm選項,即就是rm命令的參數 /* Try to disable the ability to unlink a directory. */ priv_set_remove_linkdir (); /*試圖去禁用刪除目錄的功能,Try to remove priv from the effective set*/ while ((c = getopt_long (argc, argv, "dfirvIR", long_opts, NULL)) != -1) { switch (c) //switch語句主要是根據輸入參數選項設置刪除選項 { case 'f': x.interactive = RMI_NEVER; x.ignore_missing_files = true; prompt_once = false; break; case 'i': x.interactive = RMI_ALWAYS; x.ignore_missing_files = false; prompt_once = false; break;

          case 'r': case 'R': x.recursive = true; break; ...... case_GETOPT_HELP_CHAR; case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS); default: diagnose_leading_hyphen (argc, argv); usage (EXIT_FAILURE); } } if (argc <= optind) /*參數小于等于optind,即就是沒有輸入要刪除的filename*/ { if (x.ignore_missing_files) /*指定了-f選項時,直接退出,否則輸出信息提示用戶為指定操作對象*/ exit (EXIT_SUCCESS); else { error (0, 0, _("missing operand")); usage (EXIT_FAILURE); } } if (x.recursive && preserve_root) { static struct dev_ino dev_ino_buf; x.root_dev_ino = get_root_dev_ino (&dev_ino_buf); if (x.root_dev_ino == NULL) error (EXIT_FAILURE, errno, _("failed to get attributes of %s"), quote ("/")); } size_t n_files = argc - optind; /*n_files存儲命令行指定操作對象個數,即就是要刪除文件個數(目錄視為一個)*/ char **file = argv + optind; /*file存儲操作對象的索引*/ if (prompt_once && (x.recursive || 3 < n_files)) { fprintf (stderr, (x.recursive ? _("%s: remove all arguments recursively? ") : _("%s: remove all arguments? ")), program_name); if (!yesno ()) exit (EXIT_SUCCESS); } enum RM_status status = rm (file, &x); /*刪除文件,返回刪除狀態*/ assert (VALID_STATUS (status)); exit (status == RM_ERROR ? EXIT_FAILURE : EXIT_SUCCESS);}

          rm()函數

          /* Remove FILEs, honoring options specified via X. Return RM_OK if successful. */enum RM_statusrm (char *const *file, struct rm_options const *x){ enum RM_status rm_status = RM_OK; if (*file) { int bit_flags = (FTS_CWDFD | FTS_NOSTAT | FTS_PHYSICAL); if (x->one_file_system) bit_flags |= FTS_XDEV; FTS *fts = xfts_open (file, bit_flags, NULL); //創建并填充FTS結構體部分成員信息,會調用fts_open函數,失敗返回失敗的診斷信息 while (1) { FTSENT *ent; ent = fts_read (fts); //填充FTSENT結構體成員信息 if (ent == NULL) { if (errno != 0) { error (0, errno, _("fts_read failed")); rm_status = RM_ERROR; } break; } enum RM_status s = rm_fts (fts, ent, x); assert (VALID_STATUS (s)); UPDATE_STATUS (rm_status, s); } if (fts_close (fts) != 0) { error (0, errno, _("fts_close failed")); rm_status = RM_ERROR; } } return rm_status;}

          rn_fts()函數根據FTSENT結構體成員信息,做一些處理,然后調用excise()函數執行刪除操作;

          excise函數調用unlinkat系統函數

          /* Remove the file system object specified by ENT. IS_DIR specifies whether it is expected to be a directory or non-directory. Return RM_OK upon success, else RM_ERROR. */static enum RM_statusexcise (FTS *fts, FTSENT *ent, struct rm_options const *x, bool is_dir){ int flag = is_dir ? AT_REMOVEDIR : 0; if (unlinkat (fts->fts_cwd_fd, ent->fts_accpath, flag) == 0) { /* 使用了-v選項時,輸出詳細刪除信息,是目錄輸出目錄名,是文件輸出文件名, 輸出的是當前目錄到刪除文件的完全路徑(" `/tmp/aa' "或者"`test/bb'") */ if (x->verbose) { printf ((is_dir ? _("removed directory: %s\n") : _("removed %s\n")), quote (ent->fts_path)); /*quote是將輸出用(`')反引號單引號括起來*/ } return RM_OK; } //下邊為unlinkat函數執行失敗,做的一些處理 /* The unlinkat from kernels like linux-2.6.32 reports EROFS even for nonexistent files. When the file is indeed missing, map that to ENOENT, so that rm -f ignores it, as required. Even without -f, this is useful because it makes rm print the more precise diagnostic. */ if (errno == EROFS) { struct stat st; if ( ! (lstatat (fts->fts_cwd_fd, ent->fts_accpath, &st) && errno == ENOENT)) errno = EROFS; } if (ignorable_missing (x, errno)) return RM_OK; /* When failing to rmdir an unreadable directory, the typical errno value is EISDIR, but that is not as useful to the user as the errno value from the failed open (probably EPERM). Use the earlier, more descriptive errno value. */ if (ent->fts_info == FTS_DNR) errno = ent->fts_errno; error (0, errno, _("cannot remove %s"), quote (ent->fts_path)); mark_ancestor_dirs (ent); return RM_ERROR;}

          添加的覆蓋文件內容的操作就是添加在unlinkat函數前邊,因為上幾步已經做好了rm命令選項的操作,使用不同參數的不同操作,unlinkat函數是最終執行刪除操作的:

          int flag = is_dir ? AT_REMOVEDIR : 0;if(flag == 0) //根據flag標志判斷是文件還是目錄,如果是文件才執行覆蓋操作,否則不做任何操作{ //覆蓋操作}if (unlinkat (fts->fts_cwd_fd, ent->fts_accpath, flag) == 0)

        linux的rm命令源碼

        Linux下的rm命令是個刪除文件命令那么它的源代碼是怎樣的呢?下面由學習啦小編為大家整理了linux下rm命令源碼的相關知識,希望對大家有幫助! linux的rm命令源碼分析 1.原因 想要在刪除文件前,先覆蓋文件內容,防止他人恢復文件,從而得
        推薦度:
        點擊下載文檔文檔為doc格式
        3630508 主站蜘蛛池模板: 国内精品久久久久影院网站| 中文字幕亚洲综合第一页| 日本一卡2卡3卡四卡精品网站| 国产成人黄色自拍小视频| 久久国产精品色av免费看| 奇米四色7777中文字幕| 国产成人a在线观看视频免费| 天天综合亚洲色在线精品| 亚洲欧美成人aⅴ在线| 无码一区+中文字幕| 内射干少妇亚洲69XXX| 亚洲精品爆乳一区二区H| 久久 午夜福利 张柏芝| 99久久精品国产一区二区蜜芽| 欧洲熟妇熟女久久精品综合| 91久久天天躁狠狠躁夜夜| 亚洲欧美日韩久久一区二区| 性夜黄a爽影免费看| 亚洲AV旡码高清在线观看| 久久精品99久久久久久久久 | 国产欧美久久一区二区| 精品国产福利一区二区在线| 久久精品国产亚洲av麻豆甜| 99riav国产精品视频| 亚洲国产精品一区第二页| 日韩一区二区三区理伦片| 日本精品videossex黑人| 国产91丝袜在线播放动漫| 亚洲国产精品久久久久秋霞| 99在线视频免费| 虎白女粉嫩尤物福利视频| 成人国产一区二区精品 | 国产精品美女久久久久av爽| 婷婷色爱区综合五月激情韩国| 国产精品www夜色影视| 国产不卡一区二区三区视频| 亚洲熟妇在线视频观看| 免费超爽大片黄| 亚洲不卡av不卡一区二区| 大地资源中文在线观看西瓜| 国产综合色产在线精品 |