Linux 拷貝檔案函數:不知道這個對不對,轉載了再說

來源:互聯網
上載者:User
Linux 拷貝檔案函數2007-12-17 10:56
1 /*

2 * Copy a (real) file to another (real) file.

3 * file can be a regular file or a simlink

4 */

5 int create_copy_file(const char *source, const char *target)

6 {

7         struct stat src_stat;

8

9         if (lstat(source, &src_stat) == -1)

10                 return -1;

11

12         if (S_ISLNK(src_stat.st_mode)) {

13                 char lnk[1024];

14                 int lnk_size;

15                 if ((lnk_size = readlink(source, lnk, 1023)) == -1)

16                         return -2;

17                 lnk[lnk_size] = '\0';

18                 if (symlink(lnk, target) == -1)

19                         return -3;

20         } else if (S_ISREG(src_stat.st_mode)) {

21                 int src, dst;

22                 int rsize;

23                 char buf[1024];

24                 if ((src = open(source, O_RDONLY)) == -1) {

25                         close(dst);

26                         return -4;

27                 }

28                 if ((dst = creat(target, src_stat.st_mode)) == -1)

29                         return -5;

30                 while ((rsize = read(src, buf, 1024))) {

31                         if (rsize == -1 && errno == EINTR)

32                                 continue ;

33                         if (rsize == -1) {

34                                 close(src);

35                                 close(dst);

36                                 return -6;

37                         }

38                         while (write(dst, buf, rsize) == -1)

39                                 if (errno != EINTR) {

40                                 close(src);

41                                 close(dst);

42                                 return -7;

43                         }

44                 }

45                 close(src);

46                 close(dst);

47         } else {

48                 return -8;

49         }

50         return 0;

51 }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.