Subversion Repositories HelenOS

Rev

Rev 2404 | 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
 */
2435 jelen 35
 
36
/** @addtogroup FileSystemImpl
37
* @{
38
*/
2404 konopa 39
 
2435 jelen 40
/**
41
 * @file    open.c
42
 * @brief   This file contains the procedures for opening, closing, and seeking on files.
2374 konopa 43
 */
44
 
45
#include "fs.h"
46
#include "block.h"
47
#include "file.h"
48
#include "fproc.h"
49
#include "inode.h"
50
#include "param.h"
51
 
2435 jelen 52
/**
53
 * Perform the OPEN system call
54
 */
2374 konopa 55
int do_open()
56
{
57
 
58
    /* Perform the open(file_name) system call */
59
 
60
    int r;
61
    register inode_t *rip;
62
    filp_t *fil_ptr;
63
 
64
 
65
    r = fetch_name(file_name, strlen(file_name)+1);
66
    if (r != OK)
67
        return err_code; /* name was bad */
68
 
69
    /* See if file descriptor and filp slots are available. */
70
    if ( (r = get_fd(0, &fd, &fil_ptr)) != OK)
71
        return r;
72
 
73
    /* Scan path name. */
74
    if ( (rip = eat_path(user_path)) == NIL_INODE)
75
        return err_code;
76
 
77
    /* Claim the file descriptor and filp slot and fill them in. */
78
    fp->fp_filp[fd] = fil_ptr;
79
    fil_ptr->filp_count = 1;
80
    fil_ptr->filp_ino = rip;
81
    fil_ptr->filp_flags = R_BIT;
82
 
83
    return fd;
84
}
85
 
2435 jelen 86
/**
87
 * Perform the CLOSE system call
88
 */
2374 konopa 89
int do_close()
90
{
91
 
92
    /* Perform the close(fd) system call. */
93
 
94
    register filp_t *rfilp;
95
    register inode_t *rip;
96
 
97
 
98
    /* First locate the inode that belongs to the file descriptor */
99
    if ( (rfilp = get_filp(fd)) == NIL_FILP)
100
        return err_code;
101
 
102
    rip = rfilp->filp_ino;        /* 'rip' points to the inode */
103
 
104
    if (--rfilp->filp_count == 0) {
105
        put_inode(rip);
106
    }
107
 
108
    fp->fp_filp[fd] = NIL_FILP;
109
 
110
    return OK;
111
}
112
 
2435 jelen 113
/**
114
 * Perform the SEEK system call
115
 */
2374 konopa 116
int do_lseek()
117
{
118
 
119
    /* Perform the seek(ls_fd, offset, whence) system call. */
120
 
121
    register filp_t *rfilp;
122
    register offset_t pos, new_pos;
123
 
124
 
125
    /* Check to see if the file descriptor is valid. */
126
    if ( (rfilp = get_filp(fd)) == NIL_FILP)
127
        return err_code;
128
 
129
 
130
    /* The value of 'whence' determines the start position to use. */
131
    switch(whence) {
132
        case 0: pos = 0;
133
            break;
134
            case 1: pos = rfilp->filp_pos;  
135
            break;
136
            case 2: pos = rfilp->filp_ino->i_size;  
137
            break;
138
            default: return FS_EINVAL;
139
    }
140
 
141
    /* Check for overflow. */
142
    if (((long)offset > 0) && ((long)(pos + offset) < (long)pos))
143
        return FS_EINVAL;
144
    if (((long)offset < 0) && ((long)(pos + offset) > (long)pos))
145
        return FS_EINVAL;
146
 
147
    new_pos = pos + offset;
148
 
149
    /* Check for setting behind the end. */
150
    if ((new_pos) > rfilp->filp_ino->i_size) {
151
        pos = rfilp->filp_ino->i_size;
152
    }
153
    else if (new_pos < 0) {
154
          pos = 0;
155
    }
156
    else {
157
        pos = new_pos;
158
    }
159
    rfilp->filp_pos = pos;
160
 
161
    return pos;
162
}
163
 
2435 jelen 164
/**
165
 * }
166
 */
167