• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  *    conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  *    of conditions and the following disclaimer in the documentation and/or other materials
13  *    provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  *    to endorse or promote products derived from this software without specific prior written
17  *    permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <unistd.h>
33 #include <sys/types.h>
34 #include <dirent.h>
35 #ifdef LOSCFG_FS_VFS
36 #include <fs/fs.h>
37 
_lseek(int fd,off_t offset,int whence)38 off_t _lseek(int fd, off_t offset, int whence)
39 {
40     int ret;
41     struct file *filep = NULL;
42 
43     /* Get the file structure corresponding to the file descriptor. */
44     ret = fs_getfilep(fd, &filep);
45     if (ret < 0) {
46         /* The errno value has already been set */
47         return (off_t)-get_errno();
48     }
49 
50     /* libc seekdir function should set the whence to SEEK_SET, so we can discard
51      * the whence argument here */
52     if (filep->f_oflags & O_DIRECTORY) {
53         /* defensive coding */
54         if (filep->f_dir == NULL) {
55             return (off_t)-EINVAL;
56         }
57         if (offset == 0) {
58             rewinddir(filep->f_dir);
59         } else {
60             seekdir(filep->f_dir, offset);
61         }
62         ret = telldir(filep->f_dir);
63         if (ret < 0) {
64             return (off_t)-get_errno();
65         }
66         return ret;
67     }
68 
69     /* Then let file_seek do the real work */
70     ret = file_seek(filep, offset, whence);
71     if (ret < 0) {
72         return -get_errno();
73     }
74     return ret;
75 }
76 
_lseek64(int fd,int offsetHigh,int offsetLow,off64_t * result,int whence)77 off64_t _lseek64(int fd, int offsetHigh, int offsetLow, off64_t *result, int whence)
78 {
79     off64_t ret;
80     struct file *filep = NULL;
81     off64_t offset = ((off64_t)offsetHigh << 32) + (uint)offsetLow; /* 32: offsetHigh is high 32 bits */
82 
83     /* Get the file structure corresponding to the file descriptor. */
84     ret = fs_getfilep(fd, &filep);
85     if (ret < 0) {
86         /* The errno value has already been set */
87         return (off64_t)-get_errno();
88     }
89 
90     /* libc seekdir function should set the whence to SEEK_SET, so we can discard
91      * the whence argument here */
92     if (filep->f_oflags & O_DIRECTORY) {
93         /* defensive coding */
94         if (filep->f_dir == NULL) {
95             return (off64_t)-EINVAL;
96         }
97         if (offsetLow == 0) {
98             rewinddir(filep->f_dir);
99         } else {
100             seekdir(filep->f_dir, offsetLow);
101         }
102         ret = telldir(filep->f_dir);
103         if (ret < 0) {
104             return (off64_t)-get_errno();
105         }
106         goto out;
107     }
108 
109     /* Then let file_seek do the real work */
110     ret = file_seek64(filep, offset, whence);
111     if (ret < 0) {
112         return (off64_t)-get_errno();
113     }
114 
115 out:
116     *result = ret;
117 
118     return 0;
119 }
120 
121 #endif
122