Fossil SCM

Refactor file_perm(), file_islink(), and file_isexe(). Now file_perm() calls stat only once.

dmitry 2011-09-01 22:56 symlinks
Commit eac23495a950cdd232e10b507de516bec89fdd12
1 file changed +35 -40
+35 -40
--- src/file.c
+++ src/file.c
@@ -100,16 +100,13 @@
100100
*/
101101
int file_isfile_or_link(const char *zFilename){
102102
#if !defined(_WIN32)
103103
if ( g.allowSymlinks ){
104104
return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode) || S_ISLNK(fileStat.st_mode);
105
- }else{
106
- return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
107105
}
108
-#else
109
- return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
110106
#endif
107
+ return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
111108
}
112109
113110
/*
114111
** Return TRUE if the named file is an ordinary file. Return false
115112
** for directories, devices, fifos, symlinks, etc.
@@ -116,28 +113,10 @@
116113
*/
117114
int file_isfile(const char *zFilename){
118115
return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
119116
}
120117
121
-/*
122
-** Return TRUE if the named file is a symlink and symlinks are allowed.
123
-** Return false for all other cases.
124
-**
125
-** On Windows, always return False.
126
-*/
127
-int file_islink(const char *zFilename){
128
-#if !defined(_WIN32)
129
- if( g.allowSymlinks ){
130
- return getStat(zFilename) ? 0 : S_ISLNK(fileStat.st_mode);
131
- }else{
132
- return 0;
133
- }
134
-#else
135
- return 0;
136
-#endif
137
-}
138
-
139118
/*
140119
** Create symlink to file on Unix, or plain-text file with
141120
** symlink target if "allow-symlinks" is off or we're on Windows.
142121
**
143122
** Arguments: target file (symlink will point to it), link file
@@ -180,36 +159,52 @@
180159
blob_reset(&content);
181160
}
182161
}
183162
184163
/*
185
-** Return TRUE if the named file is an executable. Return false
186
-** for directories, devices, fifos, symlinks, etc.
164
+** Return file permissions (normal, executable, or symlink):
165
+** - PERM_EXE if file is executable;
166
+** - PERM_LNK on Unix if file is symlink and allow-symlinks option is on;
167
+** - PERM_REG for all other cases (regular file, directory, fifo, etc).
187168
*/
188
-int file_isexe(const char *zFilename){
189
- if( getStat(zFilename) || !S_ISREG(fileStat.st_mode) ) return 0;
169
+int file_perm(const char *zFilename){
170
+ if( getStat(zFilename) ) return PERM_REG;
190171
#if defined(_WIN32)
191172
# if defined(__DMC__) || defined(_MSC_VER)
192173
# define S_IXUSR _S_IEXEC
193174
# endif
194
- return ((S_IXUSR)&fileStat.st_mode)!=0;
175
+ if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
176
+ return PERM_EXE;
177
+ else
178
+ return PERM_REG;
195179
#else
196
- return ((S_IXUSR|S_IXGRP|S_IXOTH)&fileStat.st_mode)!=0;
197
-#endif
198
-}
199
-
200
-
201
-/*
202
-** Return file "permissions" (normal, executable, or symlink).
203
-*/
204
-int file_perm(const char *zFilename){
205
- /*TODO(dchest): optimize by calling stat once.*/
206
- if( file_isexe(zFilename) )
180
+ if( S_ISREG(fileStat.st_mode) &&
181
+ ((S_IXUSR|S_IXGRP|S_IXOTH)&fileStat.st_mode)!=0 )
207182
return PERM_EXE;
208
- if( file_islink(zFilename) )
183
+ else if( g.allowSymlinks && S_ISLNK(fileStat.st_mode) )
209184
return PERM_LNK;
210
- return PERM_REG;
185
+ else
186
+ return PERM_REG;
187
+#endif
188
+}
189
+
190
+/*
191
+** Return TRUE if the named file is an executable. Return false
192
+** for directories, devices, fifos, symlinks, etc.
193
+*/
194
+int file_isexe(const char *zFilename){
195
+ return file_perm(zFilename)==PERM_EXE;
196
+}
197
+
198
+/*
199
+** Return TRUE if the named file is a symlink and symlinks are allowed.
200
+** Return false for all other cases.
201
+**
202
+** On Windows, always return False.
203
+*/
204
+int file_islink(const char *zFilename){
205
+ return file_perm(zFilename)==PERM_LNK;
211206
}
212207
213208
/*
214209
** Return 1 if zFilename is a directory. Return 0 if zFilename
215210
** does not exist. Return 2 if zFilename exists but is something
216211
--- src/file.c
+++ src/file.c
@@ -100,16 +100,13 @@
100 */
101 int file_isfile_or_link(const char *zFilename){
102 #if !defined(_WIN32)
103 if ( g.allowSymlinks ){
104 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode) || S_ISLNK(fileStat.st_mode);
105 }else{
106 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
107 }
108 #else
109 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
110 #endif
 
111 }
112
113 /*
114 ** Return TRUE if the named file is an ordinary file. Return false
115 ** for directories, devices, fifos, symlinks, etc.
@@ -116,28 +113,10 @@
116 */
117 int file_isfile(const char *zFilename){
118 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
119 }
120
121 /*
122 ** Return TRUE if the named file is a symlink and symlinks are allowed.
123 ** Return false for all other cases.
124 **
125 ** On Windows, always return False.
126 */
127 int file_islink(const char *zFilename){
128 #if !defined(_WIN32)
129 if( g.allowSymlinks ){
130 return getStat(zFilename) ? 0 : S_ISLNK(fileStat.st_mode);
131 }else{
132 return 0;
133 }
134 #else
135 return 0;
136 #endif
137 }
138
139 /*
140 ** Create symlink to file on Unix, or plain-text file with
141 ** symlink target if "allow-symlinks" is off or we're on Windows.
142 **
143 ** Arguments: target file (symlink will point to it), link file
@@ -180,36 +159,52 @@
180 blob_reset(&content);
181 }
182 }
183
184 /*
185 ** Return TRUE if the named file is an executable. Return false
186 ** for directories, devices, fifos, symlinks, etc.
 
 
187 */
188 int file_isexe(const char *zFilename){
189 if( getStat(zFilename) || !S_ISREG(fileStat.st_mode) ) return 0;
190 #if defined(_WIN32)
191 # if defined(__DMC__) || defined(_MSC_VER)
192 # define S_IXUSR _S_IEXEC
193 # endif
194 return ((S_IXUSR)&fileStat.st_mode)!=0;
 
 
 
195 #else
196 return ((S_IXUSR|S_IXGRP|S_IXOTH)&fileStat.st_mode)!=0;
197 #endif
198 }
199
200
201 /*
202 ** Return file "permissions" (normal, executable, or symlink).
203 */
204 int file_perm(const char *zFilename){
205 /*TODO(dchest): optimize by calling stat once.*/
206 if( file_isexe(zFilename) )
207 return PERM_EXE;
208 if( file_islink(zFilename) )
209 return PERM_LNK;
210 return PERM_REG;
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
211 }
212
213 /*
214 ** Return 1 if zFilename is a directory. Return 0 if zFilename
215 ** does not exist. Return 2 if zFilename exists but is something
216
--- src/file.c
+++ src/file.c
@@ -100,16 +100,13 @@
100 */
101 int file_isfile_or_link(const char *zFilename){
102 #if !defined(_WIN32)
103 if ( g.allowSymlinks ){
104 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode) || S_ISLNK(fileStat.st_mode);
 
 
105 }
 
 
106 #endif
107 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
108 }
109
110 /*
111 ** Return TRUE if the named file is an ordinary file. Return false
112 ** for directories, devices, fifos, symlinks, etc.
@@ -116,28 +113,10 @@
113 */
114 int file_isfile(const char *zFilename){
115 return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode);
116 }
117
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
118 /*
119 ** Create symlink to file on Unix, or plain-text file with
120 ** symlink target if "allow-symlinks" is off or we're on Windows.
121 **
122 ** Arguments: target file (symlink will point to it), link file
@@ -180,36 +159,52 @@
159 blob_reset(&content);
160 }
161 }
162
163 /*
164 ** Return file permissions (normal, executable, or symlink):
165 ** - PERM_EXE if file is executable;
166 ** - PERM_LNK on Unix if file is symlink and allow-symlinks option is on;
167 ** - PERM_REG for all other cases (regular file, directory, fifo, etc).
168 */
169 int file_perm(const char *zFilename){
170 if( getStat(zFilename) ) return PERM_REG;
171 #if defined(_WIN32)
172 # if defined(__DMC__) || defined(_MSC_VER)
173 # define S_IXUSR _S_IEXEC
174 # endif
175 if( S_ISREG(fileStat.st_mode) && ((S_IXUSR)&fileStat.st_mode)!=0 )
176 return PERM_EXE;
177 else
178 return PERM_REG;
179 #else
180 if( S_ISREG(fileStat.st_mode) &&
181 ((S_IXUSR|S_IXGRP|S_IXOTH)&fileStat.st_mode)!=0 )
 
 
 
 
 
 
 
 
 
182 return PERM_EXE;
183 else if( g.allowSymlinks && S_ISLNK(fileStat.st_mode) )
184 return PERM_LNK;
185 else
186 return PERM_REG;
187 #endif
188 }
189
190 /*
191 ** Return TRUE if the named file is an executable. Return false
192 ** for directories, devices, fifos, symlinks, etc.
193 */
194 int file_isexe(const char *zFilename){
195 return file_perm(zFilename)==PERM_EXE;
196 }
197
198 /*
199 ** Return TRUE if the named file is a symlink and symlinks are allowed.
200 ** Return false for all other cases.
201 **
202 ** On Windows, always return False.
203 */
204 int file_islink(const char *zFilename){
205 return file_perm(zFilename)==PERM_LNK;
206 }
207
208 /*
209 ** Return 1 if zFilename is a directory. Return 0 if zFilename
210 ** does not exist. Return 2 if zFilename exists but is something
211

Keyboard Shortcuts

Open search /
Next entry (timeline) j
Previous entry (timeline) k
Open focused entry Enter
Show this help ?
Toggle theme Top nav button