chan.c 3.0 KB

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