Raspberry Pi 3 (Raspbian) programatically accessing a USB drive: best way to do this? - raspbian

I'm running Raspbian 1.0, v9/15 on an RP3. I'm working on an application that will access a dedicated USB flash drive to store collected data. The static path to write the data is "/media/user/kingston". This works great unless I have a system crash at which point the owner and group for "/media/user/kingston" are changed to root, and the mount point for the flash drive now is now changed to "/media/user/kingston1" which (clearly) does not work with the static path defined in my application. Is there a standard dynamic path option that is available for this device and OS? Thanks in advance...
Update: Based on BJ Black's advice, this is what I added to fstab after creating a directory named /usb at the root of the filesystem,
/dev/sda1 /usb vfat noatime,nofail,user,rw,exec,umask=000 0 3
The advantages are,
1) allows bootup even when no USB drive is inserted,
2) allows any usb storage device to be used here.
Disadvantage is that the system must be powered down (or drive umounted) before flash drive is removed otherwise bad things happen, (I tried this). Question: Is there anything that I can do to reduce the "bad things" in the event that someone pulls out the usb while it is still mounted? Thanks in advance.

In general, I tend to prefer using a hard mount point for this kind of thing. There are possibly some neat tricks in /sys (or by using a grep against the output of mount(1) or /proc/mounts), but they'll fail as soon as you change out your Kingston stick for some other vendor (yuck).
Have a look at this link to better understand what's automounting your usb stick and how to shut that mechanism off.
As for mounting in a permanent place, I'd try either:
Create a mount point (say /data) and put an entry in fstab(5) so it gets mounted automatically on boot.
Same as 1, but use the noauto and user and do an explicit call to mount(1) from your application, or...
Install autofs and put an entry in auto.misc to automount the device (probably /dev/sda1) on use. Then symlink /data -> /misc/data.
Of these, I like option 3 the best, as the stick will unmount automatically when idle. Then your application need only double-check that the path is writable beforehand.
Also be super-careful on writes, as cheapie USB sticks tend to erase in large blocks and their wear levelling is weak at best (so both small random writes and large writes tend to wear the flash fast). Using f2fs or careful write buffering helps (and the noatime option on mount helps). And don't call fsync() all the time in your app!

Related

How do small teams do secure backups of source code?

First of all, I don't mean version control such as git.
I do use git locally but, I'm trying to determine the best way to do back-ups of source code (as well as other app assets) in case of hardware failure or such.
I was thinking I could set up a script to tar my project folders, and encrypt them with gpg. I would then save the encrypted tar to external hard drives and to 1 or more off-site locations using a service such as amazon drive or dropbox.
Currently, I'm a sole developer so my thinking was that this method should be okay. But I wanted to get some input to make sure I'm doing this the best/most reliable way possible.
If there is a better approach to this that may be more applicable to small teams, then please let me know, as I'm more than happy to do the extra work implementing the approach.
There are much of ways of doing that.
But, if you always work local and you need a simple way of doing that, you may take a look at run scripts if some specific usb device is plugged in.
Meaning that a simple backup script with tar would run if you plug in your specific backup hdd.
Take a look at udev rules in linux.
udev is a generic device manager running as a daemon on a Linux system and listening (via a netlink socket) to uevents the kernel sends out if a new device is initialized or a device is removed from the system. The udev package comes with an extensive set of rules that match against exported values of the event and properties of the discovered device. A matching rule will possibly name and create a device node and run configured programs to set up and configure the device.
Take a look at these posts:
https://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device
&
https://askubuntu.com/questions/401390/running-a-script-on-connecting-usb-device
If you plan to go further, to extend the team or even to keep your code for a while in other words, if you want to be professional, I would go with a scalable and reliable tool designed for this: use a real backup and restore tool and don't use scripts. A lot of people, small (and even not so small) companies are doing it and they end up in trouble: maintenance, scalabolity, update, and so on.
There are plenty of backup & restore tools for different purposes and/or platforms, prices and so on. https://en.wikipedia.org/wiki/List_of_backup_software would be a good start :)
Cheers
Werlan

In OSX (Mountain Lion) Only allowing an application to open if a condition is met

Is there any simple way to do this?
I basically want iTunes to not open if an external hard drive is not connected. This is essentially a user issue - as despite asking multiple times, my girlfriend will forget and open an audio or video file without the external HDD connected (where the iTunes library is kept) and so the usual rigamarole occurs... the media file will then try and add itself to the iTunes library, which can't be found, so it'll default back to it's position on the internal hard disk, and then when I come to use it, it'll try and consolidate it for me (which is nice), except it then decides it needs to organise it - and this takes about 6 hours due to the amount of music I have on there.
I've tried changing the internal (default) iTunes music folder path to an alias to the external one, but that starts throwing out some beastly errors once you get into the loop (when the alias is essentially a pointer to an invalid location).
Is Automator something that could be used? Sorry I'm not very pro with OS X I'm afraid.
Thanks,
Duncan
It seems a straightforward way to do this is just to move the iTunes.app onto the external hard drive, and replace iTunes.app in the Applications folder with an alias pointing to iTunes on the external HDD (called iTunes).
As iTunes is system protected, you'll need to change the permissions of iTunes.app to move it, which you can do via gui or terminal (sudo chmod...)
Simples.

Use the Windows raw access APIs to directly access the USB drive

