source: trunk/kernel/syscalls/sys_rename.c @ 631

Last change on this file since 631 was 613, checked in by alain, 6 years ago
  • introduce the sys_rename.c file to support the mv command.
  • introduce the user_dir.c file to support the ls command.
File size: 4.4 KB
RevLine 
[613]1/*
2 * sys_rename.c - Rename a file or a directory.
3 *
4 * Author        Alain Greiner (2016,2017,2018)
5 *
6 * Copyright (c) UPMC Sorbonne Universites
7 *
8 * This file is part of ALMOS-MKH.
9 *
10 * ALMOS-MKH is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; version 2.0 of the License.
13 *
14 * ALMOS-MKH is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17 * General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with ALMOS-MKH; if not, write to the Free Software Foundation,
21 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24#include <kernel_config.h>
25#include <hal_kernel_types.h>
26#include <hal_uspace.h>
27#include <errno.h>
28#include <printk.h>
29#include <thread.h>
30#include <vfs.h>
31#include <process.h>
32#include <remote_rwlock.h>
33
34#include <syscalls.h>
35
36///////////////////////////
37int sys_rename( char * old,
38                char * new )
39{
40    error_t        error;
41    xptr_t         old_root_inode_xp;   // extended pointer on old path root inode
42    xptr_t         new_root_inode_xp;   // extended pointer on new path root inode
43
44    char           k_old[CONFIG_VFS_MAX_PATH_LENGTH];
45    char           k_new[CONFIG_VFS_MAX_PATH_LENGTH];
46
47    thread_t     * this     = CURRENT_THREAD;
48    process_t    * process  = this->process;
49
50#if (DEBUG_SYS_RENAME || CONFIG_INSTRUMENTATION_SYSCALLS)
51uint64_t     tm_start = hal_get_cycles();
52#endif
53
54    // check old name length
55    if( hal_strlen_from_uspace( old ) >= CONFIG_VFS_MAX_PATH_LENGTH )
56    {
57
58#if DEBUG_SYSCALLS_ERROR
59printk("\n[ERROR] in %s : old name too long\n", __FUNCTION__ );
60#endif
61        this->errno = ENFILE;
62        return -1;
63    }
64
65    // check new name length
66    if( hal_strlen_from_uspace( new ) >= CONFIG_VFS_MAX_PATH_LENGTH )
67    {
68
69#if DEBUG_SYSCALLS_ERROR
70printk("\n[ERROR] in %s : new name too long\n", __FUNCTION__ );
71#endif
72        this->errno = ENFILE;
73        return -1;
74    }
75
76    // copy old name an new name in kernel space
77    hal_strcpy_from_uspace( k_old , old , CONFIG_VFS_MAX_PATH_LENGTH );
78    hal_strcpy_from_uspace( k_new , new , CONFIG_VFS_MAX_PATH_LENGTH );
79
80#if DEBUG_SYS_RENAME
81if( DEBUG_SYS_RENAME < tm_start )
82printk("\n[%s] thread[%x,%x] enter / old <%s> / new <%s> / cycle %d\n",
83__FUNCTION__, process->pid, this->trdid, k_old, k_new, (uint32_t)tm_start );
84#endif
85 
86    // get extended pointer on current working directory
87    xptr_t      ref_xp  = process->ref_xp;
88    process_t * ref_ptr = GET_PTR( ref_xp );
89    cxy_t       ref_cxy = GET_CXY( ref_xp );
90    xptr_t      cwd_xp  = hal_remote_l64( XPTR( ref_cxy , &ref_ptr->cwd_xp ) );
91
92    // compute root inode for old path
93    if( k_old[0] == '/' )   old_root_inode_xp = process->vfs_root_xp;
94    else                    old_root_inode_xp = cwd_xp;
95
96    // compute root inode for new path
97    if( k_new[0] == '/' )   new_root_inode_xp = process->vfs_root_xp;
98    else                    new_root_inode_xp = cwd_xp;
99
100    // call the VFS function to creates the new link
101    // this function takes & releases the lock protecting the Inode Tree
102    error = vfs_link( old_root_inode_xp,
103                      k_old,
104                      new_root_inode_xp,
105                      k_new );
106    if( error )
107    {
108
109#if DEBUG_SYSCALLS_ERROR
110printk("\n[ERROR] in %s : cannot link new <%s>\n", __FUNCTION__, k_new );
111#endif
112        this->errno = ENFILE;
113        return -1;
114    }
115
116    // call the VFS function to remove the old link
117    // this function takes & releases the lock protecting the Inode Tree
118    error = vfs_unlink( old_root_inode_xp,
119                        k_old );
120    if( error )
121    {
122
123#if DEBUG_SYSCALLS_ERROR
124printk("\n[ERROR] in %s : cannot unlink old <%s>\n", __FUNCTION__, k_old );
125#endif
126        this->errno = ENFILE;
127        return -1;
128    }
129
130    hal_fence();
131
132#if (DEBUG_SYS_RENAME || CONFIG_INSTRUMENTATION_SYSCALLS)
133uint64_t     tm_end = hal_get_cycles();
134#endif
135
136#if DEBUG_SYS_RENAME
137if( DEBUG_SYS_RENAME < tm_end )
138printk("\n[%s] thread[%x,%x] exit / cycle %d\n",
139__FUNCTION__, process->pid, this->trdid, (uint32_t)tm_end );
140#endif
141 
142#if CONFIG_INSTRUMENTATION_SYSCALLS
143hal_atomic_add( &syscalls_cumul_cost[SYS_RENAME] , tm_end - tm_start );
144hal_atomic_add( &syscalls_occurences[SYS_RENAME] , 1 );
145#endif
146
147    return 0;
148
149}  // end sys_rename()
Note: See TracBrowser for help on using the repository browser.