github.com/akaros/go-akaros@v0.0.0-20181004170632-85005d477eab/src/lib9/dirfwstat.c (about)

     1  // +build !plan9
     2  
     3  /*
     4  Plan 9 from User Space src/lib9/dirfwstat.c
     5  http://code.swtch.com/plan9port/src/tip/src/lib9/dirfwstat.c
     6  
     7  Copyright 2001-2007 Russ Cox.  All Rights Reserved.
     8  
     9  Permission is hereby granted, free of charge, to any person obtaining a copy
    10  of this software and associated documentation files (the "Software"), to deal
    11  in the Software without restriction, including without limitation the rights
    12  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    13  copies of the Software, and to permit persons to whom the Software is
    14  furnished to do so, subject to the following conditions:
    15  
    16  The above copyright notice and this permission notice shall be included in
    17  all copies or substantial portions of the Software.
    18  
    19  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    20  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    21  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
    22  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    23  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    24  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    25  THE SOFTWARE.
    26  */
    27  
    28  #define NOPLAN9DEFINES
    29  #include <u.h>
    30  #include <libc.h>
    31  #include <sys/time.h>
    32  #include <sys/stat.h>
    33  
    34  #if defined(__FreeBSD__) || defined(__APPLE__) || defined(__OpenBSD__) || defined(__linux__) || defined(__akaros__)
    35  /* do nothing -- futimes exists and is fine */
    36  
    37  #elif defined(__SunOS5_9__)
    38  /* use futimesat */
    39  static int
    40  futimes(int fd, struct timeval *tv)
    41  {
    42  	return futimesat(fd, 0, tv);
    43  }
    44  
    45  #else
    46  /* provide dummy */
    47  /* rename just in case -- linux provides an unusable one */
    48  #undef futimes
    49  #define futimes myfutimes
    50  static int
    51  futimes(int fd, struct timeval *tv)
    52  {
    53  	USED(fd);
    54  	USED(tv);
    55  	werrstr("futimes not available");
    56  	return -1;
    57  }
    58  
    59  #endif
    60  
    61  int
    62  dirfwstat(int fd, Dir *dir)
    63  {
    64  	int ret;
    65  	struct timeval tv[2];
    66  
    67  	ret = 0;
    68  #ifndef _WIN32
    69  	if(~dir->mode != 0){
    70  		if(fchmod(fd, (mode_t)dir->mode) < 0)
    71  			ret = -1;
    72  	}
    73  #endif
    74  	if(~dir->mtime != 0){
    75  		tv[0].tv_sec = (time_t)dir->mtime;
    76  		tv[0].tv_usec = 0;
    77  		tv[1].tv_sec = (time_t)dir->mtime;
    78  		tv[1].tv_usec = 0;
    79  		if(futimes(fd, tv) < 0)
    80  			ret = -1;
    81  	}
    82  	return ret;
    83  }
    84