| 1 | /* ------------------------------------------------------------------------ */ |
| 2 | /* */ |
| 3 | /* Creates/Replaces files or directories. */ |
| 4 | /* */ |
| 5 | /* ------------------------------------------------------------------------ */ |
| 6 | |
| 7 | #include "os.h" |
| 8 | |
| 9 | #ifdef __EMX__ |
| 10 | #include <sys/types.h> |
| 11 | #include <sys/dirent.h> |
| 12 | #include <stdlib.h> |
| 13 | #endif |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> // printf() remove() |
| 16 | #include <string.h> // strncpy() |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/stat.h> // struct stat |
| 19 | #include <time.h> |
| 20 | #include "install.h" |
| 21 | |
| 22 | #if defined(DOS) || defined(WINNT) || defined(WIN16) |
| 23 | #include <io.h> // access() |
| 24 | #endif |
| 25 | #if defined(__CYGWIN__) |
| 26 | #include <Windows32/Base.h> |
| 27 | #endif |
| 28 | |
| 29 | #include "attribs.h" |
| 30 | #include "globals.h" |
| 31 | #include "uac_crt.h" |
| 32 | #include "uac_sys.h" |
| 33 | |
| 34 | #ifdef ENABLE_LOGGING |
| 35 | extern FILE *logfile; |
| 36 | #endif |
| 37 | extern char installdir[400]; |
| 38 | |
| 39 | /* Undocumented functions */ |
| 40 | #if defined(__OS2__) || defined(__EMX__) |
| 41 | APIRET APIENTRY DosReplaceModule(PSZ pszOldModule,PSZ pszNewModule,PSZ pszBackupModule); |
| 42 | #endif |
| 43 | |
| 44 | |
| 45 | /* gets file name from header |
| 46 | */ |
| 47 | CHAR *ace_fname(CHAR * s, thead * head, INT nopath) |
| 48 | { |
| 49 | INT i; |
| 50 | char *cp; |
| 51 | |
| 52 | strncpy(s, (CHAR*)(*(tfhead *) head).FNAME, i = (*(tfhead *) head).FNAME_SIZE); |
| 53 | s[i] = 0; |
| 54 | |
| 55 | if (nopath) |
| 56 | { |
| 57 | cp=strrchr(s, '\\'); |
| 58 | if (cp) |
| 59 | memmove(s, cp+1, strlen(cp)); |
| 60 | } |
| 61 | #ifdef __UNIX__ |
| 62 | else |
| 63 | { // by current OS seperator |
| 64 | cp=s; |
| 65 | while ((cp=strchr(cp, '\\'))!=NULL) |
| 66 | *cp++='/'; |
| 67 | } |
| 68 | #endif |
| 69 | |
| 70 | return s; |
| 71 | } |
| 72 | |
| 73 | void check_ext_dir(CHAR * f) // checks/creates path of file |
| 74 | { |
| 75 | char d[1024]; |
| 76 | char buffer[1024]; |
| 77 | int z, flag = 0, len = strlen(f); |
| 78 | |
| 79 | strcpy(buffer, f); |
| 80 | for(z=len;z>-1;z--) |
| 81 | { |
| 82 | if(buffer[z] == '\\') |
| 83 | { |
| 84 | buffer[z+1] = 0; |
| 85 | flag = 1; |
| 86 | z = -1; |
| 87 | } |
| 88 | } |
| 89 | if(!flag) |
| 90 | return; |
| 91 | for(z=0;z<strlen(buffer);z++) |
| 92 | { |
| 93 | if(buffer[z] == '\\') |
| 94 | { |
| 95 | if(!(z == 2 && buffer[1] == ':')) |
| 96 | { |
| 97 | strcpy(d, buffer); |
| 98 | d[z] = 0; |
| 99 | if (!fileexists(d)) |
| 100 | { |
| 101 | #if (defined(__OS2__) && !defined(__EMX__)) || (defined(WINNT) && !defined(__CYGWIN__)) |
| 102 | if (mkdir(d)) |
| 103 | #else |
| 104 | if (mkdir(d, 0)) |
| 105 | #endif |
| 106 | |
| 107 | { |
| 108 | #if 0 |
| 109 | f_err = ERR_WRITE; |
| 110 | error("Error while creating directory \"%s\".", d); |
| 111 | } |
| 112 | else |
| 113 | #else |
| 114 | } |
| 115 | #endif |
| 116 | { |
| 117 | #ifdef ENABLE_LOGGING |
| 118 | if(logfile) |
| 119 | { |
| 120 | if(strlen(d) > 1 && d[1] == ':') |
| 121 | fprintf(logfile, "<NewDir>,%s\r\n", d); |
| 122 | else |
| 123 | { |
| 124 | if(installdir[strlen(installdir)-1] == '\\') |
| 125 | fprintf(logfile, "<NewDir>,%s%s\r\n", installdir, d); |
| 126 | else |
| 127 | fprintf(logfile, "<NewDir>,%s\\%s\r\n", installdir, d); |
| 128 | } |
| 129 | } |
| 130 | #endif |
| 131 | } |
| 132 | } |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | INT ovr_delete(CHAR * n) // deletes directory or file |
| 140 | { |
| 141 | if (remove(n) && rmdir(n)) |
| 142 | { |
| 143 | #if defined(__OS2__) || defined(__EMX__) |
| 144 | DosReplaceModule(n, NULL, NULL); |
| 145 | #endif |
| 146 | if (remove(n) && rmdir(n)) |
| 147 | { |
| 148 | error("Could not delete file or directory: \"%s\" Access denied.", n); |
| 149 | return (1); |
| 150 | } |
| 151 | } |
| 152 | return (0); |
| 153 | } |
| 154 | |
| 155 | INT create_dest_file(CHAR * file, INT a) // creates file or directory |
| 156 | { |
| 157 | INT han, |
| 158 | i = 0, |
| 159 | ex = fileexists(file); |
| 160 | struct stat st; |
| 161 | extern int no_update; |
| 162 | |
| 163 | check_ext_dir(file); |
| 164 | if (f_err) |
| 165 | return (-1); |
| 166 | if (a & _A_SUBDIR) |
| 167 | { // create dir or file? |
| 168 | if (ex) |
| 169 | { |
| 170 | stat(file, &st); |
| 171 | } |
| 172 | |
| 173 | #if (defined(__OS2__) && !defined(__EMX__)) || (!defined(__CYGWIN__) && defined(WINNT)) |
| 174 | if ((!ex && mkdir(file)) || (ex && (st.st_mode & S_IFDIR))) |
| 175 | #else |
| 176 | if ((!ex && mkdir(file, 0)) || (ex && (st.st_mode & S_IFDIR))) |
| 177 | #endif |
| 178 | { |
| 179 | error("Could not create directory %s."); |
| 180 | return (-1); |
| 181 | } |
| 182 | else |
| 183 | { /* I wonder why it never gets here... :/ BS */ |
| 184 | #ifdef ENABLE_LOGGIN |
| 185 | if(logfile) |
| 186 | fprintf(logfile, "<NewDir>,%s\\%s\r\n", installdir, file); |
| 187 | #endif |
| 188 | } |
| 189 | |
| 190 | #ifdef DOS |
| 191 | _dos_setfileattr(file, a); // set directory attributes |
| 192 | #endif |
| 193 | return (-1); |
| 194 | } |
| 195 | else |
| 196 | { |
| 197 | if (ex) |
| 198 | { // does the file already exist |
| 199 | #if defined(__OS2_) || defined(__EMX__) |
| 200 | static int sddall = 0; |
| 201 | FILESTATUS3 fileinfo; |
| 202 | |
| 203 | f_ovrall = 1; |
| 204 | |
| 205 | DosQueryPathInfo(file, FIL_STANDARD, &fileinfo, sizeof(FILESTATUS3)); |
| 206 | if(!sddall) |
| 207 | { |
| 208 | FDATE fdate; |
| 209 | FTIME ftime; |
| 210 | struct tm tc, tc2; |
| 211 | time_t tt, tt2; |
| 212 | |
| 213 | *((USHORT*)&fdate) = (USHORT)(fhead.FTIME >> 16); |
| 214 | *((USHORT*)&ftime) = (USHORT)fhead.FTIME; |
| 215 | |
| 216 | tc.tm_year = fileinfo.fdateLastWrite.year + 80; |
| 217 | tc.tm_mon = fileinfo.fdateLastWrite.month - 1; |
| 218 | tc.tm_mday = fileinfo.fdateLastWrite.day; |
| 219 | tc.tm_hour = fileinfo.ftimeLastWrite.hours; |
| 220 | tc.tm_min = fileinfo.ftimeLastWrite.minutes; |
| 221 | tc.tm_sec = fileinfo.ftimeLastWrite.twosecs * 2; |
| 222 | |
| 223 | tc2.tm_year = fdate.year + 80; |
| 224 | tc2.tm_mon = fdate.month - 1; |
| 225 | tc2.tm_mday = fdate.day; |
| 226 | tc2.tm_hour = ftime.hours; |
| 227 | tc2.tm_min = ftime.minutes; |
| 228 | tc2.tm_sec = ftime.twosecs * 2; |
| 229 | |
| 230 | if((tt = mktime(&tc)) == -1 || (tt2 = mktime(&tc2)) == -1 || tt > tt2) |
| 231 | { |
| 232 | |
| 233 | if(file[1] == ':') |
| 234 | i = confirm("File \"%s\" has a newer modification time. Overwrite?", file); // prompt for overwrite |
| 235 | else |
| 236 | { |
| 237 | if(installdir[strlen(installdir)-1] == '\\') |
| 238 | i = confirm("File \"%s%s\" has a newer modification time. Overwrite?", installdir, file); // prompt for overwrite |
| 239 | else |
| 240 | i = confirm("File \"%s\\%s\" has a newer modification time. Overwrite?", installdir, file); // prompt for overwrite |
| 241 | } |
| 242 | if(i == 1) |
| 243 | { |
| 244 | sddall = 1; |
| 245 | i = 0; |
| 246 | } |
| 247 | if (i == 3) |
| 248 | f_err = ERR_USER; |
| 249 | if(i) |
| 250 | return -1; |
| 251 | } |
| 252 | } |
| 253 | fileinfo.attrFile = FILE_NORMAL; |
| 254 | DosSetPathInfo(file, FIL_STANDARD, (PVOID)&fileinfo, sizeof(FILESTATUS3), 0); |
| 255 | #endif |
| 256 | if (!f_ovrall) |
| 257 | { |
| 258 | if(installdir[strlen(installdir)-1] == '\\') |
| 259 | i = confirm("Overwrite file \"%s%s\"?", installdir, file); // prompt for overwrite |
| 260 | else |
| 261 | i = confirm("Overwrite file \"%s\\%s\"?", installdir, file); // prompt for overwrite |
| 262 | f_ovrall = (i == 1); |
| 263 | if (i == 3) |
| 264 | f_err = ERR_USER; |
| 265 | } |
| 266 | if ((i && !f_ovrall) || ovr_delete(file)) |
| 267 | return (-1); // delete? |
| 268 | } |
| 269 | #if defined(__OS2_) || defined(__EMX__) || defined(WIN32) |
| 270 | if ((han = open(file, O_WRONLY | O_TRUNC | O_CREAT | O_BINARY, |
| 271 | S_IREAD | S_IWRITE | S_IEXEC | S_IDELETE | |
| 272 | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH )) < 0) |
| 273 | #else |
| 274 | if ((han = open(file, O_WRONLY | O_TRUNC | O_CREAT, |
| 275 | S_IREAD | S_IWRITE | S_IEXEC | S_IDELETE | |
| 276 | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH )) < 0) |
| 277 | #endif |
| 278 | error("Could not create destination file \"%s\".", file); |
| 279 | else |
| 280 | { |
| 281 | #ifdef ENABLE_LOGGING |
| 282 | if(logfile) |
| 283 | { |
| 284 | if(!no_update) |
| 285 | { |
| 286 | if(strlen(file) > 1 && file[1] == ':') |
| 287 | fprintf(logfile, "<FileInst>,%s\r\n", file); |
| 288 | else |
| 289 | { |
| 290 | if(installdir[strlen(installdir)-1] == '\\') |
| 291 | fprintf(logfile, "<FileInst>,%s%s\r\n", installdir, file); |
| 292 | else |
| 293 | fprintf(logfile, "<FileInst>,%s\\%s\r\n", installdir, file); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | #endif |
| 298 | } |
| 299 | |
| 300 | return (han); |
| 301 | } |
| 302 | } |
| 303 | |