unlnfs.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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 <bio.h>
  12. #include <libsec.h>
  13. enum
  14. {
  15. ENCLEN = 26,
  16. };
  17. typedef struct Name Name;
  18. struct Name {
  19. char shortname[ENCLEN + 1];
  20. char* longname;
  21. Name* next;
  22. };
  23. Name *names;
  24. void rename(char*, char*, char*);
  25. void renamedir(char*);
  26. void readnames(char*);
  27. void
  28. main(int argc, char **argv)
  29. {
  30. char lnfile[256], *d;
  31. d = ".";
  32. if(argc > 1)
  33. d = argv[1];
  34. snprint(lnfile, sizeof(lnfile), "%s/.longnames", d);
  35. readnames(lnfile);
  36. renamedir(d);
  37. }
  38. void
  39. renamedir(char *d)
  40. {
  41. int n;
  42. Dir *dir;
  43. char *sub;
  44. int fd, i;
  45. Name *na;
  46. fd = open(d, OREAD);
  47. if (fd == -1)
  48. return;
  49. while((n = dirread(fd, &dir)) > 0){
  50. for(i = 0; i < n; i++){
  51. if(dir[i].mode & DMDIR){
  52. sub = malloc(strlen(d) + 1 + strlen(dir[i].name) + 1);
  53. sprint(sub, "%s/%s", d, dir[i].name);
  54. renamedir(sub);
  55. free(sub);
  56. }
  57. if(strlen(dir[i].name) != ENCLEN)
  58. continue;
  59. for (na = names; na != nil; na = na->next){
  60. if (strcmp(na->shortname, dir[i].name) == 0){
  61. rename(d, dir[i].name, na->longname);
  62. break;
  63. }
  64. }
  65. }
  66. free(dir);
  67. }
  68. close(fd);
  69. }
  70. void
  71. rename(char *d, char *old, char *new)
  72. {
  73. char *p;
  74. Dir dir;
  75. p = malloc(strlen(d) + 1 + strlen(old) + 1);
  76. sprint(p, "%s/%s", d, old);
  77. nulldir(&dir);
  78. dir.name = new;
  79. if(dirwstat(p, &dir) == -1)
  80. fprint(2, "unlnfs: cannot rename %s to %s: %r\n", p, new);
  81. free(p);
  82. }
  83. void
  84. long2short(char shortname[ENCLEN+1], char *longname)
  85. {
  86. uint8_t digest[MD5dlen];
  87. md5((uint8_t*)longname, strlen(longname), digest, nil);
  88. enc32(shortname, ENCLEN+1, digest, MD5dlen);
  89. }
  90. void
  91. readnames(char *lnfile)
  92. {
  93. Biobuf *bio;
  94. char *f;
  95. Name *n;
  96. bio = Bopen(lnfile, OREAD);
  97. if(bio == nil){
  98. fprint(2, "unlnfs: cannot open %s: %r\n", lnfile);
  99. exits("error");
  100. }
  101. while((f = Brdstr(bio, '\n', 1)) != nil){
  102. n = malloc(sizeof(Name));
  103. n->longname = f;
  104. long2short(n->shortname, f);
  105. n->next = names;
  106. names = n;
  107. }
  108. Bterm(bio);
  109. }