Do You Coding?

[function] unlink ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ ๋ณธ๋ฌธ

CS & Engineering/C

[function] unlink ํ•จ์ˆ˜ ์•Œ์•„๋ณด๊ธฐ

๐Ÿ“Œ ๋งค๋‰ด์–ผ (Linux)

๋”๋ณด๊ธฐ

NAME
       unlink - delete a name and possibly the file it refers to
: ์ด๋ฆ„๊ณผ ํŒŒ์ผ์„ ์‚ญ์ œ


SYNOPSIS
       #include <unistd.h>
       int unlink(const char *pathname);
       
DESCRIPTION
       unlink() deletes a name from the filesystem.  If that name was
       the last link to a file and no processes have the file open, the
       file is deleted and the space it was using is made available for
       reuse.

       If the name was the last link to a file but any processes still
       have the file open, the file will remain in existence until the
       last file descriptor referring to it is closed.

       If the name referred to a symbolic link, the link is removed.

       If the name referred to a socket, FIFO, or device, the name for
       it is removed but processes which have the object open may
       continue to use it.
: undlink()๋Š” ํŒŒ์ผ ์‹œ์Šคํ…œ์—์„œ ์ด๋ฆ„์„ ์‚ญ์ œํ•ฉ๋‹ˆ๋‹ค. ํ•ด๋‹น ์ด๋ฆ„์ด ํŒŒ์ผ์— ๋Œ€ํ•œ ๋งˆ์ง€๋ง‰ ๋งํฌ์ด๊ณ 

ํŒŒ์ผ์ด ์—ด๋ฆฌ์ง€ ์•Š๋Š” ํ”„๋กœ์„ธ์Šค๊ฐ€ ์—†๋Š” ๊ฒฝ์šฐ, ํŒŒ์ผ์€ ์‚ญ์ œ๋˜๊ณ  ํŒŒ์ผ์ด ์‚ฌ์šฉ ์ค‘์ด๋˜ ๊ณต๊ฐ„์ด ์žฌ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฆ„์ด ํŒŒ์ผ์— ๋Œ€ํ•œ ๋งˆ์ง€๋ง‰ ๋งํฌ์˜€์ง€๋งŒ ํ”„๋กœ์„ธ์Šค์— ํŒŒ์ผ์ด ์—ด๋ ค ์žˆ๋Š” ๊ฒฝ์šฐ ํ•ด๋‹น ํŒŒ์ผ์„ ์ฐธ์กฐํ•˜๋Š”

๋งˆ์ง€๋ง‰ ํŒŒ์ผ ์„ค๋ช…์ž๊ฐ€ ๋‹ซํž ๋•Œ๊นŒ์ง€ ํŒŒ์ผ์€ ๊ทธ๋Œ€๋กœ ์œ ์ง€๋ฉ๋‹ˆ๋‹ค.

์ด๋ฆ„์ด ๊ธฐํ˜ธ ๋งํฌ๋ฅผ ์ฐธ์กฐํ•˜๋ฉด ๋งํฌ๊ฐ€ ์ œ๊ฑฐ๋ฉ๋‹ˆ๋‹ค.

์ด๋ฆ„์ด ์†Œ์ผ“, FIFO ๋˜๋Š” ์žฅ์น˜๋ฅผ ์–ธ๊ธ‰ํ•œ ๊ฒฝ์šฐ ํ•ด๋‹น ์†Œ์ผ“์˜ ์ด๋ฆ„์€ ์ œ๊ฑฐ๋˜์ง€๋งŒ

๊ฐœ์ฒด๊ฐ€ ์—ด๋ ค ์žˆ๋Š” ํ”„๋กœ์„ธ์Šค๋Š” ๊ณ„์†ํ•ด์„œ ์ด ์†Œ์ผ“์„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.


RETURN VALUE
       On success, zero is returned.  On error, -1 is returned, and
       errno is set to indicate the error.

