Subversion Repositories HelenOS

Rev

Rev 2380 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
2404 konopa 1
/*
2
 * Copyright (c) 1987,1997, Prentice Hall
3
 * All rights reserved.
4
 *
5
 * Redistribution and use of the MINIX operating system in source and
6
 * binary forms, with or without modification, are permitted provided
7
 * that the following conditions are met:
8
 
9
 * - Redistributions of source code must retain the above copyright
10
 *   notice, this list of conditions and the following disclaimer.
11
 
12
 * - Redistributions in binary form must reproduce the above
13
 *   copyright notice, this list of conditions and the following
14
 *   disclaimer in the documentation and/or other materials provided
15
 *   with the distribution.
16
 
17
 * - Neither the name of Prentice Hall nor the names of the software
18
 *   authors or contributors may be used to endorse or promote
19
 *   products derived from this software without specific prior
20
 *   written permission.
21
 
22
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS, AUTHORS, AND
23
 * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
26
 * IN NO EVENT SHALL PRENTICE HALL OR ANY AUTHORS OR CONTRIBUTORS BE
27
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
28
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
29
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
30
 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
31
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
32
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
33
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34
 */
35
 
36
 
2380 konopa 37
/* This file contains the code for performing four system calls relating to
38
 * status and directories.
39
 */
40
 
41
/* Methods:
42
 * do_chdir:  perform the CHDIR system call
43
 * do_stat:   perform the STAT system call
44
 * do_fstat:  perform the FSTAT system call
45
 */
46
 
47
 
48
#include "fs.h"
49
#include "stat.h"
50
#include "file.h"
51
#include "fproc.h"
52
#include "inode.h"
53
#include "param.h"
54
 
55
 
56
static int change(inode_t **iip, char *name_ptr, int len);
57
static int stat_inode(inode_t *rip, filp_t *fil_ptr, char *user_addr);
58
 
59
int do_chdir()
60
{
61
 
62
    /* Perform the chdir(file_name) system call */
63
    int r;
64
 
65
    r = change(&fp->fp_workdir, file_name, strlen(file_name)+1);
66
 
67
    return r;
68
}
69
 
70
int change(inode_t **iip, char *name_ptr, int len)
71
{
72
 
73
    /*
74
     * iip:     pointer to the inode pointer for the dir
75
     * name_ptr:    pointer to the directory name to change to
76
     * len:     length of the directory name string
77
     */
78
 
79
    /* Do the actual work for chdir() and chroot(). */
80
 
81
    inode_t *rip;
82
    register int r;
83
 
84
    r = OK;
85
 
86
    /* Try to open the new directory. */
87
    if (fetch_name(name_ptr, len) != OK)
88
        return err_code;
89
 
90
    if ((rip = eat_path(user_path)) == NIL_INODE)
91
        return err_code;
92
 
93
    /* It must be a directory and also be searchable. */
94
    if ((rip->i_mode & I_TYPE) != I_DIRECTORY)
95
        r = FS_ENOTDIR;
96
 
97
    /* If error, return inode. */
98
    if (r != OK) {
99
        put_inode(rip);
100
        return r;
101
    }
102
 
103
    /* Everything is OK.  Make the change. */
104
    put_inode(*iip);    /* release the old directory */
105
    *iip = rip;     /* acquire the new one */
106
 
107
    return OK;
108
}
109
 
110
int do_stat()
111
{
112
 
113
    /* Perform the stat(name, buf) system call. */
114
 
115
    register inode_t *rip;
116
    register int r;
117
 
118
    /* Both stat() and fstat() use the same routine to do the real work.  That
119
     * routine expects an inode, so acquire it temporarily.
120
     */
121
 
122
    if (fetch_name(file_name, strlen(file_name)+1) != OK)
123
        return err_code;
2404 konopa 124
 
2380 konopa 125
    if ((rip = eat_path(user_path)) == NIL_INODE)
126
        return err_code;
2404 konopa 127
 
2380 konopa 128
    r = stat_inode(rip, NIL_FILP, fp->buffer); /* actually do the work.*/
129
    put_inode(rip);     /* release the inode */
130
 
131
    return r;
132
}
133
 
134
int do_fstat()
135
{
136
 
137
    /* Perform the fstat(fd, buf) system call. */
138
 
139
    register filp_t *rfilp;
140
 
141
 
142
    /* Is the file descriptor valid? */
143
    if ((rfilp = get_filp(fd)) == NIL_FILP)
144
        return err_code;
145
 
146
    if (rfilp->filp_ino == NIL_INODE)
147
        return FS_ENOENT;
148
 
149
    return(stat_inode(rfilp->filp_ino, NIL_FILP, fp->buffer));
150
}
151
 
152
int stat_inode(register inode_t *rip, filp_t *fil_ptr, char *user_addr)
153
{  
154
 
155
    /*
156
     * rip:         pointer to inode to stat
157
     * fil_ptr:     filp pointer, supplied by 'fstat'
158
     * user_addr:   user space address where stat buf goes
159
     */
160
 
161
    /* Common code for stat and fstat system calls. */
162
 
163
    stat_t statbuf;
164
 
165
    /* Fill in the statbuf struct. */
166
    statbuf.st_ino = rip->i_num;
167
    statbuf.st_mode = rip->i_mode;
168
    statbuf.st_nlink = rip->i_nlinks & BYTE;
169
    statbuf.st_size = rip->i_size;
170
 
171
    /* Copy infomations to userspace. */
172
    memcpy(user_addr, &statbuf, sizeof(statbuf));
173
 
174
    return OK;
175
}
176