source: TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_stat.c @ 1431

Subversion URL: http://proj.badc.rl.ac.uk/svn/ndg/TI05-delivery/trunk/src/bbftp-server-3.2.0/bbftpd/bbftpd_stat.c
Revision 1431, 6.1 KB checked in by spascoe, 14 years ago (diff)

bbftpd.h should be the first #include so that glibc headers pick up
any configuration flags.

Line 
1/*
2 * bbftpd/bbftpd_stat.c
3 * Copyright (C) 1999, 2000, 2001, 2002 IN2P3, CNRS
4 * bbftp@in2p3.fr
5 * http://doc.in2p3.fr/bbftp
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20 */ 
21
22/****************************************************************************
23
24 
25 RETURN:
26        0  Keep the connection open (does not mean that the file has been
27           successfully created)
28        -1 Tell the calling program to close the connection
29 
30 *****************************************************************************/
31#include <bbftpd.h>
32
33#include <stdio.h>
34#include <stdlib.h>
35#include <string.h>
36#include <bbftpd_private_log.h>
37#include <utime.h>
38#if TIME_WITH_SYS_TIME
39# include <sys/time.h>
40# include <time.h>
41#else
42# if HAVE_SYS_TIME_H
43#  include <sys/time.h>
44# else
45#  include <time.h>
46# endif
47#endif
48#include <netinet/in.h>
49#include <sys/types.h>
50#include <sys/stat.h>
51#include <unistd.h>
52#include <common.h>
53#include <daemon.h>
54#include <daemon_proto.h>
55#include <structures.h>
56
57#ifdef NDG_AUTH
58# include "ndg.h"
59#endif
60
61extern int transferoption ;
62extern  int     recvcontrolto ;
63
64int bbftpd_stat(int sock,int msglen) 
65{
66    char    *buffer ;
67    char    *logmessage ;
68    struct  mess_dir *msg_file ;
69    char    *filename ;
70    int     recursif ;
71    int     retcode ;
72
73#ifdef STANDART_FILE_CALL
74    struct stat statbuf;
75    struct stat rfstatbuf;
76#else
77    struct stat64 statbuf ;
78# ifdef WITH_RFIO64
79    struct stat64 rfstatbuf ;
80# else
81    struct stat rfstatbuf;
82# endif
83#endif
84
85    if ( (buffer = (char *) malloc (msglen+1) ) == NULL ) {
86        bbftpd_log(BBFTPD_ERR,"Unable to malloc space for file name (%d)",msglen) ;
87        reply(MSG_BAD,"Unable to malloc space for file name") ;
88        return 0 ;
89    }
90    if ( (logmessage = (char *) malloc (msglen+1024) ) == NULL ) {
91        bbftpd_log(BBFTPD_ERR,"Unable to malloc space for logmessage ") ;
92        reply(MSG_BAD,"Unable to malloc space for logmessage") ;
93        FREE(buffer) ;
94        return 0 ;
95    }
96    /*
97    ** Read the characteristics of the file
98    */
99    if ( readmessage(sock,buffer,msglen,recvcontrolto) < 0 ) {
100        /*
101        ** Error ...
102        */
103        bbftpd_log(BBFTPD_ERR,"Error reading file name") ;
104        FREE(buffer) ;
105        FREE(logmessage) ;
106        return -1 ;
107    }
108    buffer[msglen] = '\0' ;
109    msg_file = (struct mess_dir *) buffer ;
110#ifdef NDG_AUTH
111    if (bbftpd_private_authz_control(MSG_STAT, msg_file->transferoption, msg_file->dirname,
112                                     logmessage) != 0) {
113      bbftpd_log(BBFTPD_ERR, logmessage);
114      reply(MSG_BAD, logmessage);
115      FREE(buffer);
116      FREE(logmessage);
117      return 0;
118    }
119#endif // NDG_AUTH
120
121    transferoption  = msg_file->transferoption ;
122    filename = msg_file->dirname ;
123   
124    if ( (transferoption & TROPT_RFIO) == TROPT_RFIO ) {
125#if defined(WITH_RFIO) || defined(WITH_RFIO64)
126        sprintf(logmessage,"Unable to stat %s",filename) ;
127# ifdef WITH_RFIO64
128        retcode = rfio_stat64(filename,&rfstatbuf);
129# else
130        retcode = rfio_stat(filename,&rfstatbuf);
131# endif
132#else
133        retcode = -1;
134        sprintf(logmessage,"RFIO is not supported by the server") ;
135#endif
136    } else {
137        sprintf(logmessage,"Unable to stat %s",filename) ;
138#ifdef STANDART_FILE_CALL
139        retcode = stat(filename,&statbuf);
140#else
141        retcode = stat64(filename,&statbuf);
142#endif
143    }     
144    if (retcode < 0) {
145        reply(MSG_BAD_NO_RETRY,logmessage) ;
146        FREE(buffer) ;
147        FREE(logmessage) ;
148        return 0 ;
149    } else {
150      char *at, *mt, *ct;
151      if ( (transferoption & TROPT_RFIO) == TROPT_RFIO ) {
152        at = strdup(ctime(&rfstatbuf.st_atime));
153        at[strlen(at)-1]='\0';
154        mt = strdup(ctime(&rfstatbuf.st_mtime));
155        mt[strlen(mt)-1]='\0';
156        ct = strdup(ctime(&rfstatbuf.st_ctime));
157        ct[strlen(ct)-1]='\0';
158
159#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
160# define RF_ST_BLKSIZE_FORMAT "d"
161#else
162# define RF_ST_BLKSIZE_FORMAT "s"
163#endif
164
165#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
166# define RF_ST_BLOCKS_FORMAT LONG_LONG_FORMAT
167#else
168# define RF_ST_BLOCKS_FORMAT "s"
169#endif
170
171        sprintf(logmessage,"%" LONG_LONG_FORMAT " %#08o %" LONG_LONG_FORMAT " %" RF_ST_BLKSIZE_FORMAT " %" RF_ST_BLOCKS_FORMAT "  %d %d (%s) (%s) (%s)",
172                        (long long)rfstatbuf.st_ino, 
173                        rfstatbuf.st_mode,
174                        (long long)rfstatbuf.st_size, 
175#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
176                        rfstatbuf.st_blksize, 
177#else
178                        "N/A",
179#endif
180#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
181                        (long long)rfstatbuf.st_blocks, 
182#else
183                        "N/A",
184#endif
185                        rfstatbuf.st_uid, 
186                        rfstatbuf.st_gid, 
187                        at, 
188                        mt, 
189                        ct) ;
190      } else {
191        at = strdup(ctime(&statbuf.st_atime));
192        at[strlen(at)-1]='\0';
193        mt = strdup(ctime(&statbuf.st_mtime));
194        mt[strlen(mt)-1]='\0';
195        ct = strdup(ctime(&statbuf.st_ctime));
196        ct[strlen(ct)-1]='\0';
197
198#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
199# define ST_BLKSIZE_FORMAT "d"
200#else
201# define ST_BLKSIZE_FORMAT "s"
202#endif
203
204#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
205# define ST_BLOCKS_FORMAT LONG_LONG_FORMAT
206#else
207# define ST_BLOCKS_FORMAT "s"
208#endif
209
210        sprintf(logmessage,"%" LONG_LONG_FORMAT " %#08o %" LONG_LONG_FORMAT " %" ST_BLKSIZE_FORMAT " %" ST_BLOCKS_FORMAT "  %d %d (%s) (%s) (%s)",
211                        (long long)statbuf.st_ino, 
212                        statbuf.st_mode, 
213                        (long long)statbuf.st_size, 
214#ifdef HAVE_STRUCT_STAT_ST_BLKSIZE
215                        statbuf.st_blksize, 
216#else
217                        "N/A",
218#endif
219#ifdef HAVE_STRUCT_STAT_ST_BLOCKS
220                        (long long)statbuf.st_blocks, 
221#else
222                        "N/A",
223#endif
224                        statbuf.st_uid, 
225                        statbuf.st_gid, 
226                        at, 
227                        mt, 
228                        ct) ;
229      }
230      reply(MSG_OK,logmessage) ;
231      FREE(buffer) ;
232      FREE(logmessage) ;
233      FREE(at) ;
234      FREE(mt) ;
235      FREE(ct) ;
236      return 0 ;
237    }
238}
Note: See TracBrowser for help on using the repository browser.