Compare commits
37 Commits
bbec26f438
...
c_rewrite
Author | SHA1 | Date | |
---|---|---|---|
8024e86962
|
|||
88742c2ea5
|
|||
d101e186c4
|
|||
2d881a0d34
|
|||
cb735a01a1
|
|||
e3c9924fbc
|
|||
3645f7f30c
|
|||
66ca74441d
|
|||
7bca34cc89
|
|||
e4ab48fb22
|
|||
67c7f48cd7
|
|||
e2d4253050
|
|||
81b07991aa
|
|||
452b0e92e2
|
|||
f89e7112a4
|
|||
b28009f83b
|
|||
0cf266dd87
|
|||
6fce70ba3c
|
|||
b13189e7de
|
|||
92777bbbf8
|
|||
91eb6be7c6
|
|||
c28fbf309e
|
|||
fe9d5936e7
|
|||
30462c50c8
|
|||
6f9ee34a94
|
|||
3198a02081
|
|||
6593f087c4
|
|||
8b5704f397
|
|||
3c2ecc72ae
|
|||
e86a004a28
|
|||
a8eee49762
|
|||
e4e6f51bdb
|
|||
0db312e8a0
|
|||
2fff3ce2a2
|
|||
b7a2dc89be
|
|||
afab665ca6
|
|||
ea112d4ceb
|
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@ -0,0 +1,2 @@
|
||||
# Build artifacts
|
||||
update
|
52
README.md
52
README.md
@ -1,49 +1,33 @@
|
||||
# Update
|
||||
|
||||
Update script written in Bash for Arch Linux only. Keeps all your pacman and aur packages as well as your flatpaks up to date with one simple script. It also backups a list of all your pacman and aur packages and flatpaks.
|
||||
|
||||
## Requirements
|
||||
|
||||
Here is what is required.
|
||||
```bash
|
||||
pacman -S just
|
||||
```
|
||||
It also technically requires sudo, but if you use something else, then just create a softlink for this script to work.<br/>
|
||||
Doing that would look like this.
|
||||
```bash
|
||||
ln -s /usr/bin/your_program /usr/bin/sudo
|
||||
```
|
||||
Alpm hooks for the pacman package manager.<br>
|
||||
This project consists of two hooks, one for pre-transaction and the other for
|
||||
post-transaction.
|
||||
|
||||
## Installation
|
||||
|
||||
```bash
|
||||
git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git && cd update
|
||||
git clone https://gitea.hopeless-cloud.xyz/AustrianToast/update.git
|
||||
cd update
|
||||
```
|
||||
|
||||
Before installing, please edit the config and configure it to your liking.<br/>
|
||||
Before installing, please edit the config and configure it to your liking.
|
||||
|
||||
Then install using
|
||||
```bash
|
||||
just install
|
||||
make install
|
||||
```
|
||||
|
||||
## Usage
|
||||
If your require the previous functionality of being able to update flatpak and
|
||||
having the ability to preview the updateable packages and flatpaks.<br>
|
||||
This functionality has been re-packaged into `update.zsh`.<br>
|
||||
If you use oh-my-zsh, then you just throw this file into your `$ZSH_CUSTOM`.<br>
|
||||
This path is by default `~/.oh-my-zsh/custom`. If you use only plain zsh or any other
|
||||
shell, then you can just take the contained functions and put them inside the
|
||||
according shell config file. For example your `.bashrc`.
|
||||
|
||||
```
|
||||
Usage: update [OPTION]
|
||||
|
||||
options:
|
||||
no flag same as -p
|
||||
-f updates using flatpak update only
|
||||
-p updates using a pacman-wrapper only
|
||||
-a updates using flatpak update and a pacman-wrapper
|
||||
-g shutdowns the computer afterwards
|
||||
-r reboots the computer afterwards
|
||||
--help displays this message
|
||||
--preview shows a preview of which pkg's will be updates
|
||||
--version prints out the version number
|
||||
--backup just does the before-backup without updating
|
||||
```
|
||||
|
||||
## Contributing
|
||||
The function `flatpak-update` contains a variable called `BACKUP_LOCATION`.<br>
|
||||
Please change this path to your desired backup location.
|
||||
|
||||
## Contributing
|
||||
Contributions are always welcome!
|
||||
|
10
makefile
10
makefile
@ -1,10 +0,0 @@
|
||||
install:
|
||||
@echo "==> Installing update into /usr/local/bin"
|
||||
@sudo install -Dm755 update /usr/local/bin/update
|
||||
@[[ -f ${HOME}/.config/update.conf ]] || cp update.conf ${HOME}/.config/
|
||||
@echo "==> Finished."
|
||||
|
||||
uninstall:
|
||||
@echo "==> Uninstalling update from /usr/local/bin"
|
||||
@sudo rm /usr/local/bin/update ${HOME}/.config/update.conf
|
||||
@echo "==> Finished."
|
139
update
139
update
@ -1,139 +0,0 @@
|
||||
#!/usr/bin/bash
|
||||
|
||||
source "$HOME"/.config/update.conf || exit 1
|
||||
[[ ! -d $BACKUP_LOCATION ]] && mkdir --parents "$BACKUP_LOCATION"
|
||||
|
||||
VERSION="2.1.0"
|
||||
IGREEN="\033[0;92m" # Intense Green
|
||||
IYELLOW="\033[0;93m" # Intense Yellow
|
||||
NO_COLOR="\033[0m" # Text Reset
|
||||
TMP="/tmp/update"
|
||||
DATE="$(date +"%Y-%m-%dT%H:%M:%S%:z")" # RFC 3339 date-time https://datatracker.ietf.org/doc/html/rfc3339#section-5.6
|
||||
FINAL_COMMAND="true"
|
||||
|
||||
trap interrupt_function INT
|
||||
|
||||
interrupt_function() {
|
||||
echo "Interrupt has been detected"
|
||||
[[ -f /var/lib/pacman/db.lck ]] && sudo rm --force /var/lib/pacman/db.lck > /dev/null 2>&1
|
||||
rm --recursive --force "$TMP" > /dev/null 2>&1
|
||||
rm --force "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new
|
||||
exit 1
|
||||
}
|
||||
|
||||
help() {
|
||||
echo "Usage: update [OPTION]"
|
||||
echo
|
||||
echo "options:"
|
||||
echo "no option same as -p"
|
||||
echo "-f updates using flatpak update only"
|
||||
echo "-p updates using a pacman-wrapper only"
|
||||
echo "-a updates using flatpak update and a pacman-wrapper"
|
||||
echo "-g shutdowns the computer afterwards"
|
||||
echo "-r reboots the computer afterwards"
|
||||
echo "--help displays this message"
|
||||
echo "--preview shows a preview of pkg's and flatpaks which can be updated"
|
||||
echo "--version prints out the version number"
|
||||
echo "--backup just does the before-backup without updating"
|
||||
}
|
||||
|
||||
lock_pacman_db() {
|
||||
if [ -f /var/lib/pacman/db.lck ]; then
|
||||
echo -e "${IYELLOW}->${NO_COLOR} /var/lib/pacman/db.lck exists"
|
||||
echo -e "${IYELLOW}->${NO_COLOR} there might be another instance of pacman running. exiting..."
|
||||
exit 1
|
||||
fi
|
||||
sudo touch /var/lib/pacman/db.lck
|
||||
}
|
||||
|
||||
delete_oldest_backup() {
|
||||
if [[ $(find $BACKUP_LOCATION -name '*$1*' -exec printf %c {} + | wc -c) -ge $BACKUP_AMOUNT ]]; then
|
||||
rm --force $(find "$BACKUP_LOCATION" -name '*$1*' | sort -rn | head -1)
|
||||
fi
|
||||
}
|
||||
|
||||
before_backup() {
|
||||
lock_pacman_db
|
||||
[[ -d $TMP ]] && rm --recursive --force "$TMP"
|
||||
mkdir --parents "$TMP"/before-backup_"$DATE" "$TMP"/after-backup_"$DATE"
|
||||
|
||||
pacman --verbose --query > "$TMP"/before-backup_"$DATE"/pacman-before.txt
|
||||
[[ -f /usr/bin/flatpak ]] && flatpak list --all --show-details > "$TMP"/before-backup_"$DATE"/flatpak-before.txt
|
||||
|
||||
tar --create --zstd --file "$TMP"/before-backup_"$DATE".tar.zst.new "$TMP"/before-backup_"$DATE" /var/lib/pacman/local > /dev/null 2>&1 # for some reason it needs the output suppresion
|
||||
mv "$TMP"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"
|
||||
|
||||
sudo rm --force /var/lib/pacman/db.lck
|
||||
}
|
||||
|
||||
after_backup() {
|
||||
pacman --verbose --query > "$TMP"/after-backup_"$DATE"/pacman-after.txt
|
||||
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
||||
tar --create --zstd --file "$TMP"/after-backup_"$DATE".tar.zst.new "$TMP"/after-backup_"$DATE" > /dev/null 2>&1
|
||||
|
||||
mv "$TMP"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"
|
||||
delete_oldest_backup after-backup
|
||||
mv "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/after-backup_"$DATE".tar.zst
|
||||
|
||||
delete_oldest_backup before-backup
|
||||
mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst
|
||||
|
||||
rm --recursive --force "$TMP"
|
||||
}
|
||||
|
||||
update() {
|
||||
"$PACMAN_WRAPPER"
|
||||
if [[ -x /usr/bin/flatpak ]]; then
|
||||
flatpak update --assumeyes
|
||||
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
||||
fi
|
||||
}
|
||||
|
||||
if [[ ${1:0:2} == \-\- ]]; then
|
||||
case "${1:2}" in
|
||||
help)
|
||||
help ;;
|
||||
preview)
|
||||
"$PACMAN_WRAPPER" -Syy
|
||||
"$PACMAN_WRAPPER" --query --upgrades
|
||||
[[ -x /usr/bin/flatpak ]] && flatpak remote-ls --updates ;;
|
||||
version)
|
||||
echo "$VERSION" ;;
|
||||
backup)
|
||||
before_backup
|
||||
delete_oldest_backup before-backup
|
||||
mv "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst.new "$BACKUP_LOCATION"/before-backup_"$DATE".tar.zst
|
||||
rm --recursive --force "$TMP" ;;
|
||||
?)
|
||||
help; exit 1;;
|
||||
esac
|
||||
exit 0
|
||||
fi
|
||||
|
||||
before_backup && echo -e "${IGREEN}pre-backup complete${NO_COLOR}"
|
||||
|
||||
[[ -z $1 ]] && update
|
||||
while getopts 'fpagr' OPTIONS; do
|
||||
case $OPTIONS in
|
||||
f)
|
||||
if [[ -x /usr/bin/flatpak ]]; then
|
||||
flatpak update --assumeyes
|
||||
flatpak list --all --show-details > "$TMP"/after-backup_"$DATE"/flatpak-after.txt
|
||||
fi ;;
|
||||
p)
|
||||
"$PACMAN_WRAPPER" ;;
|
||||
a)
|
||||
update ;;
|
||||
g)
|
||||
[[ $1 == \-\g ]] && update
|
||||
FINAL_COMMAND="systemctl --check-inhibitors=yes poweroff" ;;
|
||||
r)
|
||||
[[ $1 == \-\r ]] && update
|
||||
FINAL_COMMAND="systemctl --check-inhibitors=yes reboot" ;;
|
||||
?)
|
||||
help; exit 1;;
|
||||
esac
|
||||
done
|
||||
|
||||
after_backup && echo -e "${IGREEN}after-backup complete${NO_COLOR}"
|
||||
$FINAL_COMMAND && exit 0
|
83
update.c
Normal file
83
update.c
Normal file
@ -0,0 +1,83 @@
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <errno.h>
|
||||
|
||||
int command_builder(char *command, char **args) {
|
||||
char *tmp_command = malloc(strlen(command));
|
||||
memcpy(tmp_command, command, strlen(command));
|
||||
char *tmp_token;
|
||||
size_t index = 0;
|
||||
|
||||
if (tmp_command == NULL) { return 1; }
|
||||
|
||||
tmp_token = strtok(tmp_command, " ");
|
||||
while(tmp_token != NULL) {
|
||||
args[index] = malloc(strlen(tmp_token));
|
||||
memcpy(args[index], tmp_token, strlen(tmp_token));
|
||||
tmp_token = strtok(NULL, " ");
|
||||
index++;
|
||||
}
|
||||
free(tmp_command);
|
||||
args[index++] = NULL;
|
||||
return 0;
|
||||
}
|
||||
|
||||
int run_cmd_and_wait(char *command) {
|
||||
char **args = malloc(strlen(command));
|
||||
int ret = command_builder(command, args);
|
||||
if (ret != 0) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
signed int pid = fork();
|
||||
int status;
|
||||
|
||||
if (pid == -1) {
|
||||
fprintf(stderr, "Couldn't fork. Error = %s\n", strerror(errno));
|
||||
return 1;
|
||||
} else if (pid > 0) {
|
||||
waitpid(pid, &status, 0);
|
||||
} else {
|
||||
execvp(args[0], args);
|
||||
}
|
||||
free(args);
|
||||
|
||||
return status;
|
||||
}
|
||||
|
||||
int main() {
|
||||
FILE *os_release = fopen("/etc/os-release", "r");
|
||||
if (!os_release) {
|
||||
fprintf(stderr, "Couldn't open os-release. Error = %s\n", strerror(errno));
|
||||
}
|
||||
char buffer[1024];
|
||||
while (fgets(buffer, sizeof buffer, os_release)) {
|
||||
if (strncmp(buffer, "NAME=", 5) == 0) {
|
||||
memmove(buffer, buffer + 6, (sizeof buffer) - 6);
|
||||
buffer[strcspn(buffer, "\"")] = '\0';
|
||||
break;
|
||||
}
|
||||
}
|
||||
int ret;
|
||||
char *args[100];
|
||||
if (strcmp(buffer, "EndeavourOS") == 0) {
|
||||
ret = run_cmd_and_wait("yay");
|
||||
} else if (strcmp(buffer, "Debian") == 0) {
|
||||
ret = run_cmd_and_wait("apt update");
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = run_cmd_and_wait("apt upgrade");
|
||||
} else if (strcmp(buffer, "FreeBSD") == 0) {
|
||||
ret = run_cmd_and_wait("pkg update");
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
ret = run_cmd_and_wait("pkg upgrade");
|
||||
} else {
|
||||
fprintf(stderr, "OS %s not suported\n", buffer);
|
||||
return 1;
|
||||
}
|
||||
return ret;
|
||||
}
|
@ -1,8 +0,0 @@
|
||||
# Backup to this folder
|
||||
BACKUP_LOCATION=~/
|
||||
|
||||
# use this pacman-wrapper
|
||||
PACMAN_WRAPPER=yay
|
||||
|
||||
# how many different backups to keep
|
||||
BACKUP_AMOUNT=2
|
Reference in New Issue
Block a user