: ์„ฑ๊ณต ์‹œ 0์ด ๋ฐ˜ํ™˜๋˜๋ฉฐ, ์˜ค๋ฅ˜ ์‹œ -1์ด ๋ฐ˜ํ™˜๋˜๊ณ  errno๊ฐ€ ์„ค์ •๋˜์–ด ์˜ค๋ฅ˜๋ฅผ ๋‚˜ํƒ€๋ƒ…๋‹ˆ๋‹ค.

๐Ÿ“Œ ํ•จ์ˆ˜ ์„ค๋ช…

unlink ํ•จ์ˆ˜๋Š” ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๋Š” system call ํ•จ์ˆ˜๋กœ์„œ, hard link์˜ ์ด๋ฆ„์„ ์‚ญ์ œํ•˜๊ณ 

hard link๊ฐ€ ์ฐธ์กฐํ•˜๋Š” count๋ฅผ 1 ๊ฐ์†Œ์‹œํ‚จ๋‹ค.

 

์—ฌ๊ธฐ์„œ hard link ๋ž€, '์›๋ณธ ํŒŒ์ผ๊ณผ ๋™์ผํ•œ i-node (ํŒŒ์ผ์˜ ๊ณ ์œ  ๋ฒˆํ˜ธ, ํŒŒ์ผ ์ƒ์„ฑ์‹œ ์ฃผ์–ด์ง.) ๋ฅผ ๊ฐ€์ง€๋Š” ๋ณต์‚ฌ๋ณธ' ์œผ๋กœ,

cp๋กœ ๋งŒ๋“œ๋Š” ์ผ๋ฐ˜ ๋ณต์‚ฌ๋ณธ๊ณผ ๋‹ค๋ฅด๊ฒŒ, ์›๋ณธ์„ ์ˆ˜์ •ํ•˜๋“  hard link ๋ณต์‚ฌ๋ณธ์„ ์ˆ˜์ •ํ•˜๋“  ํ•จ๊ป˜ ์ˆ˜์ •๋œ๋‹ค.

i-node ๋กœ 1234๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ํŒŒ์ผ์ด ์ˆ˜์ •๋˜๋ฉด ๊ฐ™์€ i-node : 1234๋ฅผ ๊ฐ€์ง„ ๋ชจ๋“  ํŒŒ์ผ๋“ค์ด ๊ฐ™์ด ์ˆ˜์ •๋˜๋Š” ๊ฒƒ์ด๋‹ค.

๋”ฐ๋ผ์„œ, ์›๋ณธ ํŒŒ์ผ์ด ์ง€์›Œ์ ธ๋„ hard link ํŒŒ์ผ์€ ๋ณด์กด๋˜์–ด ์‹คํ–‰๋œ๋‹ค.

(๋ฐ˜๋Œ€๋˜๋Š” ๊ฐœ๋…์œผ๋กœ soft link๋Š” i-node๊ฐ€ ๋‹ฌ๋ผ ์›๋ณธ์ด ์ง€์›Œ์ง€๋ฉด ๋งํฌ ์—ฐ๊ฒฐ์ด ๋Š๊ธฐ๊ฒŒ ๋œ๋‹ค.)

 

๊ทธ๋ฆฌํ•˜์—ฌ, ์œ„์—์„œ ์–ธ๊ธ‰ํ•œ unlink๋ฅผ ํ†ตํ•ด hard link๊ฐ€ ์ฐธ์กฐํ•˜๋Š” count๊ฐ€ ์ค„์–ด๋“ค์–ด 0์ด ๋˜๋ฉด,

์›๋ณธ๊ณผ hard link ๋ชจ๋‘ ์ง€์›Œ์ง„ ์ƒํƒœ์ด๋ฏ€๋กœ, ํ•ด๋‹น ํŒŒ์ผ์˜ ๋‚ด์šฉ์ด ์ €์žฅ๋œ disk ๊ณต๊ฐ„์„ freeํ•˜๊ฒŒ ๋œ๋‹ค.

hard link๊ฐ€ ์—†๋Š” ์ผ๋ฐ˜ ํŒŒ์ผ์€ ๋ฐ”๋กœ disk ๊ณต๊ฐ„์„ freeํ•˜์—ฌ ์‚ฌ์‹ค์ƒ ํŒŒ์ผ์„ ์‚ญ์ œํ•˜๋Š” ๊ฒƒ์ด๋‹ค.

 

