3

I'm trying to write a test case for some code that should iterates over a filesystem tree, but should stop if a file is on another device. In writing tests for it, I considered creating a file in /dev/shm, symlinking to it from my test directory, and then checking that it isn't read, but /dev/shm seems to be Linux-specific. mount-ing a tmpfs could be an option, but it requires root...

Any ideas how this can be accomplished?

Brennan Vincent
  • 10,736
  • 9
  • 32
  • 54
  • I was thinking in creating your own dummy filesystem for this purpose. The reading [Does mount always require root privileges?](http://unix.stackexchange.com/q/20838/40596) suggests that you may be able to without root, as long as you are able to configure `/etc/fstab` before. And this of course needs root at least once. Can you do that or you are looking for a fully non-root way? – fedorqui Jan 24 '15 at 17:52
  • Devices have a major and minor number. The only ways a directory tree can have multiple devices in the sense you mean (ignoring zpools for example) is for there to exist one of: a mountpoint, or a symbolic link. The stat answer covers that in both cases. @RolandSmith – jim mcnamara Jan 24 '15 at 18:05
  • The short answer to the question in the title is "no", since it is possible to have complete systems living in only one device (i.e. the root file system). – Jens Jan 24 '15 at 18:19

2 Answers2

3

Try FUSE : http://fuse.sourceforge.net/. FUSE is supported under Linux, FreeBSD/NetBSD, OpenSolaris, and Mac OS X, and hence, whatever toy filesystem you use for your tests (see http://sourceforge.net/p/fuse/wiki/FileSystems/) will

a) be cross-platform b) in user-space (no root required) and c) not require too much of test scaffolding to set up and tear down

vijucat
  • 2,059
  • 1
  • 15
  • 17
0

It depends on how you define "device". Do you consider files on different filesystems on the same disk on the same device or not?

Using stat(2) you can get the numeric ID from the filesystem containing the field in the st_dev field of the returned struct stat. That way you can distinguish between filesystems.

Roland Smith
  • 42,427
  • 3
  • 64
  • 94
  • "Different device" means the value for `st_dev` returned by `stat(2)` is different, as you point out. My question is not about how to distinguish between files on different devices. – Brennan Vincent Jan 24 '15 at 22:08