2005-03-14 20:51:43 -05:00
|
|
|
/*
|
|
|
|
* util.c
|
|
|
|
*
|
2006-01-02 14:55:35 -05:00
|
|
|
* Copyright (c) 2002-2006 by Judd Vinet <jvinet@zeroflux.org>
|
2005-03-14 20:51:43 -05:00
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
|
|
|
|
* USA.
|
|
|
|
*/
|
|
|
|
|
2006-10-15 15:34:52 -04:00
|
|
|
#if defined(__APPLE__) || defined(__OpenBSD__)
|
|
|
|
#include <sys/syslimits.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#endif
|
|
|
|
|
2006-11-21 23:53:10 -05:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/ioctl.h>
|
2007-02-03 20:36:45 -05:00
|
|
|
#include <sys/time.h>
|
2006-11-21 23:53:10 -05:00
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
#include "config.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <dirent.h>
|
2005-03-16 16:21:06 -05:00
|
|
|
#include <unistd.h>
|
2006-05-14 22:19:57 -04:00
|
|
|
#include <libintl.h>
|
2005-10-06 03:28:30 -04:00
|
|
|
#ifdef CYGWIN
|
|
|
|
#include <limits.h> /* PATH_MAX */
|
|
|
|
#endif
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
#include <alpm.h>
|
|
|
|
#include <alpm_list.h>
|
2005-03-14 20:51:43 -05:00
|
|
|
/* pacman */
|
|
|
|
#include "util.h"
|
2005-12-20 18:58:27 -05:00
|
|
|
#include "conf.h"
|
2006-02-14 13:30:23 -05:00
|
|
|
#include "log.h"
|
2005-03-14 20:51:43 -05:00
|
|
|
|
2005-12-20 18:58:27 -05:00
|
|
|
extern config_t *config;
|
2005-10-18 03:40:04 -04:00
|
|
|
|
2006-11-20 04:10:23 -05:00
|
|
|
/* gets the current screen column width */
|
2007-01-18 11:52:57 -05:00
|
|
|
unsigned int getcols()
|
2006-11-20 04:10:23 -05:00
|
|
|
{
|
2006-12-28 12:20:41 -05:00
|
|
|
if(!isatty(1)) {
|
|
|
|
/* We will default to 80 columns if we're not a tty
|
|
|
|
* this seems a fairly standard file width.
|
|
|
|
*/
|
|
|
|
return 80;
|
2007-01-18 11:52:57 -05:00
|
|
|
} else {
|
2006-11-21 23:53:10 -05:00
|
|
|
#ifdef TIOCGSIZE
|
2007-01-18 11:52:57 -05:00
|
|
|
struct ttysize win;
|
|
|
|
if(ioctl(1, TIOCGSIZE, &win) == 0) {
|
|
|
|
return win.ts_cols;
|
|
|
|
}
|
2006-11-21 23:53:10 -05:00
|
|
|
#elif defined(TIOCGWINSZ)
|
2007-01-18 11:52:57 -05:00
|
|
|
struct winsize win;
|
|
|
|
if(ioctl(1, TIOCGWINSZ, &win) == 0) {
|
|
|
|
return win.ws_col;
|
|
|
|
}
|
2006-11-21 23:53:10 -05:00
|
|
|
#endif
|
2007-01-18 11:52:57 -05:00
|
|
|
/* If we can't figure anything out, we'll just assume 80 columns */
|
|
|
|
/* TODO any problems caused by this assumption? */
|
|
|
|
return 80;
|
2006-11-21 23:53:10 -05:00
|
|
|
}
|
|
|
|
/* Original envvar way - prone to display issues
|
2006-11-20 04:10:23 -05:00
|
|
|
const char *cenv = getenv("COLUMNS");
|
|
|
|
if(cenv != NULL) {
|
|
|
|
return atoi(cenv);
|
|
|
|
}
|
|
|
|
return -1;
|
2006-11-21 23:53:10 -05:00
|
|
|
*/
|
2006-11-20 04:10:23 -05:00
|
|
|
}
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* does the same thing as 'mkdir -p' */
|
|
|
|
int makepath(char *path)
|
|
|
|
{
|
|
|
|
char *orig, *str, *ptr;
|
|
|
|
char full[PATH_MAX] = "";
|
|
|
|
mode_t oldmask;
|
|
|
|
|
|
|
|
oldmask = umask(0000);
|
|
|
|
|
|
|
|
orig = strdup(path);
|
|
|
|
str = orig;
|
|
|
|
while((ptr = strsep(&str, "/"))) {
|
|
|
|
if(strlen(ptr)) {
|
|
|
|
struct stat buf;
|
|
|
|
|
|
|
|
strcat(full, "/");
|
|
|
|
strcat(full, ptr);
|
|
|
|
if(stat(full, &buf)) {
|
2005-03-16 16:52:49 -05:00
|
|
|
if(mkdir(full, 0755)) {
|
2005-03-14 20:51:43 -05:00
|
|
|
free(orig);
|
|
|
|
umask(oldmask);
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(orig);
|
|
|
|
umask(oldmask);
|
|
|
|
return(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* does the same thing as 'rm -rf' */
|
|
|
|
int rmrf(char *path)
|
|
|
|
{
|
|
|
|
int errflag = 0;
|
|
|
|
struct dirent *dp;
|
|
|
|
DIR *dirp;
|
|
|
|
|
|
|
|
if(!unlink(path)) {
|
|
|
|
return(0);
|
|
|
|
} else {
|
|
|
|
if(errno == ENOENT) {
|
|
|
|
return(0);
|
|
|
|
} else if(errno == EPERM) {
|
|
|
|
/* fallthrough */
|
|
|
|
} else if(errno == EISDIR) {
|
|
|
|
/* fallthrough */
|
|
|
|
} else if(errno == ENOTDIR) {
|
|
|
|
return(1);
|
|
|
|
} else {
|
|
|
|
/* not a directory */
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
if((dirp = opendir(path)) == (DIR *)-1) {
|
|
|
|
return(1);
|
|
|
|
}
|
|
|
|
for(dp = readdir(dirp); dp != NULL; dp = readdir(dirp)) {
|
|
|
|
if(dp->d_ino) {
|
2006-01-07 04:42:48 -05:00
|
|
|
char name[PATH_MAX];
|
2005-03-14 20:51:43 -05:00
|
|
|
sprintf(name, "%s/%s", path, dp->d_name);
|
|
|
|
if(strcmp(dp->d_name, "..") && strcmp(dp->d_name, ".")) {
|
|
|
|
errflag += rmrf(name);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
closedir(dirp);
|
|
|
|
if(rmdir(path)) {
|
|
|
|
errflag++;
|
|
|
|
}
|
|
|
|
return(errflag);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* output a string, but wrap words properly with a specified indentation
|
|
|
|
*/
|
2007-01-18 11:52:57 -05:00
|
|
|
void indentprint(const char *str, unsigned int indent)
|
2005-03-14 20:51:43 -05:00
|
|
|
{
|
2006-11-20 04:10:23 -05:00
|
|
|
const char *p = str;
|
2007-01-18 11:52:57 -05:00
|
|
|
unsigned int cidx = indent;
|
2005-03-14 20:51:43 -05:00
|
|
|
|
|
|
|
while(*p) {
|
|
|
|
if(*p == ' ') {
|
2006-11-20 04:10:23 -05:00
|
|
|
const char *next = NULL;
|
2007-01-25 21:13:16 -05:00
|
|
|
unsigned int len;
|
2005-03-14 20:51:43 -05:00
|
|
|
p++;
|
|
|
|
if(p == NULL || *p == ' ') continue;
|
|
|
|
next = strchr(p, ' ');
|
|
|
|
if(next == NULL) {
|
|
|
|
next = p + strlen(p);
|
|
|
|
}
|
|
|
|
len = next - p;
|
2006-11-21 23:53:10 -05:00
|
|
|
if(len > (getcols()-cidx-1)) {
|
2005-03-14 20:51:43 -05:00
|
|
|
/* newline */
|
2007-01-18 11:52:57 -05:00
|
|
|
unsigned int i;
|
2005-03-14 20:51:43 -05:00
|
|
|
fprintf(stdout, "\n");
|
|
|
|
for(i = 0; i < indent; i++) {
|
|
|
|
fprintf(stdout, " ");
|
|
|
|
}
|
|
|
|
cidx = indent;
|
|
|
|
} else {
|
|
|
|
printf(" ");
|
|
|
|
cidx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
fprintf(stdout, "%c", *p);
|
|
|
|
p++;
|
|
|
|
cidx++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Convert a string to uppercase
|
|
|
|
*/
|
|
|
|
char *strtoupper(char *str)
|
|
|
|
{
|
|
|
|
char *ptr = str;
|
|
|
|
|
|
|
|
while(*ptr) {
|
|
|
|
(*ptr) = toupper(*ptr);
|
|
|
|
ptr++;
|
|
|
|
}
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Trim whitespace and newlines from a string
|
|
|
|
*/
|
|
|
|
char *strtrim(char *str)
|
|
|
|
{
|
|
|
|
char *pch = str;
|
|
|
|
while(isspace(*pch)) {
|
|
|
|
pch++;
|
|
|
|
}
|
|
|
|
if(pch != str) {
|
|
|
|
memmove(str, pch, (strlen(pch) + 1));
|
|
|
|
}
|
|
|
|
|
|
|
|
pch = (char *)(str + (strlen(str) - 1));
|
|
|
|
while(isspace(*pch)) {
|
|
|
|
pch--;
|
|
|
|
}
|
|
|
|
*++pch = '\0';
|
|
|
|
|
|
|
|
return str;
|
|
|
|
}
|
|
|
|
|
2007-01-19 04:28:44 -05:00
|
|
|
void list_display(const char *title, alpm_list_t *list)
|
|
|
|
{
|
|
|
|
alpm_list_t *i;
|
|
|
|
int cols, len;
|
|
|
|
|
|
|
|
len = strlen(title);
|
|
|
|
printf("%s ", title);
|
|
|
|
|
|
|
|
if(list) {
|
|
|
|
for(i = list, cols = len; i; i = alpm_list_next(i)) {
|
|
|
|
char *str = alpm_list_getdata(i);
|
2007-02-01 01:35:30 -05:00
|
|
|
int s = strlen(str) + 2;
|
2007-01-19 04:28:44 -05:00
|
|
|
unsigned int maxcols = getcols();
|
|
|
|
if(s + cols >= maxcols) {
|
|
|
|
int i;
|
|
|
|
cols = len;
|
|
|
|
printf("\n");
|
2007-02-01 01:35:30 -05:00
|
|
|
for (i = 0; i <= len; ++i) {
|
2007-01-19 04:28:44 -05:00
|
|
|
printf(" ");
|
|
|
|
}
|
|
|
|
}
|
2007-02-01 01:35:30 -05:00
|
|
|
printf("%s ", str);
|
2007-01-19 04:28:44 -05:00
|
|
|
cols += s;
|
|
|
|
}
|
|
|
|
printf("\n");
|
|
|
|
} else {
|
|
|
|
printf(_("None\n"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-03 20:36:45 -05:00
|
|
|
/* Display a list of transaction targets.
|
|
|
|
* `pkgs` should be a list of pmsyncpkg_t's,
|
|
|
|
* retrieved from a transaction object
|
|
|
|
*/
|
2007-02-04 03:26:52 -05:00
|
|
|
void display_targets(alpm_list_t *syncpkgs)
|
2007-02-03 20:36:45 -05:00
|
|
|
{
|
|
|
|
char *str;
|
|
|
|
alpm_list_t *i, *j;
|
|
|
|
alpm_list_t *targets = NULL, *to_remove = NULL;
|
2007-02-08 15:44:47 -05:00
|
|
|
/* TODO these are some messy variable names */
|
|
|
|
unsigned long size = 0, isize = 0, rsize = 0;
|
|
|
|
double mbsize = 0.0, mbisize = 0.0, mbrsize = 0.0;
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2007-02-04 03:26:52 -05:00
|
|
|
for(i = syncpkgs; i; i = alpm_list_next(i)) {
|
2007-02-03 20:36:45 -05:00
|
|
|
pmsyncpkg_t *sync = alpm_list_getdata(i);
|
2007-02-26 03:43:02 -05:00
|
|
|
pmpkg_t *pkg = alpm_sync_get_pkg(sync);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
|
|
|
/* If this sync record is a replacement, the data member contains
|
|
|
|
* a list of packages to be removed due to the package that is being
|
|
|
|
* installed. */
|
|
|
|
if(alpm_sync_get_type(sync) == PM_SYNC_TYPE_REPLACE) {
|
|
|
|
alpm_list_t *to_replace = alpm_sync_get_data(sync);
|
|
|
|
|
|
|
|
for(j = to_replace; j; j = alpm_list_next(j)) {
|
|
|
|
pmpkg_t *rp = alpm_list_getdata(j);
|
|
|
|
const char *name = alpm_pkg_get_name(rp);
|
|
|
|
|
|
|
|
if(!alpm_list_find_str(to_remove, name)) {
|
2007-02-08 15:44:47 -05:00
|
|
|
rsize += alpm_pkg_get_isize(rp);
|
2007-02-03 20:36:45 -05:00
|
|
|
to_remove = alpm_list_add(to_remove, strdup(name));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
size += alpm_pkg_get_size(pkg);
|
|
|
|
isize += alpm_pkg_get_isize(pkg);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
|
|
|
asprintf(&str, "%s-%s", alpm_pkg_get_name(pkg), alpm_pkg_get_version(pkg));
|
|
|
|
targets = alpm_list_add(targets, str);
|
|
|
|
}
|
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
/* Convert byte sizes to MB */
|
|
|
|
mbsize = (double)(size) / (1024.0 * 1024.0);
|
|
|
|
mbisize = (double)(isize) / (1024.0 * 1024.0);
|
|
|
|
mbrsize = (double)(rsize) / (1024.0 * 1024.0);
|
|
|
|
|
2007-02-03 20:36:45 -05:00
|
|
|
if(to_remove) {
|
|
|
|
MSG(NL, "\n"); /* TODO ugly hack. printing a single NL should be easy */
|
|
|
|
list_display(_("Remove:"), to_remove);
|
|
|
|
FREELIST(to_remove);
|
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
if(mbrsize > 0) {
|
|
|
|
/* round up if size is really small */
|
|
|
|
if(mbrsize < 0.1) {
|
|
|
|
mbrsize = 0.1;
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
2007-02-08 15:44:47 -05:00
|
|
|
MSG(NL, _("\nTotal Removed Size: %.2f MB\n"), mbrsize);
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MSG(NL, "\n"); /* TODO ugly hack. printing a single NL should be easy */
|
|
|
|
list_display(_("Targets:"), targets);
|
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
/* round up if size is really small */
|
|
|
|
if(mbsize < 0.1) {
|
|
|
|
mbsize = 0.1;
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
2007-02-08 15:44:47 -05:00
|
|
|
MSG(NL, _("\nTotal Package Size: %.2f MB\n"), mbsize);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
if(mbisize > mbsize) {
|
|
|
|
/*round up if size is really small */
|
|
|
|
if(mbisize < 0.1) {
|
|
|
|
mbisize = 0.1;
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
2007-02-08 15:44:47 -05:00
|
|
|
MSG(NL, _("Total Installed Size: %.2f MB\n"), mbisize);
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
FREELIST(targets);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Silly little helper function, determines if the caller needs a visual update
|
|
|
|
* since the last time this function was called.
|
|
|
|
* This is made for the two progress bar functions, to prevent flicker
|
|
|
|
*
|
|
|
|
* first_call indicates if this is the first time it is called, for
|
|
|
|
* initialization purposes */
|
|
|
|
float get_update_timediff(int first_call)
|
|
|
|
{
|
|
|
|
float retval = 0.0;
|
|
|
|
static struct timeval last_time = {0};
|
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
/* on first call, simply set the last time and return */
|
2007-02-03 20:36:45 -05:00
|
|
|
if(first_call) {
|
2007-02-08 15:44:47 -05:00
|
|
|
gettimeofday(&last_time, NULL);
|
2007-02-03 20:36:45 -05:00
|
|
|
} else {
|
2007-02-08 15:44:47 -05:00
|
|
|
struct timeval this_time;
|
|
|
|
float diff_sec, diff_usec;
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
gettimeofday(&this_time, NULL);
|
|
|
|
diff_sec = this_time.tv_sec - last_time.tv_sec;
|
|
|
|
diff_usec = this_time.tv_usec - last_time.tv_usec;
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
retval = diff_sec + (diff_usec / 1000000.0);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
2007-02-08 15:44:47 -05:00
|
|
|
/* return 0 and do not update last_time if interval was too short */
|
2007-02-03 20:36:45 -05:00
|
|
|
if(retval < UPDATE_SPEED_SEC) {
|
2007-02-08 15:44:47 -05:00
|
|
|
retval = 0.0;
|
|
|
|
} else {
|
|
|
|
last_time = this_time;
|
|
|
|
/* printf("\nupdate retval: %f\n", retval); DEBUG*/
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
}
|
2007-02-08 15:44:47 -05:00
|
|
|
|
2007-02-03 20:36:45 -05:00
|
|
|
return(retval);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* refactored from cb_trans_progress */
|
|
|
|
void fill_progress(const int percent, const int proglen)
|
|
|
|
{
|
|
|
|
const unsigned short chomp = alpm_option_get_chomp();
|
|
|
|
const unsigned int hashlen = proglen - 8;
|
|
|
|
const unsigned int hash = percent * hashlen / 100;
|
2007-02-26 03:43:02 -05:00
|
|
|
static unsigned int lasthash = 0, mouth = 0;
|
2007-02-03 20:36:45 -05:00
|
|
|
unsigned int i;
|
|
|
|
|
2007-02-26 03:43:02 -05:00
|
|
|
if(percent == 0) {
|
|
|
|
lasthash = 0;
|
|
|
|
mouth = 0;
|
|
|
|
}
|
|
|
|
|
2007-02-03 20:36:45 -05:00
|
|
|
printf(" [");
|
|
|
|
for(i = hashlen; i > 1; --i) {
|
|
|
|
/* if special progress bar enabled */
|
|
|
|
if(chomp) {
|
|
|
|
if(i > hashlen - hash) {
|
|
|
|
printf("-");
|
|
|
|
} else if(i == hashlen - hash) {
|
|
|
|
if(lasthash == hash) {
|
|
|
|
if(mouth) {
|
|
|
|
printf("\033[1;33mC\033[m");
|
|
|
|
} else {
|
|
|
|
printf("\033[1;33mc\033[m");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
lasthash = hash;
|
|
|
|
mouth = mouth == 1 ? 0 : 1;
|
|
|
|
if(mouth) {
|
|
|
|
printf("\033[1;33mC\033[m");
|
|
|
|
} else {
|
|
|
|
printf("\033[1;33mc\033[m");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if(i%3 == 0) {
|
|
|
|
printf("\033[0;37mo\033[m");
|
|
|
|
} else {
|
|
|
|
printf("\033[0;37m \033[m");
|
|
|
|
}
|
|
|
|
} /* else regular progress bar */
|
|
|
|
else if(i > hashlen - hash) {
|
|
|
|
printf("#");
|
|
|
|
} else {
|
|
|
|
printf("-");
|
|
|
|
}
|
|
|
|
}
|
2007-02-10 04:34:59 -05:00
|
|
|
printf("] %3d%%", percent);
|
2007-02-03 20:36:45 -05:00
|
|
|
|
|
|
|
if(percent == 100) {
|
|
|
|
printf("\n");
|
2007-02-10 04:34:59 -05:00
|
|
|
} else {
|
|
|
|
printf("\r");
|
2007-02-03 20:36:45 -05:00
|
|
|
}
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-03-14 20:51:43 -05:00
|
|
|
/* vim: set ts=2 sw=2 noet: */
|