Читаем Linux программирование в примерах полностью

1  /* Эта функция вызывается один раз для каждого объекта файловой

2     системы, с которой сталкивается nftw. nftw осуществляет сначала

3     поиск вглубь. Эта функция знает это и собирает итоги для каталогов

4     на основе изменений в глубине текущего элемента. */

5

6  static int

7  process_file(const char *file, const struct stat *sb,

8   int file_type, struct FTW *info)

9  {

10  uintmax_t size;

11  uintmax_t size_to_print;

12  static int first_call = 1;

13  static size_t prev_level;

14  static size_t n_alloc;

15  static uintmax_t *sum_ent;

16  static uintmax_t *sum_subdir;

17  int print = 1;

18

19  /* Всегда определяйте info->skip перед возвратом. */

20  info->skip = excluded_filename(exclude, file + info->base);

    /* Для --exclude */

Эта функция делает многое, поскольку ей приходится реализовать все опции du. Строка 17 устанавливает print в true (1); по умолчанию выводятся сведения о каждом файле. Дальнейший код устанавливает ее при необходимости в false (0).

Строка 20 устанавливает info->skip на основе опции --exclude. Обратите внимание, что это исключает подкаталоги, если каталог совпадает с шаблоном для --exclude.

22 switch (file_type)

23 {

24 case FTW_NS:

25  error (0, errno, _("cannot access %s"), quote(file));

26  G_fail = 1; /* Установить глобальную переменную для дальнейшего */

27  return 0; /* Вернуть 0 для продолжения */

28

29 case FTW_DCHP:

30  error(0, errno, _("cannot change to parent of directory %s"),

31  quote(file));

32  G_fail = 1;

33  return 0;

34

35 case FTW_DCH:

36  /* Нельзя просто вернуться, поскольку, хотя nftw не может войти в

37     каталог, она может использовать stat, постольку у нас есть размер */

38  error(0, errno, _("cannot change to directory %s"), quote(file));

39  G_fail = 1;

40  break;

41

42 case FTW_DNR:

43  /* Нельзя просто вернуться, поскольку, хотя nftw не может прочесть

44     каталог, она может вызвать stat, постольку у нас есть размер. */

45  error(0, errno, _("cannot read directory %s"), quote(file));

46  G_fail = 1;

47  break;

48

49 default:

50  break;

51 }

52

53 /* Если это первая (предварительная) встреча с каталогом,

54    сразу вернуться. */

55 if (file_type == FTW_DPRE)

56  return 0;

Строки 22–51 являются стандартным оператором switch. Ошибки, для которых нет информации о размере, устанавливают глобальную переменную G_fail в 1 и возвращают 0, чтобы продолжить обработку (см строки 24–27 и 29–33). Ошибки, для которых есть размер, также устанавливают G_fail, но затем прерывают switch для того, чтобы обработать статистику (см. строки 35–40 и 42–47).

Строки 55–56 сразу завершают функцию, если это первая встреча с каталогом

58 /* Если файл исключается или если он уже учитывался

59    через прямую ссылку, не включать его в сумму. */

60 if (info->skip,

61  || (!opt_count_all

62  && 1 < sb->st_nlink

63  && hash_ins(sb->st_ino, sb->st_dev)))

64 {

65  /* Заметьте, мы не должны здесь просто возвращаться.

Перейти на страницу:

Похожие книги

C++ Primer Plus
C++ Primer Plus

C++ Primer Plus is a carefully crafted, complete tutorial on one of the most significant and widely used programming languages today. An accessible and easy-to-use self-study guide, this book is appropriate for both serious students of programming as well as developers already proficient in other languages.The sixth edition of C++ Primer Plus has been updated and expanded to cover the latest developments in C++, including a detailed look at the new C++11 standard.Author and educator Stephen Prata has created an introduction to C++ that is instructive, clear, and insightful. Fundamental programming concepts are explained along with details of the C++ language. Many short, practical examples illustrate just one or two concepts at a time, encouraging readers to master new topics by immediately putting them to use.Review questions and programming exercises at the end of each chapter help readers zero in on the most critical information and digest the most difficult concepts.In C++ Primer Plus, you'll find depth, breadth, and a variety of teaching techniques and tools to enhance your learning:• A new detailed chapter on the changes and additional capabilities introduced in the C++11 standard• Complete, integrated discussion of both basic C language and additional C++ features• Clear guidance about when and why to use a feature• Hands-on learning with concise and simple examples that develop your understanding a concept or two at a time• Hundreds of practical sample programs• Review questions and programming exercises at the end of each chapter to test your understanding• Coverage of generic C++ gives you the greatest possible flexibility• Teaches the ISO standard, including discussions of templates, the Standard Template Library, the string class, exceptions, RTTI, and namespaces

Стивен Прата

Программирование, программы, базы данных