-1

I have created a debian package and added the following code in the postinst script:

path="$HOME/sample"
echo "$path"
if [ -d "$path" ]
then
 rm -rf "$path"
 echo 'File deleted successfully :)'
fi

so that if the path is present, it would delete it during installation. It works perfectly when I install my .deb package through dpkg. But while installing through Ubuntu software centre, none of it works. Why does this happen?

For background, I have made an app that would create a directory in the home directory of the user or root installing to the system .So if I am reinstalling or installing again after uninstalling, I need to check if the directory is present or not; if present, I need to delete it. I have distributed the app as a Debian package. So the question is how to check if the directory is present in the home directory? The directory is not created while installing the app. It is externally created while running the app. Also note that I cannot change it to a different folder because the app cannot be changed.

tripleee
  • 175,061
  • 34
  • 275
  • 318
Dev
  • 95
  • 1
  • 12
  • What do you expect `$HOME` to contain? The `postinst` script will be running as `root` and it should not be touching anyting outside of `/usr` (and possibly `/etc`) anyway. – tripleee Jan 22 '15 at 13:02
  • I meant the home path of the root or user who is installing the .deb package .If it's not touching outside of /usr then how could it possibly work while using dpkg command. – Dev Jan 22 '15 at 13:08
  • I don't understand your question. Binaries get installed in `/usr/bin`, support files get installed in `/usr/share` or `/usr/lib`, configuration files in `/etc`. I'm not saying it's technically impossible, and what you are trying is probably just a quick proof of concept to help you understand how to do something actually useful, but my first step of inquiry would be to change the script so you see what `$HOME` expands to, or use a less wacky location for your test file. – tripleee Jan 22 '15 at 13:15
  • Ok.I understood what if I am installing a deb package in an user having su privilege.While executing sudo dpkg command in terminal it deletes the directory and shows the user's home path too..But while installing same deb package using ubuntu software centre is that the script is not executing?or the path is not accessible ?I have given the same password for the user used in dpkg and Ubuntu software centre.. – Dev Jan 22 '15 at 14:07
  • With `sudo` your own `$HOME` variable might be visible to `dpkg` but that's not something you can generally rely on. Again, `$HOME` is not a good place to muck with in `postinst`. I'm still hoping this is not central to your question and that what you actually want to accomplish would thus be achievable. – tripleee Jan 22 '15 at 16:47
  • ok thanks for the help.Let me try . – Dev Jan 23 '15 at 06:43

1 Answers1

2

The problem is not with Ubuntu, but with your use of HOME in the postinst. It apparently happens to work with sudo dpkg from your own account (although in some settings, sudo would not propagate your HOME then, either) but this is not supported or well-defined.

HOME does not make sense in a Debian package anyway, because it is a system-wide install, and HOME is a per-user variable.

If I understand your requirement correctly, you need to loop through the home directories of all users, and remove the sample folder from each if present.

# Ad-hoc
getent passwd | cut -d: -sf6 |
while read dir; do
    test -d "$dir" || continue
    rm -rvf "$dir/sample"
done

This is extremely intrusive so you really should try to change the app instead -- what if a user has a directory named sample for some other reason? The app should use a reasonably unique dot name (.appname-sample?) instead, or store its per-user data in a system location where it can be properly managed by the system.

In fact, in the meantime, your postinst script should probably only move the sample directory to something like .sample.dpkg-old. This is no less intrusive, but at least it avoids destroying your users' data completely by silly mistake.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • Ok,then so can you tell me how to check if a particular directory is present or not in home directory of root or user while Debian package installation through software centre.?What if i am using a notifiy msg like"notify-send -u critical -i "Test" "new test message" " – Dev Jan 23 '15 at 07:07
  • I still don't see how `notify-send` figures into this. You cannot rely on a user to be logged in when the `postinst` script runs so it is also not clear where the notification should be delivered. As a rough approximation, I added a `-v` option to `rm` which causes it to display removed files on its standard error. If it is invoked via e.g. `cron`, the output will typically be delivered to the administrator via email. – tripleee Jan 23 '15 at 08:26
  • Ya I got it.Thanks a lot ..for helping me ...I am able to get the result..thanks a lot .I have removed the notify send command and used the give loop and it works perfectly ..Thank You..:)) – Dev Jan 23 '15 at 09:11