forked from Imagelibrary/rtems
libcsupport: Implement utime() in terms of utimensat()
utime() now calls utimensat() to update file access and modification timestamps. Updated license. Closes #4397
This commit is contained in:
@@ -1,58 +1,66 @@
|
|||||||
|
/* SPDX-License-Identifier: BSD-2-Clause */
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @file
|
* @file
|
||||||
*
|
*
|
||||||
* @brief Set File Access and Modification Times
|
|
||||||
* @ingroup libcsupport
|
* @ingroup libcsupport
|
||||||
|
*
|
||||||
|
* @brief Set file access and modification times in seconds.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* COPYRIGHT (c) 1989-1999.
|
* COPYRIGHT (C) 1989, 2021 On-Line Applications Research Corporation (OAR).
|
||||||
* On-Line Applications Research Corporation (OAR).
|
|
||||||
*
|
*
|
||||||
* The license and distribution terms for this file may be
|
* Redistribution and use in source and binary forms, with or without
|
||||||
* found in the file LICENSE in this distribution or at
|
* modification, are permitted provided that the following conditions
|
||||||
* http://www.rtems.org/license/LICENSE.
|
* are met:
|
||||||
|
* 1. Redistributions of source code must retain the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer.
|
||||||
|
* 2. Redistributions in binary form must reproduce the above copyright
|
||||||
|
* notice, this list of conditions and the following disclaimer in the
|
||||||
|
* documentation and/or other materials provided with the distribution.
|
||||||
|
*
|
||||||
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
||||||
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
||||||
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
||||||
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
||||||
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
||||||
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
||||||
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
||||||
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
||||||
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
||||||
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
||||||
|
* POSSIBILITY OF SUCH DAMAGE.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#ifdef HAVE_CONFIG_H
|
#ifdef HAVE_CONFIG_H
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* FIXME: This include is a workaround for a broken <utime.h> in Newlib */
|
#include <sys/stat.h>
|
||||||
#include <sys/types.h>
|
|
||||||
|
|
||||||
#include <utime.h>
|
|
||||||
|
|
||||||
#include <rtems/libio_.h>
|
#include <rtems/libio_.h>
|
||||||
|
|
||||||
|
#include <fcntl.h>
|
||||||
|
#include <utime.h>
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* POSIX 1003.1b 5.5.6 - Set File Access and Modification Times
|
* https://pubs.opengroup.org/onlinepubs/009604599/functions/utime.html
|
||||||
|
*
|
||||||
|
* Set file access and modification times
|
||||||
*/
|
*/
|
||||||
int utime( const char *path, const struct utimbuf *times )
|
int utime(
|
||||||
|
const char *path,
|
||||||
|
const struct utimbuf *times
|
||||||
|
)
|
||||||
{
|
{
|
||||||
int rv = 0;
|
struct timespec new_times[2];
|
||||||
rtems_filesystem_eval_path_context_t ctx;
|
|
||||||
int eval_flags = RTEMS_FS_FOLLOW_LINK;
|
|
||||||
const rtems_filesystem_location_info_t *currentloc =
|
|
||||||
rtems_filesystem_eval_path_start( &ctx, path, eval_flags );
|
|
||||||
struct utimbuf now_times;
|
|
||||||
|
|
||||||
if ( times == NULL ) {
|
if ( times == NULL ) {
|
||||||
time_t now = time( NULL );
|
return utimensat(AT_FDCWD, path, NULL, 0);
|
||||||
|
|
||||||
now_times.actime = now;
|
|
||||||
now_times.modtime = now;
|
|
||||||
|
|
||||||
times = &now_times;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
rv = (*currentloc->mt_entry->ops->utime_h)(
|
_Timespec_Set(&new_times[0], times->actime, 0);
|
||||||
currentloc,
|
_Timespec_Set(&new_times[1], times->modtime, 0);
|
||||||
times->actime,
|
|
||||||
times->modtime
|
|
||||||
);
|
|
||||||
|
|
||||||
rtems_filesystem_eval_path_cleanup( &ctx );
|
return utimensat(AT_FDCWD, path, new_times, 0);
|
||||||
|
|
||||||
return rv;
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user