At the end of USB physical drive, I want to write data directly using windows raw access API. I don't want to use kernel driver to do that.
As far as I know, HDD direct access is blocked from windows xp sp2 or sp3(?) for security reason. I'm not sure this is true for USB drive.
Please guide me how to get there. Thanks a lot.
It depends on how "direct" you want to be.
Something like
HANDLE hDrive = CreateFile("\\\\.\\F:", ...);
ReadFile(hDrive, ...);
should get you what you need in most situations, although you might need DeviceIoControl with
IOCTL_USB_USER_REQUEST with USBUSER_PASS_THRU
IOCTL_SCSI_PASS_THROUGH_DIRECT
or IOCTL_ATA_PASS_THROUGH_DIRECT
if you're doing something really advanced.
P.S.: This should be on StackOverflow, as other people have mentioned.
Voted to move to Stack Overflow. I think regardless, using an external HDD connected by USB won't change anything, as it still appears as a normal disk to windows.
When you say "raw access API", do you mean functions like CreateFile, WriteFile etc as listed here? Because those functions should be able to be called from normal Win32 apps. Or do you want direct access to the disk itself, with no filesystem layer in between? (Some user apps such as HxD can directly open disks for reading/writing - use with caution)

OSX: Hook file read event

I have a particular file I want to monitor for file read attempts by all applications on OSX. I'd like to be able to interrupt the requests so I could decide which applications have permission to read the file and which don't (by querying the user, or checking a cache of user responses). Is this possible with the OSX API? If not, is it even possible to get a list of which applications or processes do read a file?
I'm not saying there's no way to do this, but what #Jonathan is talking about isn't it.
That API is for tracking the creation, change, and destruction of files. Notably this tool is used by things like Spotlight to watch activity on the filesystem for new, interesting files.
But, wisely, reading isn't one of the events it tracks.
And even if reading WAS tracked, it is still the wrong mechanism, as it's a notification system after the fact, not in line with the call itself.
I seriously doubt what you want is possible the way you describe it.
With Access Control Lists, you can limit access at the user level (Fred can read the file, but Bob can not). This is a setting on the file itself. But there's no mechanism to allow Bobs App1 to read a file, while Bobs App2 can not, since there's really no formal mechanism of "application identity" beyond the command to executed, or whatever the program "says" its name is (both of which can be spoofed if motivated enough).
However, feel free to crawl the Darwin sources -- no doubt the answer is buried in there somewhere near the open(2) call.
EDIT, regarding comment.
What are you trying to do? What's the overall context?
Another thing that you may want to try is to use FUSE.
FUSE is a utility that let's you have "user space filesystems". People use FUSE for many purposes, like reading NTFS volumes, or mounting remote system via SSH.
They have a simple example, that gives you a skeleton that you can fill in for your purposes.
For most of the use cases, you'll simple defer to the system. However, for OPEN you will add your logic. Then you could point your FUSE utility at a directory, and "mount it". Then all of the files below that directory can use your new behavior.
I'm still not sure how you will identify Apps by name, but if it's not a real "security" issue, just for local control, I imaging you can come up with something. Activity Monitor has apps names, so they must be available, and FUSE will be running within the process space (I think), rather than through some external mechanism.
All that said, I think FUSE is your best bet, but it's probably not appropriate if you want to do this to "any file" with no preparation by the user (like not installing FUSE). If you wanted to do "any file", your FUSE system would need to be mounted at root, and then you'll simply have a full "clone" of the filesystem, with those files from the normal root "unprotected", while those from your new FUSE root will be protected. So, if someone wanted to NOT use your FUSE system, the real file is readily available to them through the actual file location.
If not, is it even possible to get a list of which applications or processes do read a file?
The command-line tool fs_util allows you to monitor filesystem activity, including reading.
The writings of Amit Singh should come in very handy. He explored the API that provides FileSystem events a few years ago, and provided a sample tool that allows you to intercept FS events. It's open source!
If i remember his conclusion properly, their isn't an official API, but you can use apple's tools to achieve what you want.

File path for J2ME FileConnection?

I'm writing a MIDlet which needs to write file. I'm using FileConnection from JSR-75 to accomplish this.
The intention is to have this MIDlet runnning on as much devices as possible (all MIDP 2.0 devices with JSR-75 support, ideally).
On several emulators and an HTC Touch Pro2, I can perfectly use the following code to get the root of the filesystem:
Enumeration drives = FileSystemRegistry.listRoots();
String root = (String) drives.nextElement();
String path = "file:///" + root;
However, on a Nokia S60 5th edition emulator, trying to open a FileConnection to this path throws a java.lang.SecurityException. Apparently S60 devices do not allow connections to the root of the filesystem. I realise I can use something like System.getProperty("fileconn.dir.photos"), but that isn't supported on all devices either.
So, my actual question: what is the best approach to get a path to create a FileConnection with, that allows for maximum portability?
Thanks.
Edit:
I suppose I could iterate over all the roots in the Enumeration, and check for a writable one, but that's hardly optimal for two reasons. First, there aren't necessarily any writable roots. Second, this could be the phone memory or a memory card, so the storage method wouldn't be consistent across devices, which is rather ugly.
You are supposed to open read-only connections to roots in order to find out what folder they contain.
As a general rule, when opening a read_write connection to a folder throws a SecurityException, try to open a read-only connection to browse through sub-folders in order to find a writable one.
Specifically on Symbian (and other platforms advanced enough to provide secure data cages to your MIDlets), you can use System.getProperty("fileconn.dir.private"); to find a writable area.
I will tell you what we do. We have a test app that just finds out the file system root and the SD card root if applicable. We set this as a jad parameter. The code reads it from the Jad file. Since you dont need to recompile the jar for different devices this works out very well, just change the jad parameter for a handset with different file system root.