br.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /*
  2. * Copyright (C) 2005-2009 Junjiro Okajima
  3. *
  4. * This program, aufs is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #define _GNU_SOURCE /* strndup */
  19. #include <sys/stat.h>
  20. #include <sys/types.h>
  21. #include <mntent.h>
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <linux/aufs_type.h>
  27. #include "au_util.h"
  28. static int by_opts(char ***br, int *nbr, char *bropt)
  29. {
  30. char *p, **a;
  31. int l;
  32. /* bropts is placed at the end of mnt_opts */
  33. errno = EINVAL;
  34. //puts(bropt);
  35. if (strchr(bropt, ','))
  36. AuFin("%s", bropt);
  37. l = strlen(bropt);
  38. p = malloc(l + 2);
  39. if (!p)
  40. AuFin("malloc");
  41. memcpy(p, bropt, l + 1);
  42. bropt = p;
  43. bropt[l + 1] = 0; /* end marker */
  44. *nbr = 1;
  45. while (1) {
  46. p = strchr(p + 1, ':');
  47. if (!p)
  48. break;
  49. *p = 0;
  50. (*nbr)++;
  51. }
  52. a = malloc(sizeof(a) * (*nbr + 1));
  53. if (!a)
  54. AuFin("malloc");
  55. *br = a;
  56. *a++ = bropt;
  57. p = bropt;
  58. while (*p) {
  59. p += strlen(p) + 1;
  60. *a++ = p;
  61. }
  62. *--a = NULL;
  63. /* don't free bropt */
  64. return 0;
  65. }
  66. #ifdef DEBUG
  67. #define SiPathPrefix "/tmp/aufs/si_"
  68. #define BufSiz 4
  69. #else
  70. #define SiPathPrefix "/sys/fs/aufs/si_"
  71. #define BufSiz BUFSIZ
  72. #endif
  73. static int by_sysfs(char ***br, int *nbr, char *siopt)
  74. {
  75. int err, i, l, sz;
  76. char buf[BufSiz], path[] = SiPathPrefix "1234567890123456/br32767";
  77. char *p, *end, **a, *q;
  78. FILE *fp;
  79. errno = EINVAL;
  80. end = strchr(siopt, ',');
  81. if (end)
  82. i = end - siopt;
  83. else
  84. i = strlen(siopt);
  85. strncpy(path + sizeof(SiPathPrefix) - 1, siopt, i);
  86. p = path + sizeof(SiPathPrefix) - 1 + i;
  87. strcpy(p, "/br");
  88. p += 3; /* "/br" */
  89. *nbr = 0;
  90. err = 0;
  91. while (!err) {
  92. sprintf(p, "%d", (*nbr)++);
  93. err = access(path, F_OK);
  94. }
  95. a = malloc(sizeof(*br) * *nbr);
  96. if (!a)
  97. AuFin("malloc");
  98. (*nbr)--;
  99. *br = a;
  100. for (i = 0; i < *nbr; i++) {
  101. sprintf(p, "%d", i);
  102. fp = fopen(path, "r");
  103. if (!fp)
  104. AuFin("%s", path);
  105. if (fgets(buf, sizeof(buf), fp) != buf)
  106. AuFin("%s", path);
  107. l = strlen(buf);
  108. if (l < 1)
  109. AuFin("internal error, %d", l);
  110. q = strndup(buf, l - 1);
  111. if (buf[l - 1] != '\n') {
  112. /* a branch path with crazy length */
  113. /* stat(2) for sysfs is meaningless */
  114. sz = sizeof(buf);
  115. do {
  116. free(q);
  117. sz <<= 1;
  118. q = malloc(sz);
  119. if (!q)
  120. AuFin("malloc");
  121. rewind(fp);
  122. if (fgets(q, sz, fp) != q)
  123. AuFin("%s", path);
  124. l = strlen(q);
  125. } while (q[l - 1] != '\n');
  126. q[l - 1] = 0;
  127. }
  128. *a++ = q;
  129. /* don't free q */
  130. fclose(fp); /* ignore */
  131. }
  132. *a = NULL;
  133. return 0;
  134. }
  135. #define BrOpt ",br:"
  136. #define SiOpt "si"
  137. int au_br(char ***br, int *nbr, struct mntent *ent)
  138. {
  139. char *p;
  140. *nbr = 0;
  141. p = strstr(ent->mnt_opts, BrOpt);
  142. if (p)
  143. return by_opts(br, nbr, p + sizeof(BrOpt) - 1);
  144. p = hasmntopt(ent, SiOpt);
  145. if (p)
  146. return by_sysfs(br, nbr, p + sizeof(SiOpt));
  147. /* broken opts */
  148. AuFin("internal error, %s", ent->mnt_opts);
  149. return -1; /* never reach here */
  150. }