http://prdownloads.sourceforge.net/lufs/lufs-0.9.7.tar.gz?download
[lufs.git] / filesystems / ftpfs / ftpsys_unix.cpp
1 /*
2  * ftpsys_unix.cpp
3  * Copyright (C) 2002 Florin Malita <mali@go.ro>
4  *
5  * This file is part of LUFS, a free userspace filesystem implementation.
6  * See http://lufs.sourceforge.net/ for updates.
7  *
8  * LUFS is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * LUFS is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
21  */
22
23
24 #include <unistd.h>
25 #include <stdlib.h>
26 #include <stdio.h>
27 #include <ctype.h>
28 #include <string.h>
29
30 #include <sys/types.h>
31 #include <sys/stat.h>
32
33 #ifndef __USE_XOPEN
34 #define __USE_XOPEN
35 #endif
36
37 #include <time.h>
38
39 #include <lufs/proto.h>
40 #include <lufs/fs.h>
41
42 #include "ftpsys.h"
43 #include "ftpsys_unix.h"
44
45 static char*
46 next_word(char *c){
47     while( (*c != ' ') && (*c != 0))
48         c++;
49     if(! *c)
50         return c;
51
52     while(*c == ' ')
53         c++;    
54     return c;
55 }
56
57 static char*
58 nth_word(char *line, int n){
59   int i;
60   char *c;
61
62   for(c = line, i = 0; (c != NULL) && (i < n); i++)
63     c = next_word(c);
64
65   return c;
66 }
67
68 ftpsys_unix::ftpsys_unix(){
69     CMD_LIST = "LIST -al";
70 }
71
72 ftpsys_unix::~ftpsys_unix(){
73
74 }
75
76 int
77 ftpsys_unix::parse_line(char *buf, char *file, struct lufs_fattr *fattr, char *link, struct credentials *cred){
78     unsigned long nlink, size;
79     int res;
80     struct tm tm;
81     time_t tt;
82     char user[32], group[32], month[5], day[5], year[6], date[20];
83     char *c, *cc;
84
85     *file = *link = user[0] = group[0] = month[0] = day[0] = year[0] = 0;
86
87     if((res = sscanf(buf, "%*11s %lu %32s %32s %lu %3s %2s %5s %1024s -> %1024s", &nlink, user, group, &size, month, day, year, file, link)) < 7){
88         TRACE("could only match "<<res<<" attributes");
89         return -1;
90     }
91
92     sprintf(date,"%s,%s,%s", year, month, day);
93     tt = time(NULL);
94     gmtime_r(&tt, &tm);
95     tm.tm_sec = tm.tm_min = tm.tm_hour = 0;
96     if(strchr(year, ':'))
97         strptime(date, "%H:%M,%b,%d", &tm);
98     else
99         strptime(date, "%Y,%b,%d", &tm);
100
101     memset(fattr, 0, sizeof(struct lufs_fattr));
102
103     strtol(user, &c, 10);
104     if(*c == 0){
105         if((int)cred->uid == atoi(user))
106             fattr->f_uid = 1;
107     }else{
108         if(!strcmp(cred->user, user))
109             fattr->f_uid = 1;
110     }
111
112     strtol(group, &c, 10);
113     if(*c == 0){
114         if((int)cred->gid == atoi(group))
115             fattr->f_gid = 1;
116     }else{
117         if(!strcmp(cred->group, group))
118             fattr->f_gid = 1;
119     }
120
121     fattr->f_nlink = nlink;
122     fattr->f_size = size;
123     fattr->f_ctime = fattr->f_mtime = fattr->f_atime = mktime(&tm);
124     
125     if(tolower(buf[0]) == 'd') fattr->f_mode = S_IFDIR;
126     else if(tolower(buf[0]) == 'l') fattr->f_mode = S_IFLNK;
127     else fattr->f_mode = S_IFREG;
128     
129     if(tolower(buf[1]) != '-') fattr->f_mode |= S_IRUSR;
130     if(tolower(buf[2]) != '-') fattr->f_mode |= S_IWUSR;
131     if(tolower(buf[3]) != '-') fattr->f_mode |= S_IXUSR;
132     if(tolower(buf[4]) != '-') fattr->f_mode |= S_IRGRP;
133     if(tolower(buf[5]) != '-') fattr->f_mode |= S_IWGRP;
134     if(tolower(buf[6]) != '-') fattr->f_mode |= S_IXGRP;
135     if(tolower(buf[7]) != '-') fattr->f_mode |= S_IROTH;
136     if(tolower(buf[8]) != '-') fattr->f_mode |= S_IWOTH;
137     if(tolower(buf[9]) != '-') fattr->f_mode |= S_IXOTH;
138
139     for(c = buf; *c; c++){
140         if((*c == 0x0a) || (*c == 0x0d)){
141             *c = 0;
142             break;
143         }
144     }
145
146
147     if((c = nth_word(buf, 8))){
148
149       TRACE("left: " << c);
150       cc=strstr(c, "->");
151       if(cc != NULL){
152         *(cc-1) = 0;
153         strcpy(file, c);
154         strcpy(link, (cc + 3));
155       }else{
156         strcpy(file, c);
157       }
158
159       TRACE("file: " << file<<", link: " << link);
160       return 0;
161     }else
162       return -1;
163 }