0

Disclaimer: This is homework; I don't want a solution.

Also, no libraries outside c/c++ standard libraries are available.

I'm looking for a push in the right direction to understand what this portion of work from my assigned semester project (create a virtual FTP server) is even asking me to do:

The server allows to create a virtual filesystem. By a virtual filesystem, we mean a mapping of a served directory to the real directory on the filesystem. For example, the client tree will look like: /home/user1 maps to /mnt/x/home/user1 /www maps to /var/cache/www /home/user_list.txt maps to /var/ftpclient/user_list.txt The user will see /home/user1 directory and /www directory and the file /home/user_list.txt

I followed up with this question to my lecturer:

Are /home/user1 -> /mnt/x/home/user1 , /www -> /var/cache/www , and /var/cache/www/home/user_list.txt -> /var/ftpclient/user_list.txt the only directory mappings which need to be supported (so each user will have 2 directories and 1 file as shown automatically created for them)?

to which the following reply was given:

These mappings are just example settings. Your solution should be able map anything to anything it similar way.

From my current understanding, I need to only allow users of my FTP server to access directories and files which are explicitly mapped (specified via the configuration file). This will probably mean a mapping of something like /home -> /home/users (so all users will see that they're in a pseudo root directory for FTP-ing stuff, e.g. user Bob sees /home/bob/.

Also, with which API do I need to work to support FTP commands like ls, cd, etc. which work with the real unerlying file system?

ironicaldiction
  • 1,200
  • 4
  • 12
  • 27

2 Answers2

1

You are creating your own FTP server (or at least a portion thereof). It will need to solve the problem of /home/bob translates to /home/users/bob. I believe the way you are meant to do this is that if someone types cd /home/bob, you simply translate the passed in file-location to a function that takes the user-provided pat (in this case/home/bob) to it's "real" form (/home/users/bob) before it's passed to the chdir function that actually changes the directory. To make things like pwd and ls show the correct path, you will either need to "remember where you are" (bearing in mind that someone may want to do cd ../joe, cd ../tom/.././mats/../joe, or cd ..; cd joe to move to /home/joe, which should all [modulo my typos] translate to /home/users/joe but display as /home/joe - in other words, your cd will need to understand the current directory . and parent directory .. to move around), or have a "reverse translation" that takes /home/users/joe and comes up with /home/joe. It's my current thought that the latter is simpler, but I haven't solved EXACTLY this problem.

There are probably several solutions that you can follow, but a "match start of string" and working in absolute paths would work unless you want to do very complicated things and allow you don't need users to do REALLY complicated things, for example, if we have this mapping:

/home -> /mnt/x/home     (e.g /home/bob becomes /mnt/x/home/bob)
/www  -> /var/cache/www    (e.g /www/index.html becomes /var/cache/www/index.html)

Now, if a user were to do:

cd /home/bob/../../www/    (could be worse with more . and .. mixed in)

then you need to actually understand where you are, and translate fix up ../.. into / again. [Of course, similar problems using a cd /home/bob then cd .. and cd www may pose similar problems].

I would clarify if that is actually required by your lecturer.

If it is not required, then match the start of anything starting with / (everything else, just pass to chdir without change)

Mats Petersson
  • 126,704
  • 14
  • 140
  • 227
0

The last question is the easiest: use the Boost Filesystem library, it has the types you'll need such as file paths.

For the first question, the idea is that GET /home/user_list.txt returns the contents of /var/ftpclient/user_list.txt. That means you first need to translate the virtual name into a real name (Some fanciness is possible here, but basically you want to check if any prefix of the virtual name appears in the translation table. Fanciness includes dealing with the case of names not found). Secondly, with the real name you want to open that file, read its contents, and return those to the client.

MSalters
  • 173,980
  • 10
  • 155
  • 350