Question: Oh Virtuous Shaikh, the custom of the people is that the library of the masjid is from the masjid, adjacent to the masjid. Is it allowed to make ‘Itikaaf in it?
Answer: “If the boundaries of the masjid encompass it and it is included in it, then it is from it. As for if it is not encompassed by the boundaries, then it is not from the masjid. We build upon that, if we say it is from the masjid, that it is allowed to make ‘Itikaaf in it. If he enters it, he does not sit until he prays two raka’at and he does not sit in it and he is Junub except with ablution. If she is menstruating, she does not remain in it.
It is allowed for you to enter to take a book to benefit from it in seeking knowledge, even if you are junub because you are passerby and Allah says:
وَلا جُنُباً إِلَّا عَابِرِي سَبِيل
Nor when you are in a state of Janaba, (i.e. in a state of sexual impurity and have not yet taken a bath) except when travelling on the road (without enough water, or just passing through a mosque)”
[Taken from: https://shamela.ws/browse.php/book-7687/page-3254 ]
Faisal Ibn Abdul Qaadir Ibn Hassan