chan.c 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. #include <u.h>
  2. #include <libc.h>
  3. #include "compat.h"
  4. #include "error.h"
  5. Chan*
  6. newchan(void)
  7. {
  8. Chan *c;
  9. c = smalloc(sizeof(Chan));
  10. /* if you get an error before associating with a dev,
  11. close calls rootclose, a nop */
  12. c->type = 0;
  13. c->flag = 0;
  14. c->ref = 1;
  15. c->dev = 0;
  16. c->offset = 0;
  17. c->iounit = 0;
  18. c->aux = 0;
  19. c->name = 0;
  20. return c;
  21. }
  22. void
  23. chanfree(Chan *c)
  24. {
  25. c->flag = CFREE;
  26. cnameclose(c->name);
  27. }
  28. void
  29. cclose(Chan *c)
  30. {
  31. if(c->flag&CFREE)
  32. panic("cclose %lux", getcallerpc(&c));
  33. if(decref(c))
  34. return;
  35. if(!waserror()) {
  36. devtab[c->type]->close(c);
  37. poperror();
  38. }
  39. chanfree(c);
  40. }
  41. Chan*
  42. cclone(Chan *c)
  43. {
  44. Chan *nc;
  45. Walkqid *wq;
  46. wq = devtab[c->type]->walk(c, nil, nil, 0);
  47. if(wq == nil)
  48. error("clone failed");
  49. nc = wq->clone;
  50. free(wq);
  51. nc->name = c->name;
  52. if(c->name)
  53. incref(c->name);
  54. return nc;
  55. }
  56. enum
  57. {
  58. CNAMESLOP = 20
  59. };
  60. static Ref ncname;
  61. void cleancname(Cname*);
  62. int
  63. isdotdot(char *p)
  64. {
  65. return p[0]=='.' && p[1]=='.' && p[2]=='\0';
  66. }
  67. int
  68. incref(Ref *r)
  69. {
  70. int x;
  71. lock(r);
  72. x = ++r->ref;
  73. unlock(r);
  74. return x;
  75. }
  76. int
  77. decref(Ref *r)
  78. {
  79. int x;
  80. lock(r);
  81. x = --r->ref;
  82. unlock(r);
  83. if(x < 0)
  84. panic("decref");
  85. return x;
  86. }
  87. Cname*
  88. newcname(char *s)
  89. {
  90. Cname *n;
  91. int i;
  92. n = smalloc(sizeof(Cname));
  93. i = strlen(s);
  94. n->len = i;
  95. n->alen = i+CNAMESLOP;
  96. n->s = smalloc(n->alen);
  97. memmove(n->s, s, i+1);
  98. n->ref = 1;
  99. incref(&ncname);
  100. return n;
  101. }
  102. void
  103. cnameclose(Cname *n)
  104. {
  105. if(n == nil)
  106. return;
  107. if(decref(n))
  108. return;
  109. decref(&ncname);
  110. free(n->s);
  111. free(n);
  112. }
  113. Cname*
  114. addelem(Cname *n, char *s)
  115. {
  116. int i, a;
  117. char *t;
  118. Cname *new;
  119. if(s[0]=='.' && s[1]=='\0')
  120. return n;
  121. if(n->ref > 1){
  122. /* copy on write */
  123. new = newcname(n->s);
  124. cnameclose(n);
  125. n = new;
  126. }
  127. i = strlen(s);
  128. if(n->len+1+i+1 > n->alen){
  129. a = n->len+1+i+1 + CNAMESLOP;
  130. t = smalloc(a);
  131. memmove(t, n->s, n->len+1);
  132. free(n->s);
  133. n->s = t;
  134. n->alen = a;
  135. }
  136. if(n->len>0 && n->s[n->len-1]!='/' && s[0]!='/') /* don't insert extra slash if one is present */
  137. n->s[n->len++] = '/';
  138. memmove(n->s+n->len, s, i+1);
  139. n->len += i;
  140. if(isdotdot(s))
  141. cleancname(n);
  142. return n;
  143. }
  144. /*
  145. * In place, rewrite name to compress multiple /, eliminate ., and process ..
  146. */
  147. void
  148. cleancname(Cname *n)
  149. {
  150. char *p;
  151. if(n->s[0] == '#'){
  152. p = strchr(n->s, '/');
  153. if(p == nil)
  154. return;
  155. cleanname(p);
  156. /*
  157. * The correct name is #i rather than #i/,
  158. * but the correct name of #/ is #/.
  159. */
  160. if(strcmp(p, "/")==0 && n->s[1] != '/')
  161. *p = '\0';
  162. }else
  163. cleanname(n->s);
  164. n->len = strlen(n->s);
  165. }
  166. void
  167. isdir(Chan *c)
  168. {
  169. if(c->qid.type & QTDIR)
  170. return;
  171. error(Enotdir);
  172. }