본문 바로가기
C++ 200제/코딩 IT 정보

리눅스 Patch 사용 방법과 명령어 (Linux 패치 확인)

by vicddory 2018. 2. 14.

리눅스 Patch 사용 방법과 명령어 (Linux 패치 확인)

 

기본적인 튜토리얼은 아래 사이트를 참조하세요.

 

리눅스 Patch 참조할 사이트

 

1. Introduction: Using diff and linux patch [링크]

2. linux patch(1) - Linux man page (개인적으로 추천하는 사이트) [링크]

3. How to apply a linux patch [링크]

 

리눅스 Patch 사용 명령어 (Linux 패치)
[Linux Patch 방법, 옵션]

 

튜토리얼은 생략하고 바로 patch 하시려면 아래 내용을 따라 하세요.

 

우선, 리눅스 patch의 기본 형태는 아래와 같습니다.

 

1
[rechosen@localhost ~]$ diff originalfile updatedfile
cs

 

패치 방법에는 두 가지가 있습니다.

 

압축되지 않은 패치 대상인 파일들을 교체하거나, 압축된 패치 대상들을 바로 교체하는 방법입니다. 기존에 이미 압축되지 않은 패치 대상 파일들이 존재하는데 엔터만 누른다면, 제목과 같은 메시지가 출력되면서 패치가 정상적으로 이뤄지지 않습니다.

 

리눅스 patch 과정에서 Y나 N을 눌러줘야 하는데 좀 복잡하고 귀찮습니다. 그래서 압축된 파일 자체의 내부 파일들을 linux patch 합니다.

 

아래 그림은 압축된 파일을 패치한 결과입니다.

 

리눅스 patch 사용 예 - 성공하면 succeeded 확인 됨
리눅스 patch 사용 예 - 성공하면 succeeded 확인 됨

 

성공 메세지와 패치 명령어가 보이죠?

 

기존의 파일들을 Reverse 한다는 의미의 -R 옵션을 추가해줘야 합니다.

 

리눅스 patch의 튜토리얼 사이트를 참고해 보면,

 

-R or --reverse

 

Assume that this linux patch was created with the old and new files swapped.

(Yes, I'm afraid that does happen occasionally, human nature being what it is.)

patch attempts to swap each hunk around before applying it. Rejects come out in the swapped format.

The -R option does not work with ed diff scripts because there is too little information to reconstruct the reverse operation.

 

If the first hunk of a patch fails, patch reverses the hunk to see if it can be applied that way.

If it can, you are asked if you want to have the -R option set. If it can't, the patch continues to be applied normally.

(Note: this method cannot detect a reversed linux patch if it is a normal diff and if the first command is an append

(i.e. it should have been a delete) since appends always succeed, due to the fact that a null context matches anywhere.

Luckily, most patches add or change lines rather than delete them, so most reversed normal diffs begin with a delete, which fails, triggering the heuristic.)

 

Replaced 보단 swapped란 단어를 사용해서 잘 이해는 안 되지만;;

 

리눅스 Patch라는 게 별것 없고, 파일만 교체해 주는 것이라 만약 리눅스 GUI를 사용하신다면 관리자 계정으로 압축 해제 후 붙여넣기 해도 상관은 없습니다.

 

 

1
patch -p0 < target.patch
cs

 

위의 명령어를 사용하는 것은 target.patch 내부의 파일을 밖으로 끄집어낸다는 의미이니, 명령어 입력 후에 밖으로 나온 파일들만 복사 후 붙여넣기를 해도 된다는 것이죠.

 

리눅스 Patch 사용 방법과 명령어 (Linux 패치 확인)

댓글