unlnfs.c 1.9 KB

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