utime 은 파일의 Access time 및 Modification time 을 변경할 때 사용하는 함수이다.

특정 파일 정렬을 할 때, 파일의 이름을 파싱해서 정렬을 하는 것이 아닌, 파일의 Modification time 을 기준으로 정렬을 해야하는 경우가 있다.
이런 경우, 파일 작업 이후에도 이전의 Modification time 을 유지해야 하는 경우가 있는데,

이런 경우, utime 을 사용하면 편리하다.

 

utime 은 input 인자로 utimebuf 라는 구조체를 사용한다. utime 을 이용하면 access time 과 modification time 을 수정할 수 있다.

struct utimbuf {
    time_t actime;       /* access time */
    time_t modtime;      /* modification time */
};

 

SYNOPSIS

#include <sys/types.h>
#include <utime.h>

int utime(const char *filename, const struct utimbuf *times);

#include <sys/time.h>

int utimes(const char *filename, const struct timeval times[2])

샘플 코드

/*
 * main.c
 * 
 * Copyright 2014 Sungtae Kim <pchero21@gmail.com>
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 * MA 02110-1301, USA.
 * 
 * 
 */


#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <utime.h>
#include <stdlib.h>
#include <time.h>

void file_stat(struct stat sb);


int main(int argc, char **argv)
{
    struct stat sb[2];
    int i;

    if (argc != 3) {
       fprintf(stderr, "Usage: %s <pathname>\n", argv[0]);
       exit(EXIT_FAILURE);
    }
    
    printf("Before utime-----------------------------------\n");
    for(i = 0; i < 2; i++)
    {
        if (stat(argv[i + 1], &sb[i]) == -1) {
            perror("stat");
            exit(EXIT_FAILURE);
        }

        file_stat(sb[i]);
        printf("--------\n");
    }
    
    struct utimbuf ubuf;
    ubuf.actime = sb[0].st_atime;
    ubuf.modtime = sb[0].st_mtime;

    // Let's use utime!
    utime(argv[2], &ubuf);

    printf("\n\nAfter utime-----------------------------------\n");
    for(i = 0; i < 2; i++)
    {
        if (stat(argv[i + 1], &sb[i]) == -1) {
            perror("stat");
            exit(EXIT_FAILURE);
        }
        file_stat(sb[i]);
        printf("--------\n");
    }

    return 0;
}


void file_stat(struct stat sb)
{
    switch (sb.st_mode & S_IFMT) {
        case S_IFBLK:  printf("block device\n");            break;
        case S_IFCHR:  printf("character device\n");        break;
        case S_IFDIR:  printf("directory\n");               break;
        case S_IFIFO:  printf("FIFO/pipe\n");               break;
        case S_IFLNK:  printf("symlink\n");                 break;
        case S_IFREG:  printf("regular file\n");            break;
        case S_IFSOCK: printf("socket\n");                  break;
        default:       printf("unknown?\n");                break;
    }
    
    printf("I-node number:            %ld\n", (long) sb.st_ino);

    printf("Mode:                     %lo (octal)\n",
           (unsigned long) sb.st_mode);

    printf("Link count:               %ld\n", (long) sb.st_nlink);
    printf("Ownership:                UID=%ld   GID=%ld\n",
           (long) sb.st_uid, (long) sb.st_gid);

    printf("Preferred I/O block size: %ld bytes\n",
           (long) sb.st_blksize);
    printf("File size:                %lld bytes\n",
           (long long) sb.st_size);
    printf("Blocks allocated:         %lld\n",
           (long long) sb.st_blocks);

    printf("Last status change:       %s", ctime(&sb.st_ctime));
    printf("Last file access:         %s", ctime(&sb.st_atime));
    printf("Last file modification:   %s", ctime(&sb.st_mtime));
}

 

결과

pchero@MyGalaxy:~/workspace/Study/Program/file/stat_example$ ./main ./test.txt test2.txt 
Before utime-----------------------------------
regular file
I-node number:            12976592
Mode:                     100664 (octal)
Link count:               1
Ownership:                UID=1000   GID=1000
Preferred I/O block size: 4096 bytes
File size:                0 bytes
Blocks allocated:         0
Last status change:       Thu Aug  7 11:08:25 2014
Last file access:         Thu Aug  7 10:56:34 2014
Last file modification:   Thu Aug  7 10:56:34 2014
--------
regular file
I-node number:            12976593
Mode:                     100664 (octal)
Link count:               1
Ownership:                UID=1000   GID=1000
Preferred I/O block size: 4096 bytes
File size:                0 bytes
Blocks allocated:         0
Last status change:       Thu Aug  7 11:11:53 2014
Last file access:         Thu Aug  7 11:11:53 2014
Last file modification:   Thu Aug  7 11:11:53 2014
--------


After utime-----------------------------------
regular file
I-node number:            12976592
Mode:                     100664 (octal)
Link count:               1
Ownership:                UID=1000   GID=1000
Preferred I/O block size: 4096 bytes
File size:                0 bytes
Blocks allocated:         0
Last status change:       Thu Aug  7 11:08:25 2014
Last file access:         Thu Aug  7 10:56:34 2014
Last file modification:   Thu Aug  7 10:56:34 2014
--------
regular file
I-node number:            12976593
Mode:                     100664 (octal)
Link count:               1
Ownership:                UID=1000   GID=1000
Preferred I/O block size: 4096 bytes
File size:                0 bytes
Blocks allocated:         0
Last status change:       Thu Aug  7 11:12:01 2014
Last file access:         Thu Aug  7 10:56:34 2014
Last file modification:   Thu Aug  7 10:56:34 2014
--------

 

 

Tags: ,

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.