ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi all,
Note, this is regarding OpenBSD.
I've been trying to figure out how to output to a client sftp user the current disk space and total disk space (other other useful information for that matter) each time a user uploads/deletes/creates file/folders etc. I've made numerous posts on different forums/mailing lists and no one seemed to be able to answer my question. So, I decided to e-mail Theo which e-mailed me back suprisingly in 2 mins flat, and wrote the following:
'There is nothing to do what you want. You'll have to do it by hand.'
So, now, I'm here. I have absolutely no idea where to start with this, learning a new language is not a problem. I already know a decent amount of C++, but it seems that all the books that are listed on the OpenBSD website are all regarding C.
Thank you for your help in advance.
considering if you have the sftp source code you will need to insert some code to send a message displaying the disk space. THe disk space code is pretty simple.
first :
man statfs , this is a good start place
Code:
/*Find mount point for a particular directory*/
static char *
find_mount_point (const char *file)
{
char saved_cwd[MAXFILEPATH];
static char mp[MAXFILEPATH];
struct stat last_stat;
getcwd (saved_cwd, MAXFILEPATH);
if (chdir (file) < 0){
return NULL;
}
if (stat (".", &last_stat) < 0){
goto done;
}
/* Now walk up FILE's parents until we find another filesystem or /,
chdiring as we go. LAST_STAT holds stat information for the last place
we visited. */
for (;;)
{
struct stat st;
if (stat ("..", &st) < 0)
goto done;
if (st.st_dev != last_stat.st_dev || st.st_ino == last_stat.st_ino)
/* cwd is the mount point. */
break;
if (chdir ("..") < 0)
goto done;
last_stat = st;
}
/* Finally reached a mount point, see what it's called. */
getcwd (mp, MAXFILEPATH);
done:
/* Restore the original cwd. */
{
int save_errno = errno;
chdir(saved_cwd);
errno = save_errno;
}
return mp;
}
Code:
/*Find statitics of mount*/
static void refreshMount(char *mount){
struct statfs s;
long long usedSize;
long long freeSize;
if (statfs(mount, &s) != 0) {
return;
}
usedSize = (long long)((((long long)s.f_blocks) - s.f_bfree)* s.f_bsize);
freeSize = (long long)(((long long)s.f_bavail) * s.f_bsize);
}
I had to rip some of my code out but this should work for you. Give it a whirl.
Thank you so much for you help jwstric2. I was wondering which books would you suggest to read in order to learn C, and integrate it with the operating system?
Thank you,
Well I never really read a book but one of the bests I've been told is one of the oldest, The C Programming Language by Brian W Kernighan. So some googling searching though and get a grasp of how memory is laid out, how memory allocation works, ect.. I think once I understood most of the underlying concepts of the system, it make programming in any language much more simple.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.