name.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * linux/fs/hpfs/name.c
  3. *
  4. * Mikulas Patocka (mikulas@artax.karlin.mff.cuni.cz), 1998-1999
  5. *
  6. * operations with filenames
  7. */
  8. #include "hpfs_fn.h"
  9. static char *text_postfix[]={
  10. ".ASM", ".BAS", ".BAT", ".C", ".CC", ".CFG", ".CMD", ".CON", ".CPP", ".DEF",
  11. ".DOC", ".DPR", ".ERX", ".H", ".HPP", ".HTM", ".HTML", ".JAVA", ".LOG", ".PAS",
  12. ".RC", ".TEX", ".TXT", ".Y", ""};
  13. static char *text_prefix[]={
  14. "AUTOEXEC.", "CHANGES", "COPYING", "CONFIG.", "CREDITS", "FAQ", "FILE_ID.DIZ",
  15. "MAKEFILE", "READ.ME", "README", "TERMCAP", ""};
  16. void hpfs_decide_conv(struct inode *inode, unsigned char *name, unsigned len)
  17. {
  18. struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
  19. int i;
  20. if (hpfs_inode->i_conv != CONV_AUTO) return;
  21. for (i = 0; *text_postfix[i]; i++) {
  22. int l = strlen(text_postfix[i]);
  23. if (l <= len)
  24. if (!hpfs_compare_names(inode->i_sb, text_postfix[i], l, name + len - l, l, 0))
  25. goto text;
  26. }
  27. for (i = 0; *text_prefix[i]; i++) {
  28. int l = strlen(text_prefix[i]);
  29. if (l <= len)
  30. if (!hpfs_compare_names(inode->i_sb, text_prefix[i], l, name, l, 0))
  31. goto text;
  32. }
  33. hpfs_inode->i_conv = CONV_BINARY;
  34. return;
  35. text:
  36. hpfs_inode->i_conv = CONV_TEXT;
  37. return;
  38. }
  39. static inline int not_allowed_char(unsigned char c)
  40. {
  41. return c<' ' || c=='"' || c=='*' || c=='/' || c==':' || c=='<' ||
  42. c=='>' || c=='?' || c=='\\' || c=='|';
  43. }
  44. static inline int no_dos_char(unsigned char c)
  45. { /* Characters that are allowed in HPFS but not in DOS */
  46. return c=='+' || c==',' || c==';' || c=='=' || c=='[' || c==']';
  47. }
  48. static inline unsigned char upcase(unsigned char *dir, unsigned char a)
  49. {
  50. if (a<128 || a==255) return a>='a' && a<='z' ? a - 0x20 : a;
  51. if (!dir) return a;
  52. return dir[a-128];
  53. }
  54. unsigned char hpfs_upcase(unsigned char *dir, unsigned char a)
  55. {
  56. return upcase(dir, a);
  57. }
  58. static inline unsigned char locase(unsigned char *dir, unsigned char a)
  59. {
  60. if (a<128 || a==255) return a>='A' && a<='Z' ? a + 0x20 : a;
  61. if (!dir) return a;
  62. return dir[a];
  63. }
  64. int hpfs_chk_name(unsigned char *name, unsigned *len)
  65. {
  66. int i;
  67. if (*len > 254) return -ENAMETOOLONG;
  68. hpfs_adjust_length(name, len);
  69. if (!*len) return -EINVAL;
  70. for (i = 0; i < *len; i++) if (not_allowed_char(name[i])) return -EINVAL;
  71. if (*len == 1) if (name[0] == '.') return -EINVAL;
  72. if (*len == 2) if (name[0] == '.' && name[1] == '.') return -EINVAL;
  73. return 0;
  74. }
  75. char *hpfs_translate_name(struct super_block *s, unsigned char *from,
  76. unsigned len, int lc, int lng)
  77. {
  78. char *to;
  79. int i;
  80. if (hpfs_sb(s)->sb_chk >= 2) if (hpfs_is_name_long(from, len) != lng) {
  81. printk("HPFS: Long name flag mismatch - name ");
  82. for (i=0; i<len; i++) printk("%c", from[i]);
  83. printk(" misidentified as %s.\n", lng ? "short" : "long");
  84. printk("HPFS: It's nothing serious. It could happen because of bug in OS/2.\nHPFS: Set checks=normal to disable this message.\n");
  85. }
  86. if (!lc) return from;
  87. if (!(to = kmalloc(len, GFP_KERNEL))) {
  88. printk("HPFS: can't allocate memory for name conversion buffer\n");
  89. return from;
  90. }
  91. for (i = 0; i < len; i++) to[i] = locase(hpfs_sb(s)->sb_cp_table,from[i]);
  92. return to;
  93. }
  94. int hpfs_compare_names(struct super_block *s, unsigned char *n1, unsigned l1,
  95. unsigned char *n2, unsigned l2, int last)
  96. {
  97. unsigned l = l1 < l2 ? l1 : l2;
  98. unsigned i;
  99. if (last) return -1;
  100. for (i = 0; i < l; i++) {
  101. unsigned char c1 = upcase(hpfs_sb(s)->sb_cp_table,n1[i]);
  102. unsigned char c2 = upcase(hpfs_sb(s)->sb_cp_table,n2[i]);
  103. if (c1 < c2) return -1;
  104. if (c1 > c2) return 1;
  105. }
  106. if (l1 < l2) return -1;
  107. if (l1 > l2) return 1;
  108. return 0;
  109. }
  110. int hpfs_is_name_long(unsigned char *name, unsigned len)
  111. {
  112. int i,j;
  113. for (i = 0; i < len && name[i] != '.'; i++)
  114. if (no_dos_char(name[i])) return 1;
  115. if (!i || i > 8) return 1;
  116. if (i == len) return 0;
  117. for (j = i + 1; j < len; j++)
  118. if (name[j] == '.' || no_dos_char(name[i])) return 1;
  119. return j - i > 4;
  120. }
  121. /* OS/2 clears dots and spaces at the end of file name, so we have to */
  122. void hpfs_adjust_length(unsigned char *name, unsigned *len)
  123. {
  124. if (!*len) return;
  125. if (*len == 1 && name[0] == '.') return;
  126. if (*len == 2 && name[0] == '.' && name[1] == '.') return;
  127. while (*len && (name[*len - 1] == '.' || name[*len - 1] == ' '))
  128. (*len)--;
  129. }