ํ•จ์ˆ˜์˜ ์ธ์ž๋กœ๋Š” const char* pathname ์„ ๋ฐ›๋Š”๋ฐ, ์ด๋Š” ์‚ญ์ œํ•  ํŒŒ์ผ๋ช…์ด๋ฉฐ (full path ๋˜๋Š” ์ƒ๋Œ€ path)

return ๊ฐ’์œผ๋กœ๋Š” '์ •์ƒ์ ์œผ๋กœ file ๋˜๋Š” link๊ฐ€ ์‚ญ์ œ'๋˜๋ฉด 0์„ ๋ฐ˜ํ™˜ํ•˜๊ณ ,

'์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒ'ํ•˜๋ฉด -1์„ ๋ฐ˜ํ™˜ํ•˜๊ณ  ์˜ค๋ฅ˜๋‚ด์šฉ์ด errno์— ์ €์žฅ๋œ๋‹ค.


#include <unistd.h>

int main()
{
    unlink("testabc");
    return (0);
}

 

์‹คํ–‰ ๊ฒฐ๊ณผ)

 

๋‹จ์ˆœํžˆ testabc๋ผ๋Š” ํŒŒ์ผ์„ unlink ํ•˜์—ฌ ์‚ญ์ œํ•ด์ฃผ๋Š” ์ฝ”๋“œ์ด๋‹ค. ์‹คํ–‰ ์‹œ, ํ•ด๋‹น ํŒŒ์ผ์ด ์‚ญ์ œ๋œ ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

ํ•ด๋‹น ํŒŒ์ผ์˜ hard link๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์œผ๋ฏ€๋กœ ๋ฐ”๋กœ disk ๊ณต๊ฐ„์„ freeํ•ด์ค€ ์ƒํƒœ์ด๋‹ค.


ํŠน์ˆ˜ํ•œ ์ƒํ™ฉ์ด ์žˆ๋Š”๋ฐ, ์ง€์šฐ๋ ค๋Š” ํŒŒ์ผ์ด ๋” ์ด์ƒ ์—ฐ๊ฒฐ๋œ hard link๊ฐ€ ์—†์ง€๋งŒ, open๋˜์–ด ์žˆ๋Š” ๊ฒฝ์šฐ์—๋Š”

ํŒŒ์ผ์ด close๋  ๋•Œ๊นŒ์ง€ ํŒŒ์ผ์„ remain, ์œ ์ง€ ์‹œ์ผœ์ค€๋‹ค.

 #include <sys/types.h>
 #include <sys/stat.h>
 #include <unistd.h>
 #include <fcntl.h>
 #include <stdio.h>
 
int main()
{
    int fd = 0;
    fd = open("test_unlink", O_WRONLY | O_CREAT | O_TRUNC);
    unlink("test_unlink");
    char buf[10] = "good";
    int word_len = write(fd, buf, 10);
    if (word_len == 10)
        printf("write O\n");
    else
        printf("write X\n");
    close(fd);
    return (0);
}

 

๊ฒฐ๊ณผ - std_output)

write O

 

open์„ ํ•˜๊ณ , ๋ฐ”๋กœ unlinkํ•œ ์ƒํ™ฉ์ธ๋ฐ, ํ•ด๋‹น ํŒŒ์ผ์ด close๊ฐ€ ๋˜๊ธฐ ์ „๊นŒ์ง€๋Š” ํŒŒ์ผ์„ ์œ ์ง€์‹œ์ผœ์ฃผ๋Š” ํŠน์ง•์ด ์žˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ดํ›„์— write๋ฅผ ํ•˜๋ฉด, ์ •์ƒ์ ์œผ๋กœ write๊ฐ€ ๋˜๋Š” ๋ชจ์Šต์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ , close๋ฅผ ํ†ตํ•ด ๋งˆ์ง€๋ง‰์— ๋‹ซ๊ณ  ํ•ด๋‹น ํŒŒ์ผ์€ ์ง€์›Œ์ง€๊ฒŒ ๋œ๋‹ค.