00001 #include "rsync.h"
00002
00003 extern mode_t orig_umask;
00004
00005 #define FLAG_X_KEEP (1<<0)
00006 #define FLAG_DIRS_ONLY (1<<1)
00007 #define FLAG_FILES_ONLY (1<<2)
00008
00009 struct chmod_mode_struct {
00010 struct chmod_mode_struct *next;
00011 int ModeAND, ModeOR;
00012 char flags;
00013 };
00014
00015 #define CHMOD_ADD 1
00016 #define CHMOD_SUB 2
00017 #define CHMOD_EQ 3
00018
00019 #define STATE_ERROR 0
00020 #define STATE_1ST_HALF 1
00021 #define STATE_2ND_HALF 2
00022
00023
00024
00025
00026 struct chmod_mode_struct *parse_chmod(const char *modestr,
00027 struct chmod_mode_struct **root_mode_ptr)
00028 {
00029 int state = STATE_1ST_HALF;
00030 int where = 0, what = 0, op = 0, topbits = 0, topoct = 0, flags = 0;
00031 struct chmod_mode_struct *first_mode = NULL, *curr_mode = NULL,
00032 *prev_mode = NULL;
00033
00034 while (state != STATE_ERROR) {
00035 if (!*modestr || *modestr == ',') {
00036 int bits;
00037
00038 if (!op) {
00039 state = STATE_ERROR;
00040 break;
00041 }
00042 prev_mode = curr_mode;
00043 curr_mode = new_array(struct chmod_mode_struct, 1);
00044 if (prev_mode)
00045 prev_mode->next = curr_mode;
00046 else
00047 first_mode = curr_mode;
00048 curr_mode->next = NULL;
00049
00050 if (where)
00051 bits = where * what;
00052 else {
00053 where = 0111;
00054 bits = (where * what) & ~orig_umask;
00055 }
00056
00057 switch (op) {
00058 case CHMOD_ADD:
00059 curr_mode->ModeAND = CHMOD_BITS;
00060 curr_mode->ModeOR = bits + topoct;
00061 break;
00062 case CHMOD_SUB:
00063 curr_mode->ModeAND = CHMOD_BITS - bits - topoct;
00064 curr_mode->ModeOR = 0;
00065 break;
00066 case CHMOD_EQ:
00067 curr_mode->ModeAND = CHMOD_BITS - (where * 7) - (topoct ? topbits : 0);
00068 curr_mode->ModeOR = bits + topoct;
00069 break;
00070 }
00071
00072 curr_mode->flags = flags;
00073
00074 if (!*modestr)
00075 break;
00076 modestr++;
00077
00078 state = STATE_1ST_HALF;
00079 where = what = op = topoct = topbits = flags = 0;
00080 }
00081
00082 if (state != STATE_2ND_HALF) {
00083 switch (*modestr) {
00084 case 'D':
00085 if (flags & FLAG_FILES_ONLY)
00086 state = STATE_ERROR;
00087 flags |= FLAG_DIRS_ONLY;
00088 break;
00089 case 'F':
00090 if (flags & FLAG_DIRS_ONLY)
00091 state = STATE_ERROR;
00092 flags |= FLAG_FILES_ONLY;
00093 break;
00094 case 'u':
00095 where |= 0100;
00096 topbits |= 04000;
00097 break;
00098 case 'g':
00099 where |= 0010;
00100 topbits |= 02000;
00101 break;
00102 case 'o':
00103 where |= 0001;
00104 break;
00105 case 'a':
00106 where |= 0111;
00107 break;
00108 case '+':
00109 op = CHMOD_ADD;
00110 state = STATE_2ND_HALF;
00111 break;
00112 case '-':
00113 op = CHMOD_SUB;
00114 state = STATE_2ND_HALF;
00115 break;
00116 case '=':
00117 op = CHMOD_EQ;
00118 state = STATE_2ND_HALF;
00119 break;
00120 default:
00121 state = STATE_ERROR;
00122 break;
00123 }
00124 } else {
00125 switch (*modestr) {
00126 case 'r':
00127 what |= 4;
00128 break;
00129 case 'w':
00130 what |= 2;
00131 break;
00132 case 'X':
00133 flags |= FLAG_X_KEEP;
00134
00135 case 'x':
00136 what |= 1;
00137 break;
00138 case 's':
00139 if (topbits)
00140 topoct |= topbits;
00141 else
00142 topoct = 04000;
00143 break;
00144 case 't':
00145 topoct |= 01000;
00146 break;
00147 default:
00148 state = STATE_ERROR;
00149 break;
00150 }
00151 }
00152 modestr++;
00153 }
00154
00155 if (state == STATE_ERROR) {
00156 free_chmod_mode(first_mode);
00157 return NULL;
00158 }
00159
00160 if (!(curr_mode = *root_mode_ptr))
00161 *root_mode_ptr = first_mode;
00162 else {
00163 while (curr_mode->next)
00164 curr_mode = curr_mode->next;
00165 curr_mode->next = first_mode;
00166 }
00167
00168 return first_mode;
00169 }
00170
00171
00172
00173
00174 int tweak_mode(int mode, struct chmod_mode_struct *chmod_modes)
00175 {
00176 int IsX = mode & 0111;
00177 int NonPerm = mode & ~CHMOD_BITS;
00178
00179 for ( ; chmod_modes; chmod_modes = chmod_modes->next) {
00180 if ((chmod_modes->flags & FLAG_DIRS_ONLY) && !S_ISDIR(NonPerm))
00181 continue;
00182 if ((chmod_modes->flags & FLAG_FILES_ONLY) && S_ISDIR(NonPerm))
00183 continue;
00184 mode &= chmod_modes->ModeAND;
00185 if ((chmod_modes->flags & FLAG_X_KEEP) && !IsX && !S_ISDIR(NonPerm))
00186 mode |= chmod_modes->ModeOR & ~0111;
00187 else
00188 mode |= chmod_modes->ModeOR;
00189 }
00190
00191 return mode | NonPerm;
00192 }
00193
00194
00195 int free_chmod_mode(struct chmod_mode_struct *chmod_modes)
00196 {
00197 struct chmod_mode_struct *next;
00198
00199 while (chmod_modes) {
00200 next = chmod_modes->next;
00201 free(chmod_modes);
00202 chmod_modes = next;
00203 }
00204 return 0;
00205 }