00001
00005 #include "system.h"
00006
00007
00008 const char * RPMVERSION = VERSION;
00009
00010 #include "rpmio_internal.h"
00011 #include <rpmurl.h>
00012 #include <rpmmacro.h>
00013 #include <rpmlib.h>
00014 #include "legacy.h"
00015 #include "misc.h"
00016 #include "debug.h"
00017
00018 rpmRC rpmMkdirPath (const char * dpath, const char * dname)
00019 {
00020 struct stat st;
00021 int rc;
00022
00023 if ((rc = Stat(dpath, &st)) < 0) {
00024 int ut = urlPath(dpath, NULL);
00025 switch (ut) {
00026 case URL_IS_PATH:
00027 case URL_IS_UNKNOWN:
00028 if (errno != ENOENT)
00029 break;
00030
00031 case URL_IS_HTTPS:
00032 case URL_IS_HTTP:
00033 case URL_IS_FTP:
00034 rc = Mkdir(dpath, 0755);
00035 break;
00036 case URL_IS_DASH:
00037 case URL_IS_HKP:
00038 break;
00039 }
00040 if (rc < 0) {
00041 rpmError(RPMERR_CREATE, _("cannot create %%%s %s\n"), dname, dpath);
00042 return RPMRC_FAIL;
00043 }
00044 }
00045 return RPMRC_OK;
00046 }
00047
00048
00049 char ** splitString(const char * str, int length, char sep)
00050 {
00051 const char * source;
00052 char * s, * dest;
00053 char ** list;
00054 int i;
00055 int fields;
00056
00057 s = xmalloc(length + 1);
00058
00059 fields = 1;
00060 for (source = str, dest = s, i = 0; i < length; i++, source++, dest++) {
00061 *dest = *source;
00062 if (*dest == sep) fields++;
00063 }
00064
00065 *dest = '\0';
00066
00067 list = xmalloc(sizeof(*list) * (fields + 1));
00068
00069 dest = s;
00070 list[0] = dest;
00071 i = 1;
00072 while (i < fields) {
00073 if (*dest == sep) {
00074 list[i++] = dest + 1;
00075 *dest = 0;
00076 }
00077 dest++;
00078 }
00079
00080 list[i] = NULL;
00081
00082
00083 return list;
00084
00085 }
00086
00087
00088 void freeSplitString(char ** list)
00089 {
00090
00091 list[0] = _free(list[0]);
00092
00093 list = _free(list);
00094 }
00095
00096 int doputenv(const char *str)
00097 {
00098 char * a;
00099
00100
00101 a = xmalloc(strlen(str) + 1);
00102 strcpy(a, str);
00103 return putenv(a);
00104 }
00105
00106 int dosetenv(const char * name, const char * value, int overwrite)
00107 {
00108 char * a;
00109
00110 if (!overwrite && getenv(name)) return 0;
00111
00112
00113 a = xmalloc(strlen(name) + strlen(value) + sizeof("="));
00114 (void) stpcpy( stpcpy( stpcpy( a, name), "="), value);
00115 return putenv(a);
00116 }
00117
00118 int makeTempFile(const char * prefix, const char ** fnptr, FD_t * fdptr)
00119 {
00120 const char * tpmacro = "%{?_tmppath:%{_tmppath}}%{!?_tmppath:/var/tmp}";
00121 const char * tempfn = NULL;
00122 const char * tfn = NULL;
00123 static int _initialized = 0;
00124 int temput;
00125 FD_t fd = NULL;
00126 int ran;
00127
00128
00129 if (!prefix) prefix = "";
00130
00131
00132
00133
00134 if (!_initialized) {
00135 _initialized = 1;
00136 tempfn = rpmGenPath(prefix, tpmacro, NULL);
00137 if (rpmioMkpath(tempfn, 0755, (uid_t) -1, (gid_t) -1))
00138 goto errxit;
00139 }
00140
00141
00142
00143 srand(time(NULL));
00144 ran = rand() % 100000;
00145
00146
00147
00148 do {
00149 char tfnbuf[64];
00150 #ifndef NOTYET
00151 sprintf(tfnbuf, "rpm-tmp.%d", ran++);
00152 tempfn = _free(tempfn);
00153 tempfn = rpmGenPath(prefix, tpmacro, tfnbuf);
00154 #else
00155 strcpy(tfnbuf, "rpm-tmp.XXXXXX");
00156 tempfn = _free(tempfn);
00157 tempfn = rpmGenPath(prefix, tpmacro, mktemp(tfnbuf));
00158 #endif
00159
00160 temput = urlPath(tempfn, &tfn);
00161 if (*tfn == '\0') goto errxit;
00162
00163 switch (temput) {
00164 case URL_IS_DASH:
00165 case URL_IS_HKP:
00166 goto errxit;
00167 break;
00168 case URL_IS_HTTPS:
00169 case URL_IS_HTTP:
00170 case URL_IS_FTP:
00171 default:
00172 break;
00173 }
00174
00175 fd = Fopen(tempfn, "w+x");
00176
00177 } while ((fd == NULL || Ferror(fd)) && errno == EEXIST);
00178
00179 if (fd == NULL || Ferror(fd)) {
00180 rpmError(RPMERR_SCRIPT, _("error creating temporary file %s\n"), tempfn);
00181 goto errxit;
00182 }
00183
00184 switch(temput) {
00185 case URL_IS_PATH:
00186 case URL_IS_UNKNOWN:
00187 { struct stat sb, sb2;
00188 if (!stat(tfn, &sb) && S_ISLNK(sb.st_mode)) {
00189 rpmError(RPMERR_SCRIPT, _("error creating temporary file %s\n"), tfn);
00190 goto errxit;
00191 }
00192
00193 if (sb.st_nlink != 1) {
00194 rpmError(RPMERR_SCRIPT, _("error creating temporary file %s\n"), tfn);
00195 goto errxit;
00196 }
00197
00198 if (fstat(Fileno(fd), &sb2) == 0) {
00199 if (sb2.st_ino != sb.st_ino || sb2.st_dev != sb.st_dev) {
00200 rpmError(RPMERR_SCRIPT, _("error creating temporary file %s\n"), tfn);
00201 goto errxit;
00202 }
00203 }
00204 } break;
00205 default:
00206 break;
00207 }
00208
00209
00210 if (fnptr)
00211 *fnptr = tempfn;
00212 else
00213 tempfn = _free(tempfn);
00214
00215 *fdptr = fd;
00216
00217 return 0;
00218
00219 errxit:
00220 tempfn = _free(tempfn);
00221 if (fnptr)
00222 *fnptr = NULL;
00223
00224 if (fd != NULL) (void) Fclose(fd);
00225
00226 return 1;
00227 }
00228
00229 char * currentDirectory(void)
00230 {
00231 int currDirLen = 0;
00232 char * currDir = NULL;
00233
00234 do {
00235 currDirLen += 128;
00236 currDir = xrealloc(currDir, currDirLen);
00237 memset(currDir, 0, currDirLen);
00238 } while (getcwd(currDir, currDirLen) == NULL && errno == ERANGE);
00239
00240 return currDir;
00241 }