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