免责声明:这是作业;我不想要解决方案。
此外,除了 c/c++ 标准库之外,没有可用的库。
我正在寻找正确方向的插入力,以了解我分配的学期项目(创建虚拟 FTP 服务器)中的这部分工作甚至要求我做什么:
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
我向我的讲师提出了这个问题:
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)?
得到以下答复:
These mappings are just example settings. Your solution should be able map anything to anything it similar way.
根据我目前的理解,我只需要允许我的 FTP 服务器的用户访问明确映射的目录和文件(通过配置文件指定)。这可能意味着像 /home ->/home/users
这样的映射(所以所有用户都会看到他们在一个伪根目录中用于 FTP-ing 的东西,例如用户 Bob 看到 /home/bob/
。
此外,我需要使用哪个 API 来支持 FTP 命令,如 ls
、cd
等,这些命令可用于真正的底层文件系统?
最佳答案
您正在创建自己的 FTP 服务器(或至少其中的一部分)。需要解决的是/home/bob
转换为/home/users/bob
的问题。我相信你打算这样做的方式是,如果有人键入 cd/home/bob
,你只需将传入的文件位置转换为采用用户提供的 pat 的函数(在此case/home/bob
) 到它的“真实”形式 (/home/users/bob
),然后再传递给 chdir
函数更改目录。要使 pwd
和 ls
之类的东西显示正确的路径,你要么需要“记住你在哪里”(记住有人可能想做 cd ../joe
、cd ../tom/.././mats/../joe
或 cd ..; cd joe
到移至 /home/joe
,所有 [modulo my typos] 都应转换为 /home/users/joe
但显示为 /home/joe
- 换句话说,您的 cd
需要了解当前目录 .
和父目录 ..
才能四处移动),或者有一个采用 /home/users/joe
并产生 /home/joe
的“反向翻译”。我目前认为后者更简单,但我还没有完全解决这个问题。
您可能可以遵循几种解决方案,但是“匹配字符串的开头”和在绝对路径中工作是可行的,除非您想做非常复杂的事情并且允许您不需要用户做真正复杂的事情,例如,如果我们有这个映射:
/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)
现在,如果用户要做:
cd /home/bob/../../www/ (could be worse with more . and .. mixed in)
然后你需要真正了解你在哪里,并再次将 fix up ../.. 翻译成/。 [当然,使用 cd/home/bob
然后 cd ..
和 cd www
可能会出现类似的问题]。
我想澄清一下您的讲师是否真的要求这样做。
如果不需要,则匹配以 /
开头的任何内容的开头(其他所有内容,只需传递给 chdir
而无需更改)
关于c++ - 了解 FTP 服务器项目的 "create a virtual filesystem which allows mapping of arbitrary directories",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24170945/