红联Linux门户
Linux帮助

Linux下创建同文件名文件

发布时间:2007-03-02 01:07:49来源:红联作者:BeInOnTheWay
  在linux下创建相同文件名时,系统不会提示该文件已经存在,也不会创建该文件,这与windows的习惯不同,

  我查看了busybox中touch的源代码,

do {
if (utime(*argv, NULL)) {
if (errno == ENOENT) { /* no such file*/
if (flags & 1) { /* Creation is disabled, so ignore. */
continue;
}
/* Try to create the file. */
fd = open(*argv, O_RDWR | O_CREAT,
S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
);
if ((fd >= 0) && !close(fd)) {
continue;
}
}
status = EXIT_FAILURE;
bb_perror_msg("%s", *argv);
}
} while (*++argv);

  发现首先调用了函数utime对指定文件的时间进行更新,如果返回错误,就认为该文件不存在,需要创建,而如果修改时间正确,就会正确返回,系统就不会有任何提示。

56 do {
57 if (utime(*argv, NULL)) {
58 if (errno == ENOENT) { /* no such file*/
59 if (flags & 1) { /* Creation is disabled, so ignore. */
60 continue;
61 }
62 /* Try to create the file. */
63 fd = open(*argv, O_RDWR | O_CREAT,
64 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
65 );
66 if ((fd >= 0) && !close(fd)) {
67 continue;
68 }
69 }
70 }
71 errno = EEXIST;
72 status = EXIT_FAILURE;
73 bb_perror_msg("%s", *argv);
74 } while (*++argv);

  如果修改时间时正确,则强制将返回状态改为EEXIST,并返回,系统就会提示该文件已经存在。
文章评论

共有 0 条评论