Fossil SCM
Refactor file_perm(), file_islink(), and file_isexe(). Now file_perm() calls stat only once.
Commit
eac23495a950cdd232e10b507de516bec89fdd12
Parent
82a95b4692e96b7…
1 file changed
+35
-40
+35
-40
| --- src/file.c | ||
| +++ src/file.c | ||
| @@ -100,16 +100,13 @@ | ||
| 100 | 100 | */ |
| 101 | 101 | int file_isfile_or_link(const char *zFilename){ |
| 102 | 102 | #if !defined(_WIN32) |
| 103 | 103 | if ( g.allowSymlinks ){ |
| 104 | 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 | 105 | } |
| 108 | -#else | |
| 109 | - return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode); | |
| 110 | 106 | #endif |
| 107 | + return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode); | |
| 111 | 108 | } |
| 112 | 109 | |
| 113 | 110 | /* |
| 114 | 111 | ** Return TRUE if the named file is an ordinary file. Return false |
| 115 | 112 | ** for directories, devices, fifos, symlinks, etc. |
| @@ -116,28 +113,10 @@ | ||
| 116 | 113 | */ |
| 117 | 114 | int file_isfile(const char *zFilename){ |
| 118 | 115 | return getStat(zFilename) ? 0 : S_ISREG(fileStat.st_mode); |
| 119 | 116 | } |
| 120 | 117 | |
| 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 | 118 | /* |
| 140 | 119 | ** Create symlink to file on Unix, or plain-text file with |
| 141 | 120 | ** symlink target if "allow-symlinks" is off or we're on Windows. |
| 142 | 121 | ** |
| 143 | 122 | ** Arguments: target file (symlink will point to it), link file |
| @@ -180,36 +159,52 @@ | ||
| 180 | 159 | blob_reset(&content); |
| 181 | 160 | } |
| 182 | 161 | } |
| 183 | 162 | |
| 184 | 163 | /* |
| 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). | |
| 187 | 168 | */ |
| 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; | |
| 190 | 171 | #if defined(_WIN32) |
| 191 | 172 | # if defined(__DMC__) || defined(_MSC_VER) |
| 192 | 173 | # define S_IXUSR _S_IEXEC |
| 193 | 174 | # 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; | |
| 195 | 179 | #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 ) | |
| 207 | 182 | return PERM_EXE; |
| 208 | - if( file_islink(zFilename) ) | |
| 183 | + else if( g.allowSymlinks && S_ISLNK(fileStat.st_mode) ) | |
| 209 | 184 | 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; | |
| 211 | 206 | } |
| 212 | 207 | |
| 213 | 208 | /* |
| 214 | 209 | ** Return 1 if zFilename is a directory. Return 0 if zFilename |
| 215 | 210 | ** does not exist. Return 2 if zFilename exists but is something |
| 216 | 211 |
| --- 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 |