blob: f24736d7a439218aa238f75df3338c3e3e64744f [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
9#include "hpfs_fn.h"
10
Al Viro7e7742e2010-01-31 17:09:29 -050011static const char *text_postfix[]={
Linus Torvalds1da177e2005-04-16 15:20:36 -070012".ASM", ".BAS", ".BAT", ".C", ".CC", ".CFG", ".CMD", ".CON", ".CPP", ".DEF",
13".DOC", ".DPR", ".ERX", ".H", ".HPP", ".HTM", ".HTML", ".JAVA", ".LOG", ".PAS",
14".RC", ".TEX", ".TXT", ".Y", ""};
15
Al Viro7e7742e2010-01-31 17:09:29 -050016static const char *text_prefix[]={
Linus Torvalds1da177e2005-04-16 15:20:36 -070017"AUTOEXEC.", "CHANGES", "COPYING", "CONFIG.", "CREDITS", "FAQ", "FILE_ID.DIZ",
18"MAKEFILE", "READ.ME", "README", "TERMCAP", ""};
19
Al Viro7e7742e2010-01-31 17:09:29 -050020void hpfs_decide_conv(struct inode *inode, const unsigned char *name, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070021{
22 struct hpfs_inode_info *hpfs_inode = hpfs_i(inode);
23 int i;
24 if (hpfs_inode->i_conv != CONV_AUTO) return;
25 for (i = 0; *text_postfix[i]; i++) {
26 int l = strlen(text_postfix[i]);
27 if (l <= len)
28 if (!hpfs_compare_names(inode->i_sb, text_postfix[i], l, name + len - l, l, 0))
29 goto text;
30 }
31 for (i = 0; *text_prefix[i]; i++) {
32 int l = strlen(text_prefix[i]);
33 if (l <= len)
34 if (!hpfs_compare_names(inode->i_sb, text_prefix[i], l, name, l, 0))
35 goto text;
36 }
37 hpfs_inode->i_conv = CONV_BINARY;
38 return;
39 text:
40 hpfs_inode->i_conv = CONV_TEXT;
41 return;
42}
43
44static inline int not_allowed_char(unsigned char c)
45{
46 return c<' ' || c=='"' || c=='*' || c=='/' || c==':' || c=='<' ||
47 c=='>' || c=='?' || c=='\\' || c=='|';
48}
49
50static inline int no_dos_char(unsigned char c)
51{ /* Characters that are allowed in HPFS but not in DOS */
52 return c=='+' || c==',' || c==';' || c=='=' || c=='[' || c==']';
53}
54
55static inline unsigned char upcase(unsigned char *dir, unsigned char a)
56{
57 if (a<128 || a==255) return a>='a' && a<='z' ? a - 0x20 : a;
58 if (!dir) return a;
59 return dir[a-128];
60}
61
62unsigned char hpfs_upcase(unsigned char *dir, unsigned char a)
63{
64 return upcase(dir, a);
65}
66
67static inline unsigned char locase(unsigned char *dir, unsigned char a)
68{
69 if (a<128 || a==255) return a>='A' && a<='Z' ? a + 0x20 : a;
70 if (!dir) return a;
71 return dir[a];
72}
73
Al Viro7e7742e2010-01-31 17:09:29 -050074int hpfs_chk_name(const unsigned char *name, unsigned *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075{
76 int i;
77 if (*len > 254) return -ENAMETOOLONG;
78 hpfs_adjust_length(name, len);
79 if (!*len) return -EINVAL;
80 for (i = 0; i < *len; i++) if (not_allowed_char(name[i])) return -EINVAL;
81 if (*len == 1) if (name[0] == '.') return -EINVAL;
82 if (*len == 2) if (name[0] == '.' && name[1] == '.') return -EINVAL;
83 return 0;
84}
85
Al Viro7e7742e2010-01-31 17:09:29 -050086unsigned char *hpfs_translate_name(struct super_block *s, unsigned char *from,
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 unsigned len, int lc, int lng)
88{
Al Viro7e7742e2010-01-31 17:09:29 -050089 unsigned char *to;
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 int i;
91 if (hpfs_sb(s)->sb_chk >= 2) if (hpfs_is_name_long(from, len) != lng) {
92 printk("HPFS: Long name flag mismatch - name ");
93 for (i=0; i<len; i++) printk("%c", from[i]);
94 printk(" misidentified as %s.\n", lng ? "short" : "long");
95 printk("HPFS: It's nothing serious. It could happen because of bug in OS/2.\nHPFS: Set checks=normal to disable this message.\n");
96 }
97 if (!lc) return from;
98 if (!(to = kmalloc(len, GFP_KERNEL))) {
99 printk("HPFS: can't allocate memory for name conversion buffer\n");
100 return from;
101 }
102 for (i = 0; i < len; i++) to[i] = locase(hpfs_sb(s)->sb_cp_table,from[i]);
103 return to;
104}
105
Al Viro7e7742e2010-01-31 17:09:29 -0500106int hpfs_compare_names(struct super_block *s,
107 const unsigned char *n1, unsigned l1,
108 const unsigned char *n2, unsigned l2, int last)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 unsigned l = l1 < l2 ? l1 : l2;
111 unsigned i;
112 if (last) return -1;
113 for (i = 0; i < l; i++) {
114 unsigned char c1 = upcase(hpfs_sb(s)->sb_cp_table,n1[i]);
115 unsigned char c2 = upcase(hpfs_sb(s)->sb_cp_table,n2[i]);
116 if (c1 < c2) return -1;
117 if (c1 > c2) return 1;
118 }
119 if (l1 < l2) return -1;
120 if (l1 > l2) return 1;
121 return 0;
122}
123
Al Viro7e7742e2010-01-31 17:09:29 -0500124int hpfs_is_name_long(const unsigned char *name, unsigned len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 int i,j;
127 for (i = 0; i < len && name[i] != '.'; i++)
128 if (no_dos_char(name[i])) return 1;
129 if (!i || i > 8) return 1;
130 if (i == len) return 0;
131 for (j = i + 1; j < len; j++)
132 if (name[j] == '.' || no_dos_char(name[i])) return 1;
133 return j - i > 4;
134}
135
136/* OS/2 clears dots and spaces at the end of file name, so we have to */
137
Al Viro7e7742e2010-01-31 17:09:29 -0500138void hpfs_adjust_length(const unsigned char *name, unsigned *len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
140 if (!*len) return;
141 if (*len == 1 && name[0] == '.') return;
142 if (*len == 2 && name[0] == '.' && name[1] == '.') return;
143 while (*len && (name[*len - 1] == '.' || name[*len - 1] == ' '))
144 (*len)--;
145}